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