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.mapreduce.util.OafDecoder;
|
6
|
import eu.dnetlib.data.proto.OafProtos;
|
7
|
import eu.dnetlib.data.proto.ResultProtos;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
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.hbase.util.Bytes;
|
14
|
import org.apache.hadoop.io.Text;
|
15
|
|
16
|
import java.io.IOException;
|
17
|
|
18
|
/**
|
19
|
* Exports the result identifiers as json.
|
20
|
*
|
21
|
* @author claudio
|
22
|
*/
|
23
|
public class ExportSoftwaresMapper extends TableMapper<Text, Text> {
|
24
|
|
25
|
/**
|
26
|
* logger.
|
27
|
*/
|
28
|
private static final Log log = LogFactory.getLog(ExportSoftwaresMapper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
29
|
|
30
|
private static final String CF = "result";
|
31
|
|
32
|
private Text keyOut;
|
33
|
|
34
|
private Text valueOut;
|
35
|
|
36
|
@Override
|
37
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
38
|
super.setup(context);
|
39
|
|
40
|
keyOut = new Text("");
|
41
|
valueOut = new Text("");
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
protected void map(final ImmutableBytesWritable keyIn, final Result value, final Context context) throws IOException, InterruptedException {
|
46
|
try {
|
47
|
final byte[] body = value.getValue(Bytes.toBytes(CF), DedupUtils.BODY_B);
|
48
|
|
49
|
if (body == null) {
|
50
|
context.getCounter(CF, "missing body").increment(1);
|
51
|
return;
|
52
|
}
|
53
|
|
54
|
final OafProtos.Oaf d = OafDecoder.decode(body).getOaf();
|
55
|
|
56
|
String classId = d.getEntity().getResult().getMetadata().getResulttype().getClassid();
|
57
|
if (!classId.equals("software")){
|
58
|
context.getCounter(CF, classId).increment(1);
|
59
|
return;
|
60
|
}
|
61
|
|
62
|
String rowId = new String(keyIn.copyBytes());
|
63
|
if (rowId.contains("dedup_wf")){
|
64
|
context.getCounter(CF, "root record").increment(1);
|
65
|
return;
|
66
|
}
|
67
|
|
68
|
valueOut.set(JsonFormat.printToString(d.getEntity()));
|
69
|
|
70
|
context.write(keyOut, valueOut);
|
71
|
|
72
|
} catch (final Throwable e) {
|
73
|
log.error("error exporting the following record from HBase: " + value.toString(), e);
|
74
|
context.getCounter("error", e.getClass().getName()).increment(1);
|
75
|
throw new RuntimeException(e);
|
76
|
}
|
77
|
}
|
78
|
|
79
|
}
|