1
|
package eu.dnetlib.data.mapreduce.hbase.dataexport;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Map;
|
5
|
import java.util.Map.Entry;
|
6
|
import java.util.NavigableMap;
|
7
|
import java.util.stream.Stream;
|
8
|
|
9
|
import eu.dnetlib.data.mapreduce.util.DedupUtils;
|
10
|
import eu.dnetlib.data.proto.OafProtos;
|
11
|
import org.apache.commons.lang.StringUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.apache.hadoop.hbase.client.Result;
|
15
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
16
|
import org.apache.hadoop.hbase.mapreduce.TableMapper;
|
17
|
import org.apache.hadoop.hbase.util.Bytes;
|
18
|
import org.apache.hadoop.io.Text;
|
19
|
|
20
|
import com.google.common.base.Joiner;
|
21
|
import com.googlecode.protobuf.format.JsonFormat;
|
22
|
|
23
|
import eu.dnetlib.data.mapreduce.util.OafDecoder;
|
24
|
|
25
|
/**
|
26
|
* Exports Oaf objects as their json serialization.
|
27
|
*
|
28
|
* @author claudio
|
29
|
*
|
30
|
*/
|
31
|
public class ExportInformationSpaceMapper extends TableMapper<Text, Text> {
|
32
|
|
33
|
/**
|
34
|
* logger.
|
35
|
*/
|
36
|
private static final Log log = LogFactory.getLog(ExportInformationSpaceMapper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
37
|
|
38
|
private static final String SEPARATOR = "@";
|
39
|
|
40
|
private Text keyOut;
|
41
|
|
42
|
private Text valueOut;
|
43
|
|
44
|
@Override
|
45
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
46
|
super.setup(context);
|
47
|
|
48
|
keyOut = new Text();
|
49
|
valueOut = new Text();
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
protected void map(final ImmutableBytesWritable keyIn, final Result value, final Context context) throws IOException, InterruptedException {
|
54
|
try {
|
55
|
byte[] rowKey = keyIn.copyBytes();
|
56
|
Stream.of(value.raw())
|
57
|
.filter(kv -> !"".equals(Bytes.toString(kv.getValue())))
|
58
|
.forEach(kv -> {
|
59
|
try {
|
60
|
emit(rowKey, kv.getFamily(), kv.getQualifier(), kv.getValue(), context);
|
61
|
} catch (IOException | InterruptedException e) {
|
62
|
throw new RuntimeException(e);
|
63
|
}
|
64
|
});
|
65
|
|
66
|
} catch (final Throwable e) {
|
67
|
//log.error("error exporting the following record from HBase: " + value.toString(), e);
|
68
|
context.getCounter("error", e.getClass().getName()).increment(1);
|
69
|
throw new RuntimeException(e);
|
70
|
}
|
71
|
}
|
72
|
|
73
|
private void emit(final byte[] rowKey, final byte[] cf, final byte[] q, final byte[] value, final Context context) throws IOException, InterruptedException {
|
74
|
|
75
|
keyOut.set(Joiner.on(SEPARATOR).join(new String(rowKey), new String(cf), new String(q)));
|
76
|
|
77
|
if ((value == null) || (value.length == 0)) {
|
78
|
valueOut.set("");
|
79
|
} else {
|
80
|
valueOut.set(new JsonFormat().printToString(OafDecoder.decode(value).getOaf()));
|
81
|
}
|
82
|
context.write(keyOut, valueOut);
|
83
|
}
|
84
|
|
85
|
}
|