1 |
39290
|
claudio.at
|
package eu.dnetlib.data.mapreduce.hbase.dataimport;
|
2 |
|
|
|
3 |
|
|
import java.io.IOException;
|
4 |
|
|
|
5 |
|
|
import org.apache.commons.lang.StringUtils;
|
6 |
|
|
import org.apache.commons.logging.Log;
|
7 |
|
|
import org.apache.commons.logging.LogFactory;
|
8 |
|
|
import org.apache.hadoop.hbase.client.Put;
|
9 |
|
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
10 |
|
|
import org.apache.hadoop.hbase.util.Bytes;
|
11 |
|
|
import org.apache.hadoop.io.Text;
|
12 |
|
|
import org.apache.hadoop.mapreduce.Mapper;
|
13 |
|
|
|
14 |
|
|
import com.googlecode.protobuf.format.JsonFormat;
|
15 |
|
|
|
16 |
|
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
17 |
|
|
|
18 |
|
|
public class ImportInformationSpaceDumpMapper extends Mapper<Text, Text, ImmutableBytesWritable, Put> {
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* logger.
|
22 |
|
|
*/
|
23 |
|
|
private static final Log log = LogFactory.getLog(ImportInformationSpaceDumpMapper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
24 |
|
|
|
25 |
|
|
private static final String SEPARATOR = "@";
|
26 |
|
|
|
27 |
|
|
private static final boolean WRITE_TO_WAL = false;
|
28 |
|
|
|
29 |
|
|
private ImmutableBytesWritable ibw;
|
30 |
|
|
|
31 |
|
|
@Override
|
32 |
|
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
33 |
|
|
super.setup(context);
|
34 |
39297
|
claudio.at
|
ibw = new ImmutableBytesWritable();
|
35 |
39290
|
claudio.at
|
}
|
36 |
|
|
|
37 |
|
|
@Override
|
38 |
|
|
protected void map(final Text key, final Text value, final Context context) throws IOException, InterruptedException {
|
39 |
|
|
try {
|
40 |
|
|
|
41 |
|
|
String[] split = key.toString().split(SEPARATOR);
|
42 |
|
|
|
43 |
39297
|
claudio.at
|
if (split.length != 3)
|
44 |
|
|
throw new IllegalArgumentException("malformed key: " + key.toString());
|
45 |
|
|
|
46 |
39290
|
claudio.at
|
byte[] rowKey = Bytes.toBytes(split[0]);
|
47 |
|
|
byte[] columnFamily = Bytes.toBytes(split[1]);
|
48 |
|
|
byte[] qualifier = Bytes.toBytes(split[2]);
|
49 |
|
|
|
50 |
|
|
final Put put = new Put(rowKey);
|
51 |
|
|
put.setWriteToWAL(WRITE_TO_WAL);
|
52 |
|
|
ibw.set(rowKey);
|
53 |
|
|
|
54 |
|
|
if (StringUtils.isNotBlank(value.toString())) {
|
55 |
|
|
|
56 |
|
|
Oaf.Builder oaf = Oaf.newBuilder();
|
57 |
|
|
JsonFormat.merge(value.toString(), oaf);
|
58 |
|
|
|
59 |
|
|
put.add(columnFamily, qualifier, oaf.build().toByteArray());
|
60 |
|
|
} else {
|
61 |
|
|
put.add(columnFamily, qualifier, Bytes.toBytes(""));
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
context.write(ibw, put);
|
65 |
|
|
|
66 |
|
|
} catch (final Throwable e) {
|
67 |
39297
|
claudio.at
|
log.error(String.format("error importing the following record on HBase: \nkey: '%s'\nvalue: %s", key.toString(), value.toString()), e);
|
68 |
39290
|
claudio.at
|
context.getCounter("error", e.getClass().getName()).increment(1);
|
69 |
|
|
throw new RuntimeException(e);
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
}
|