1
|
package eu.dnetlib.data.mapreduce.hbase.lodImport;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.sql.Connection;
|
5
|
import java.sql.PreparedStatement;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Arrays;
|
8
|
import java.util.Iterator;
|
9
|
import java.util.List;
|
10
|
|
11
|
import org.apache.hadoop.conf.Configuration;
|
12
|
import org.apache.hadoop.io.NullWritable;
|
13
|
import org.apache.hadoop.io.Text;
|
14
|
import org.apache.hadoop.mapreduce.Reducer;
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.json.JSONObject;
|
17
|
|
18
|
import com.jolbox.bonecp.BoneCPDataSource;
|
19
|
|
20
|
import eu.dnetlib.data.mapreduce.hbase.lodImport.utils.DB;
|
21
|
import eu.dnetlib.data.mapreduce.hbase.lodImport.utils.MapCountries;
|
22
|
import eu.dnetlib.data.mapreduce.hbase.lodImport.utils.MapLanguages;
|
23
|
import eu.dnetlib.data.mapreduce.hbase.lodImport.utils.RDFizer;
|
24
|
|
25
|
|
26
|
public class LodImportReducer extends Reducer<Text, Text, NullWritable, NullWritable> {
|
27
|
|
28
|
private Logger log = Logger.getLogger(LodImportReducer.class);
|
29
|
private BoneCPDataSource ds;
|
30
|
private String fileName = "";
|
31
|
private Configuration hadoopConf;
|
32
|
// private VirtGraph graph;
|
33
|
private int counter;
|
34
|
private String[] buildQuery = new String[2];
|
35
|
private String insertQuery = "";
|
36
|
private String deleteQuery = "";
|
37
|
private String defaultGraph = "";
|
38
|
private int entitiesPerQuery;
|
39
|
private MapCountries mapCountries;
|
40
|
private MapLanguages mapLanguages;
|
41
|
private Connection conn;
|
42
|
private Connection dConn;
|
43
|
private JSONObject entitiesMappings;
|
44
|
private JSONObject relationsMappings;
|
45
|
|
46
|
|
47
|
public static enum REDUCES_COUNTER {
|
48
|
RESULT,
|
49
|
PROJECT,
|
50
|
DATASOURCE,
|
51
|
PERSON,
|
52
|
ORGANIZATION,
|
53
|
TOTAL_ENTITIES,
|
54
|
TOTAL_RELATIONS
|
55
|
}
|
56
|
|
57
|
|
58
|
@Override
|
59
|
protected void setup(Context context) throws IOException, InterruptedException {
|
60
|
this.hadoopConf = context.getConfiguration();
|
61
|
this.fileName = hadoopConf.get("lod.inputFile");
|
62
|
if (!fileName.contains("relations")) defaultGraph = hadoopConf.get("lod.defaultGraph");
|
63
|
else defaultGraph = hadoopConf.get("lod.relationsGraph");
|
64
|
this.counter = 0;
|
65
|
mapCountries = new MapCountries();
|
66
|
mapLanguages = new MapLanguages();
|
67
|
|
68
|
buildQuery[0] = "";
|
69
|
buildQuery[1] = "";
|
70
|
|
71
|
entitiesMappings = new JSONObject(hadoopConf.get("lod.jsonEntities"));
|
72
|
relationsMappings = new JSONObject(hadoopConf.get("lod.jsonRels"));
|
73
|
|
74
|
if (!fileName.contains("relations")) entitiesPerQuery = Integer.parseInt(hadoopConf.get("lod.entitiesPerQuery"));
|
75
|
else entitiesPerQuery = Integer.parseInt(hadoopConf.get("lod.relationsPerQuery"));
|
76
|
|
77
|
try {
|
78
|
DB db = new DB();
|
79
|
ds = db.getDatasource(hadoopConf.get("lod.conLine"), hadoopConf.get("lod.username"), hadoopConf.get("lod.password"), hadoopConf.get("lod.minCpart"), hadoopConf.get("lod.maxCpart"), hadoopConf.get("lod.part"));
|
80
|
conn = ds.getConnection();
|
81
|
conn.setAutoCommit(false);
|
82
|
} catch (Exception e) {
|
83
|
log.error(e.toString(), e);
|
84
|
}
|
85
|
|
86
|
}
|
87
|
|
88
|
@Override
|
89
|
protected void reduce(final Text key, final Iterable<Text> values, final Context context) throws IOException, InterruptedException {
|
90
|
|
91
|
if (entitiesMappings == null || relationsMappings == null) {
|
92
|
throw new InterruptedException("Could not read json mappings!");
|
93
|
}
|
94
|
Iterator<Text> it = values.iterator();
|
95
|
|
96
|
while (it.hasNext()) {
|
97
|
String data = it.next().toString();
|
98
|
String[] split = data.split(hadoopConf.get("lod.delim"));
|
99
|
List<String> row = new ArrayList<String>(Arrays.asList(split));
|
100
|
|
101
|
if (!fileName.contains("relations")) {
|
102
|
buildQuery = RDFizer.RDFizeEntityRow(row, ds, entitiesMappings, hadoopConf, mapCountries, mapLanguages, defaultGraph);
|
103
|
insertQuery += buildQuery[0];
|
104
|
deleteQuery += buildQuery[1];
|
105
|
} else {
|
106
|
buildQuery = RDFizer.RDFizeRelationRow(row, relationsMappings, hadoopConf);
|
107
|
insertQuery += buildQuery[0];
|
108
|
}
|
109
|
|
110
|
}
|
111
|
|
112
|
counter++;
|
113
|
if (counter > entitiesPerQuery ) {
|
114
|
try {
|
115
|
PreparedStatement ps = conn.prepareStatement("DB.DBA.TTLP_MT (?, ?, ?, ?, ?, ?,?)");
|
116
|
ps.setString(1, insertQuery);
|
117
|
ps.setString(2, "");
|
118
|
ps.setString(3, defaultGraph);
|
119
|
final int NQUAD_LEVEL = 64;
|
120
|
ps.setInt(4, NQUAD_LEVEL);
|
121
|
ps.setInt(5, 0);
|
122
|
ps.setInt(6, 15);
|
123
|
ps.setInt(7, 0);
|
124
|
ps.execute();
|
125
|
|
126
|
conn.commit();
|
127
|
|
128
|
ps.close();
|
129
|
|
130
|
buildQuery = new String[2];
|
131
|
buildQuery[0] = "";
|
132
|
buildQuery[1] = "";
|
133
|
insertQuery = "";
|
134
|
|
135
|
if (!fileName.contains("relations")) {
|
136
|
//s// TODO: keep counters for all entity types
|
137
|
context.getCounter(REDUCES_COUNTER.TOTAL_ENTITIES).increment(counter);
|
138
|
} else {
|
139
|
context.getCounter(REDUCES_COUNTER.TOTAL_RELATIONS).increment(counter);
|
140
|
}
|
141
|
|
142
|
counter = 0;
|
143
|
}catch(Exception e){
|
144
|
log.error("Virtuoso write failed at query: " + insertQuery + " \n and with error " + e.toString(), e);
|
145
|
try {
|
146
|
if (conn != null) {
|
147
|
conn.rollback();
|
148
|
conn.close();
|
149
|
conn = ds.getConnection();
|
150
|
conn.setAutoCommit(false);
|
151
|
} else {
|
152
|
conn.close();
|
153
|
conn = ds.getConnection();
|
154
|
conn.setAutoCommit(false);
|
155
|
}
|
156
|
|
157
|
PreparedStatement ps = conn.prepareStatement("DB.DBA.TTLP_MT (?, ?, ?, ?, ?, ?,?)");
|
158
|
ps.setString(1, insertQuery);
|
159
|
ps.setString(2, "");
|
160
|
ps.setString(3, defaultGraph);
|
161
|
final int NQUAD_LEVEL = 64;
|
162
|
ps.setInt(4, NQUAD_LEVEL);
|
163
|
ps.setInt(5, 0);
|
164
|
ps.setInt(6, 15);
|
165
|
ps.setInt(7, 0);
|
166
|
ps.execute();
|
167
|
|
168
|
conn.commit();
|
169
|
|
170
|
ps.close();
|
171
|
|
172
|
buildQuery = new String[2];
|
173
|
buildQuery[0] = "";
|
174
|
buildQuery[1] = "";
|
175
|
insertQuery = "";
|
176
|
|
177
|
if (!fileName.contains("relations")) {
|
178
|
//s// TODO: keep counters for all entity types
|
179
|
context.getCounter(REDUCES_COUNTER.TOTAL_ENTITIES).increment(counter);
|
180
|
} else {
|
181
|
context.getCounter(REDUCES_COUNTER.TOTAL_RELATIONS).increment(counter);
|
182
|
}
|
183
|
|
184
|
counter = 0;
|
185
|
} catch (Exception e1) {
|
186
|
log.error("Getting another Connection fail:" + e1.toString(), e1);
|
187
|
}
|
188
|
}
|
189
|
|
190
|
}
|
191
|
|
192
|
}
|
193
|
|
194
|
|
195
|
|
196
|
@Override
|
197
|
protected void cleanup(Context context) throws IOException, InterruptedException {
|
198
|
try {
|
199
|
log.info("########### \n Cleanup Started. Commiting last Triples...\n##########");
|
200
|
PreparedStatement ps = conn.prepareStatement("DB.DBA.TTLP_MT (?, ?, ?, ?, ?, ?,?)");
|
201
|
ps.setString(1, insertQuery);
|
202
|
ps.setString(2, "");
|
203
|
ps.setString(3, defaultGraph);
|
204
|
final int NQUAD_LEVEL = 64;
|
205
|
ps.setInt(4, NQUAD_LEVEL);
|
206
|
ps.setInt(5, 0);
|
207
|
ps.setInt(6, 15);
|
208
|
ps.setInt(7, 0);
|
209
|
ps.execute();
|
210
|
|
211
|
conn.commit();
|
212
|
|
213
|
ps.close();
|
214
|
if (!fileName.contains("relations")) {
|
215
|
//s// TODO: keep counters for all entity types
|
216
|
context.getCounter(REDUCES_COUNTER.TOTAL_ENTITIES).increment(counter);
|
217
|
} else {
|
218
|
context.getCounter(REDUCES_COUNTER.TOTAL_RELATIONS).increment(counter);
|
219
|
}
|
220
|
|
221
|
} catch (Exception e) {
|
222
|
log.error("Virtuoso write failed at query: " + insertQuery + " \n and with error " + e.toString(), e);
|
223
|
try {
|
224
|
if (conn != null) {
|
225
|
conn.rollback();
|
226
|
conn.close();
|
227
|
conn = ds.getConnection();
|
228
|
conn.setAutoCommit(false);
|
229
|
PreparedStatement ps = conn.prepareStatement("DB.DBA.TTLP_MT (?, ?, ?, ?, ?, ?,?)");
|
230
|
ps.setString(1, insertQuery);
|
231
|
ps.setString(2, "");
|
232
|
ps.setString(3, defaultGraph);
|
233
|
final int NQUAD_LEVEL = 64;
|
234
|
ps.setInt(4, NQUAD_LEVEL);
|
235
|
ps.setInt(5, 0);
|
236
|
ps.setInt(6, 15);
|
237
|
ps.setInt(7, 0);
|
238
|
ps.execute();
|
239
|
|
240
|
conn.commit();
|
241
|
|
242
|
ps.close();
|
243
|
if (!fileName.contains("relations")) {
|
244
|
//s// TODO: keep counters for all entity types
|
245
|
context.getCounter(REDUCES_COUNTER.TOTAL_ENTITIES).increment(counter);
|
246
|
} else {
|
247
|
context.getCounter(REDUCES_COUNTER.TOTAL_RELATIONS).increment(counter);
|
248
|
}
|
249
|
} else {
|
250
|
conn.close();
|
251
|
conn = ds.getConnection();
|
252
|
conn.setAutoCommit(false);
|
253
|
PreparedStatement ps = conn.prepareStatement("DB.DBA.TTLP_MT (?, ?, ?, ?, ?, ?,?)");
|
254
|
ps.setString(1, insertQuery);
|
255
|
ps.setString(2, "");
|
256
|
ps.setString(3, defaultGraph);
|
257
|
final int NQUAD_LEVEL = 64;
|
258
|
ps.setInt(4, NQUAD_LEVEL);
|
259
|
ps.setInt(5, 0);
|
260
|
ps.setInt(6, 15);
|
261
|
ps.setInt(7, 0);
|
262
|
ps.execute();
|
263
|
|
264
|
conn.commit();
|
265
|
|
266
|
ps.close();
|
267
|
if (!fileName.contains("relations")) {
|
268
|
//s// TODO: keep counters for all entity types
|
269
|
context.getCounter(REDUCES_COUNTER.TOTAL_ENTITIES).increment(counter);
|
270
|
} else {
|
271
|
context.getCounter(REDUCES_COUNTER.TOTAL_RELATIONS).increment(counter);
|
272
|
}
|
273
|
}
|
274
|
|
275
|
} catch (Exception e1) {
|
276
|
log.error("Getting another Connection fail:" + e1.toString(), e1);
|
277
|
}
|
278
|
} finally {
|
279
|
log.info("Cleaning up reducer...\nClosing Graph and Datasource");
|
280
|
try {
|
281
|
conn.commit();
|
282
|
conn.close();
|
283
|
} catch (Exception e) {
|
284
|
log.error("Could not Close connection \n" + e.toString(), e);
|
285
|
}
|
286
|
|
287
|
log.info("Graph and Datasource Closed...");
|
288
|
}
|
289
|
}
|
290
|
|
291
|
}
|