1 |
26600
|
sandro.lab
|
package eu.dnetlib.data.mapreduce.hbase.dataimport;
|
2 |
|
|
|
3 |
|
|
import java.io.IOException;
|
4 |
38000
|
claudio.at
|
import java.lang.reflect.Type;
|
5 |
|
|
import java.util.Map;
|
6 |
26600
|
sandro.lab
|
|
7 |
35769
|
claudio.at
|
import org.apache.commons.logging.Log;
|
8 |
|
|
import org.apache.commons.logging.LogFactory;
|
9 |
26600
|
sandro.lab
|
import org.apache.hadoop.hbase.client.Put;
|
10 |
|
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
11 |
|
|
import org.apache.hadoop.hbase.util.Bytes;
|
12 |
|
|
import org.apache.hadoop.io.Text;
|
13 |
|
|
import org.apache.hadoop.mapreduce.Mapper;
|
14 |
|
|
|
15 |
38000
|
claudio.at
|
import com.google.common.collect.Maps;
|
16 |
|
|
import com.google.gson.Gson;
|
17 |
|
|
import com.google.gson.reflect.TypeToken;
|
18 |
|
|
|
19 |
26600
|
sandro.lab
|
import eu.dnetlib.data.mapreduce.JobParams;
|
20 |
|
|
import eu.dnetlib.data.transform.Column;
|
21 |
|
|
import eu.dnetlib.data.transform.Row;
|
22 |
|
|
import eu.dnetlib.data.transform.XsltRowTransformer;
|
23 |
|
|
import eu.dnetlib.data.transform.XsltRowTransformerFactory;
|
24 |
|
|
|
25 |
|
|
public class ImportRecordsMapper extends Mapper<Text, Text, ImmutableBytesWritable, Put> {
|
26 |
|
|
|
27 |
35769
|
claudio.at
|
/**
|
28 |
|
|
* logger.
|
29 |
|
|
*/
|
30 |
|
|
private static final Log log = LogFactory.getLog(ImportRecordsMapper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
31 |
|
|
|
32 |
26600
|
sandro.lab
|
private static final boolean WRITE_TO_WAL = false;
|
33 |
|
|
|
34 |
|
|
private XsltRowTransformer transformer;
|
35 |
|
|
|
36 |
|
|
private ImmutableBytesWritable ibw;
|
37 |
|
|
|
38 |
|
|
@Override
|
39 |
35769
|
claudio.at
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
40 |
26600
|
sandro.lab
|
super.setup(context);
|
41 |
|
|
|
42 |
38000
|
claudio.at
|
final String xslt = context.getConfiguration().get(JobParams.HBASE_IMPORT_XSLT).trim();
|
43 |
26600
|
sandro.lab
|
|
44 |
35769
|
claudio.at
|
if ((xslt == null) || xslt.isEmpty()) throw new IllegalArgumentException("missing xslt");
|
45 |
38000
|
claudio.at
|
final Map<String, Object> xslParams = Maps.newHashMap();
|
46 |
26600
|
sandro.lab
|
|
47 |
38000
|
claudio.at
|
if (context.getConfiguration().get("datasourceTypeMap") != null) {
|
48 |
|
|
final Map<String, String> dsMap = getDatasourceTypeMap(context);
|
49 |
|
|
|
50 |
|
|
log.info("using datasource type map:\n" + dsMap.toString());
|
51 |
|
|
|
52 |
|
|
xslParams.put("mergeIdForHomonymsMap", dsMap);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
transformer = XsltRowTransformerFactory.newInstance(xslt, xslParams);
|
56 |
|
|
|
57 |
26600
|
sandro.lab
|
ibw = new ImmutableBytesWritable();
|
58 |
|
|
|
59 |
37240
|
claudio.at
|
log.info("got xslt: '" + xslt);
|
60 |
35769
|
claudio.at
|
log.info("using trasformer: '" + transformer.getTransformerClassName() + "'");
|
61 |
26600
|
sandro.lab
|
}
|
62 |
|
|
|
63 |
|
|
@Override
|
64 |
35769
|
claudio.at
|
protected void map(final Text key, final Text value, final Context context) throws IOException, InterruptedException {
|
65 |
|
|
try {
|
66 |
|
|
for (final Row row : transformer.apply(value.toString())) {
|
67 |
26600
|
sandro.lab
|
|
68 |
35769
|
claudio.at
|
final byte[] rowKey = Bytes.toBytes(row.getKey());
|
69 |
|
|
final Put put = new Put(rowKey);
|
70 |
|
|
put.setWriteToWAL(WRITE_TO_WAL);
|
71 |
26600
|
sandro.lab
|
|
72 |
35769
|
claudio.at
|
for (final Column<String, byte[]> col : row) {
|
73 |
|
|
final byte[] family = Bytes.toBytes(row.getColumnFamily());
|
74 |
|
|
final byte[] qualifier = Bytes.toBytes(col.getName());
|
75 |
|
|
put.add(family, qualifier, col.getValue());
|
76 |
|
|
}
|
77 |
|
|
ibw.set(rowKey);
|
78 |
|
|
context.write(ibw, put);
|
79 |
|
|
context.getCounter("mdstore", row.getColumnFamily()).increment(row.getColumns().size());
|
80 |
26600
|
sandro.lab
|
}
|
81 |
35769
|
claudio.at
|
} catch (final Throwable e) {
|
82 |
|
|
log.error("error importing the following record on HBase: " + value.toString(), e);
|
83 |
|
|
context.getCounter("error", e.getClass().getName()).increment(1);
|
84 |
|
|
throw new RuntimeException(e);
|
85 |
26600
|
sandro.lab
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
38000
|
claudio.at
|
private Map<String, String> getDatasourceTypeMap(final Context context) {
|
89 |
|
|
final String dsType = context.getConfiguration().get("datasourceTypeMap");
|
90 |
|
|
|
91 |
|
|
final Type token = new TypeToken<Map<String, String>>() {}.getType();
|
92 |
|
|
final Map<String, String> dsMap = new Gson().fromJson(dsType, token);
|
93 |
|
|
return dsMap;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
26600
|
sandro.lab
|
}
|