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