1
|
package eu.dnetlib.data.mapreduce.hbase.dataexport;
|
2
|
|
3
|
import com.google.common.base.Splitter;
|
4
|
import com.google.common.collect.Lists;
|
5
|
import com.google.common.collect.Sets;
|
6
|
import com.googlecode.protobuf.format.JsonFormat;
|
7
|
import eu.dnetlib.data.mapreduce.util.DedupUtils;
|
8
|
import eu.dnetlib.data.proto.OafProtos;
|
9
|
import eu.dnetlib.data.proto.TypeProtos;
|
10
|
import org.apache.hadoop.hbase.client.Result;
|
11
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
12
|
import org.apache.hadoop.hbase.mapreduce.TableMapper;
|
13
|
import org.apache.hadoop.io.Text;
|
14
|
import org.apache.hadoop.mapreduce.Mapper;
|
15
|
|
16
|
import java.io.IOException;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
|
20
|
public class ConfigurableExportMapper extends TableMapper<Text, Text> {
|
21
|
|
22
|
private static final String CF_DEFAULT = "result";
|
23
|
private static final String QUALIFIER_DEFAULT = "body";
|
24
|
|
25
|
private Text keyOut;
|
26
|
private Text valueOut;
|
27
|
|
28
|
private Set<String> columnFamilies = Sets.newHashSet();
|
29
|
private Set<String> qualifiers = Sets.newHashSet();
|
30
|
|
31
|
enum ExportMode {
|
32
|
ALL,
|
33
|
DUPS_ONLY
|
34
|
}
|
35
|
|
36
|
private ExportMode exportMode = ExportMode.ALL;
|
37
|
|
38
|
@Override
|
39
|
protected void setup(Context context) throws IOException, InterruptedException {
|
40
|
keyOut = new Text("");
|
41
|
valueOut = new Text();
|
42
|
|
43
|
|
44
|
columnFamilies.addAll(Lists.newArrayList(Splitter.on(",").split(context.getConfiguration().get("column.families", CF_DEFAULT))));
|
45
|
qualifiers.addAll(Lists.newArrayList(Splitter.on(",").split(context.getConfiguration().get("qualifiers", QUALIFIER_DEFAULT))));
|
46
|
exportMode = ExportMode.valueOf(context.getConfiguration().get("export.mode", ExportMode.ALL.toString()));
|
47
|
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
|
52
|
for(String cf : columnFamilies) {
|
53
|
switch (exportMode) {
|
54
|
case ALL:
|
55
|
export(value, context, cf);
|
56
|
break;
|
57
|
case DUPS_ONLY:
|
58
|
final Map<byte[], byte[]> mergedInMap = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(cf));
|
59
|
if (mergedInMap != null && !mergedInMap.isEmpty()) {
|
60
|
export(value, context, cf);
|
61
|
}
|
62
|
|
63
|
break;
|
64
|
}
|
65
|
}
|
66
|
}
|
67
|
|
68
|
private void export(Result value, Context context, String cf) throws IOException, InterruptedException {
|
69
|
for(String q : qualifiers) {
|
70
|
|
71
|
final byte[] data = value.getValue(cf.getBytes(), q.getBytes());
|
72
|
|
73
|
if (data != null) {
|
74
|
OafProtos.Oaf oaf = OafProtos.Oaf.parseFrom(data);
|
75
|
valueOut.set(JsonFormat.printToString(oaf));
|
76
|
context.write(keyOut, valueOut);
|
77
|
|
78
|
context.getCounter(cf, q).increment(1);
|
79
|
}
|
80
|
}
|
81
|
}
|
82
|
|
83
|
|
84
|
}
|