1
|
package eu.dnetlib.data.mapreduce.hbase.statsExport.mapreduce;
|
2
|
|
3
|
import com.google.common.collect.ArrayListMultimap;
|
4
|
import com.google.common.collect.Multimap;
|
5
|
import eu.dnetlib.data.mapreduce.hbase.index.config.EntityConfigTable;
|
6
|
import eu.dnetlib.data.mapreduce.hbase.index.config.IndexConfig;
|
7
|
import eu.dnetlib.data.mapreduce.hbase.index.config.LinkDescriptor;
|
8
|
import eu.dnetlib.data.mapreduce.hbase.statsExport.utils.Serializer;
|
9
|
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder;
|
10
|
import eu.dnetlib.data.mapreduce.util.RelDescriptor;
|
11
|
import eu.dnetlib.data.mapreduce.util.UpdateMerger;
|
12
|
import eu.dnetlib.data.proto.KindProtos;
|
13
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
14
|
import eu.dnetlib.data.proto.OafProtos.OafRel;
|
15
|
import eu.dnetlib.data.proto.OafProtos.OafRelOrBuilder;
|
16
|
import org.apache.hadoop.hbase.client.Result;
|
17
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
18
|
import org.apache.hadoop.hbase.mapreduce.TableMapper;
|
19
|
import org.apache.hadoop.hbase.util.Bytes;
|
20
|
import org.apache.hadoop.io.Text;
|
21
|
import org.apache.log4j.Logger;
|
22
|
|
23
|
import java.io.IOException;
|
24
|
import java.util.ArrayList;
|
25
|
import java.util.List;
|
26
|
import java.util.Map;
|
27
|
import java.util.Map.Entry;
|
28
|
|
29
|
/**
|
30
|
* Mapper Class that reads HBASE contents and prepares them for the StatsDB
|
31
|
* export
|
32
|
*/
|
33
|
public class StatsMapper extends TableMapper<Text, ImmutableBytesWritable> {
|
34
|
private Logger log = Logger.getLogger(this.getClass());
|
35
|
|
36
|
//Index configuratios for OAFs
|
37
|
private EntityConfigTable entityConfigTable;
|
38
|
|
39
|
//Counters for produced entities. Visible in the JobTracker monitoring page.
|
40
|
private enum STATS_COUNTERS {
|
41
|
datasource,
|
42
|
organization,
|
43
|
result,
|
44
|
person,
|
45
|
project
|
46
|
}
|
47
|
|
48
|
|
49
|
private enum REL_COUNTERS {
|
50
|
resultProject,
|
51
|
datasourceOrganization,
|
52
|
organizationOrganization,
|
53
|
resultResult,
|
54
|
personPerson,
|
55
|
personResult,
|
56
|
projectOrganization,
|
57
|
projectPerson,
|
58
|
resultOrganization
|
59
|
}
|
60
|
|
61
|
//Counter for corrupted records.
|
62
|
private enum ERROR_COUNTERS {
|
63
|
rottenRecords,
|
64
|
rottenRelations
|
65
|
}
|
66
|
|
67
|
//Init class: Load Index config and mapping for DB tables.
|
68
|
@Override
|
69
|
protected void setup(Context context) {
|
70
|
loadEntityConfig(context);
|
71
|
}
|
72
|
|
73
|
|
74
|
//Read HBASE table and decode Protos to OAF entities.
|
75
|
@Override
|
76
|
protected void map(final ImmutableBytesWritable keyIn, final Result result, final Context context) {
|
77
|
|
78
|
|
79
|
try {
|
80
|
OafRowKeyDecoder keyDecoder = OafRowKeyDecoder.decode(keyIn.copyBytes());
|
81
|
Oaf oaf = UpdateMerger.mergeBodyUpdates(context, result.getFamilyMap(Bytes.toBytes(keyDecoder.getType().toString())));
|
82
|
|
83
|
if (isValid(oaf))
|
84
|
emitProtos(context, result, oaf);
|
85
|
|
86
|
} catch (Exception e) {
|
87
|
System.err.println("Unable to parse proto in row: " + new String(keyIn.copyBytes()));
|
88
|
log.error("Unable to parse proto in row: " + new String(keyIn.copyBytes()), e);
|
89
|
|
90
|
context.getCounter(ERROR_COUNTERS.rottenRecords).increment(1);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
|
95
|
private boolean isValid(Oaf oaf) {
|
96
|
return oaf != null && oaf.isInitialized() && !oaf.getDataInfo().getDeletedbyinference() && !oaf.getDataInfo().getInvisible();
|
97
|
}
|
98
|
|
99
|
|
100
|
private void emitProtos(Context context, Result result, Oaf oaf) {
|
101
|
|
102
|
Oaf.Builder oafBuilder = Oaf.newBuilder().setKind(oaf.getKind()).
|
103
|
setDataInfo(oaf.getDataInfo()).setLastupdatetimestamp(oaf.getLastupdatetimestamp());
|
104
|
|
105
|
oafBuilder.setEntity(oaf.getEntity());
|
106
|
|
107
|
// emit relation first so we can cache them to entity protos
|
108
|
emitRelation(context, result, oaf, oafBuilder);
|
109
|
emitEntity(context, oafBuilder.build());
|
110
|
|
111
|
}
|
112
|
|
113
|
private void emitEntity(Context context, Oaf oaf) {
|
114
|
|
115
|
String serialized = Serializer.serialize(oaf, context.getConfiguration().get("stats.delim"), context.getConfiguration().get("stats.enclChar"));
|
116
|
if (serialized != null) {
|
117
|
try {
|
118
|
Text TextKeyOut = new Text(oaf.getEntity().getType().toString() + "," + Serializer.getId(oaf, context.getConfiguration().get("stats.delim"), context.getConfiguration().get("stats.enclChar")));
|
119
|
context.write((TextKeyOut), new ImmutableBytesWritable(serialized.getBytes()));
|
120
|
context.getCounter(STATS_COUNTERS.valueOf(oaf.getEntity().getType().toString())).increment(1);
|
121
|
|
122
|
} catch (Exception e) {
|
123
|
log.error("Error writing entity to M/R output", e);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
}
|
128
|
|
129
|
// may have multiple relations per each field
|
130
|
private void emitRelation(Context context, Result result, Oaf oaf, Oaf.Builder builder) {
|
131
|
|
132
|
try {
|
133
|
|
134
|
// writing out : personResult,ID,data
|
135
|
// derived relations; ResultLanguages, resultConcepts etc are
|
136
|
// created here
|
137
|
|
138
|
Multimap<String, String> relMap = ArrayListMultimap.create();
|
139
|
|
140
|
Serializer.extractRelations(oaf, context.getConfiguration().get("stats.delim"), context.getConfiguration().get("stats.enclChar"), relMap);
|
141
|
|
142
|
if (!relMap.isEmpty()) {
|
143
|
for (Entry<String, String> rel : relMap.entries()) {
|
144
|
Text TextKeyOut = new Text(rel.getKey() + "," + Serializer.getId(oaf, context.getConfiguration().get("stats.delim"), context.getConfiguration().get("stats.enclChar")));
|
145
|
//TODO here output
|
146
|
context.write((TextKeyOut), new ImmutableBytesWritable(rel.getValue().getBytes()));
|
147
|
|
148
|
}
|
149
|
}
|
150
|
|
151
|
|
152
|
} catch (Throwable e) {
|
153
|
log.error("Error writing relation to M/R output", e);
|
154
|
}
|
155
|
|
156
|
|
157
|
// Existing Hbase relations are generated here
|
158
|
if (entityConfigTable.getDescriptors(oaf.getEntity().getType()) != null) {
|
159
|
|
160
|
for (LinkDescriptor ld : entityConfigTable.getDescriptors(oaf.getEntity().getType())) {
|
161
|
|
162
|
try {
|
163
|
|
164
|
ArrayList<OafRel> oafRels = new ArrayList<OafRel>();
|
165
|
decodeRelation(oaf, context, result, ld, oafRels);
|
166
|
|
167
|
for (OafRel rel : oafRels) {
|
168
|
{
|
169
|
|
170
|
// builder.getEntityBuilder().addCachedRel(rel);
|
171
|
|
172
|
Text TextKeyOut = new Text(ld.getRelDescriptor().getRelType().toString()
|
173
|
+ "," + Serializer.getId(rel, context.getConfiguration().get("stats.delim"),
|
174
|
context.getConfiguration().get("stats.enclChar")));
|
175
|
|
176
|
String buff = Serializer.serialize(rel, context.getConfiguration().get("stats.delim"), context.getConfiguration().get("stats.enclChar"));
|
177
|
|
178
|
context.write((TextKeyOut), new ImmutableBytesWritable(buff.getBytes()));
|
179
|
|
180
|
context.getCounter(REL_COUNTERS.valueOf(rel.getRelType().toString())).increment(1);
|
181
|
}
|
182
|
|
183
|
}
|
184
|
} catch (Throwable e) {
|
185
|
log.error("Error for record ", e);
|
186
|
context.getCounter(ERROR_COUNTERS.rottenRelations).increment(1);
|
187
|
|
188
|
}
|
189
|
|
190
|
}
|
191
|
}
|
192
|
|
193
|
}
|
194
|
|
195
|
private void decodeRelation(final Oaf body, final Context context, Result result, final LinkDescriptor ld, List<OafRel> rels) {
|
196
|
|
197
|
try {
|
198
|
final Oaf.Builder oafBuilder = Oaf.newBuilder().setKind(KindProtos.Kind.relation);
|
199
|
final Map<byte[], byte[]> columnMap = result.getFamilyMap(Bytes.toBytes(ld.getRelDescriptor().getIt()));
|
200
|
|
201
|
if (hasData(columnMap)) {
|
202
|
|
203
|
for (Entry<byte[], byte[]> e : columnMap.entrySet()) {
|
204
|
|
205
|
Oaf decodedOaf = decodeProto(e.getValue(), context);
|
206
|
|
207
|
OafRel.Builder relBuilder = OafRel.newBuilder(decodedOaf.getRel());
|
208
|
|
209
|
if (ld.isSymmetric()) {
|
210
|
RelDescriptor rd = ld.getRelDescriptor();
|
211
|
relBuilder.setCachedTarget(body.getEntity()).setRelType(rd.getRelType()).setSubRelType(rd.getSubRelType());
|
212
|
}
|
213
|
|
214
|
|
215
|
OafRel oafRel = relBuilder.setChild(ld.isChild()).build();
|
216
|
rels.add(oafBuilder.setDataInfo(decodedOaf.getDataInfo()).setRel(oafRel).build().getRel());
|
217
|
|
218
|
|
219
|
|
220
|
}
|
221
|
}
|
222
|
|
223
|
} catch (Throwable throwable) {
|
224
|
log.error("Error Decoding relation for: " + body.getRel().getRelType() + " " + body.getEntity().getId() + " ", throwable);
|
225
|
context.getCounter(ERROR_COUNTERS.rottenRelations).increment(1);
|
226
|
|
227
|
}
|
228
|
}
|
229
|
|
230
|
|
231
|
private Oaf decodeProto(final byte[] body, Context context) {
|
232
|
try {
|
233
|
return Oaf.parseFrom(body);
|
234
|
} catch (Exception e) {
|
235
|
log.error(e);
|
236
|
context.getCounter(ERROR_COUNTERS.rottenRecords).increment(1);
|
237
|
|
238
|
|
239
|
}
|
240
|
return null;
|
241
|
}
|
242
|
|
243
|
private void loadEntityConfig(Context context) {
|
244
|
String indexConf = context.getConfiguration().get("stats.indexConf");
|
245
|
|
246
|
if (indexConf == null || indexConf.isEmpty()) {
|
247
|
log.error("NULL ENTITY CONFIGURATION TABLE IN MAPPER : ");
|
248
|
|
249
|
}
|
250
|
|
251
|
entityConfigTable = IndexConfig.load(indexConf).getConfigMap();
|
252
|
|
253
|
}
|
254
|
|
255
|
private boolean isDedupSelf(final OafRelOrBuilder rel) {
|
256
|
return rel.getSource().contains(rel.getTarget());
|
257
|
}
|
258
|
|
259
|
private boolean hasData(final Map<byte[], byte[]> columnMap) {
|
260
|
return columnMap != null && !columnMap.isEmpty();
|
261
|
}
|
262
|
|
263
|
@Override
|
264
|
protected void cleanup(Context context) throws IOException, InterruptedException {
|
265
|
|
266
|
super.cleanup(context);
|
267
|
}
|
268
|
|
269
|
public EntityConfigTable getEntityConfigTable() {
|
270
|
return entityConfigTable;
|
271
|
}
|
272
|
|
273
|
public void setEntityConfigTable(EntityConfigTable entityConfigTable) {
|
274
|
this.entityConfigTable = entityConfigTable;
|
275
|
}
|
276
|
|
277
|
}
|