Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.dataexport;
2

    
3
import com.google.gson.Gson;
4
import com.google.protobuf.InvalidProtocolBufferException;
5
import eu.dnetlib.data.mapreduce.util.DedupUtils;
6
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder;
7
import eu.dnetlib.data.mapreduce.util.UpdateMerger;
8
import eu.dnetlib.data.proto.OafProtos;
9
import eu.dnetlib.data.proto.TypeProtos;
10
import eu.dnetlib.dhp.schema.oaf.Oaf;
11
import eu.dnetlib.dhp.schema.oaf.Relation;
12
import org.apache.commons.lang.StringUtils;
13
import org.apache.hadoop.hbase.client.Result;
14
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
15
import org.apache.hadoop.hbase.mapreduce.TableMapper;
16
import org.apache.hadoop.hbase.util.Bytes;
17
import org.apache.hadoop.io.Text;
18
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
19

    
20
import java.io.IOException;
21
import java.util.Map;
22
import java.util.Map.Entry;
23
import java.util.NavigableMap;
24
import java.util.stream.Collectors;
25

    
26
/**
27
 * Exports Oaf objects as their json serialization.
28
 *
29
 * @author claudio
30
 *
31
 */
32
public class ExportInformationSpaceMapper2DHP extends TableMapper<Text, Text> {
33

    
34
	private Text keyOut;
35

    
36
	private Text valueOut;
37

    
38
	private MultipleOutputs multipleOutputs;
39

    
40
	private Gson g;
41

    
42
	@Override
43
	protected void setup(final Context context) throws IOException, InterruptedException {
44
		super.setup(context);
45

    
46
		keyOut = new Text();
47
		valueOut = new Text();
48
		multipleOutputs = new MultipleOutputs(context);
49
		g = new Gson();
50
	}
51

    
52
	@Override
53
	protected void cleanup(Context context) throws IOException, InterruptedException {
54
		multipleOutputs.close();
55
	}
56

    
57
	@Override
58
	protected void map(final ImmutableBytesWritable keyIn, final Result value, final Context context) throws IOException, InterruptedException {
59
		try {
60
			final OafRowKeyDecoder rkd = OafRowKeyDecoder.decode(keyIn.get());
61

    
62
			final OafProtos.Oaf oaf = UpdateMerger.mergeBodyUpdates(context, value.getFamilyMap(Bytes.toBytes(rkd.getType().toString())));
63

    
64
			if (oaf == null) {
65
				return;
66
			} else {
67
				emit(context, oaf);
68
			}
69

    
70
			final Map<byte[], NavigableMap<byte[], byte[]>> row = value.getNoVersionMap();
71
			
72
			for (byte[] cf : row.keySet()) {
73

    
74
				for (Entry<byte[], byte[]> q : value.getFamilyMap(cf).entrySet().stream().filter(e -> {
75
					final String key = new String(e.getKey());
76
					boolean skip = key.startsWith("update") || key.equals(DedupUtils.BODY_S);
77
					if (skip) {
78
						context.getCounter("export", String.format("skipped %s", StringUtils.substring(key, 0, 6))).increment(1);
79
					}
80
					return !skip;
81
				}).collect(Collectors.toList())) {
82
					if (new String(q.getValue()).equals("")) {
83
						context.getCounter("export", "skipped " + new String(cf)).increment(1);
84
					} else {
85
						emit(context, OafProtos.Oaf.parseFrom(q.getValue()));
86
					}
87
				}
88
			}
89
		} catch (final Throwable e) {
90
			context.getCounter("export", "error: " + e.getClass().getName()).increment(1);
91
			throw new RuntimeException(e);
92
		}
93
	}
94

    
95
	private void emit(Context context, OafProtos.Oaf oaf) throws IOException, InterruptedException {
96
		Oaf result = null;
97
		try {
98
			result = ProtoConverter.convert(oaf);
99
		} catch (Throwable e) {
100
			context.getCounter("export", "error:" + e.getClass().getName()).increment(1);
101
		}
102
		if (result != null) {
103
			keyOut.set(result.getClass().getName());
104
			valueOut.set(g.toJson(result));
105

    
106
			final String namedOutput = result.getClass().getSimpleName().toLowerCase();
107
			multipleOutputs.write(namedOutput, keyOut, valueOut, namedOutput + "/" + namedOutput);
108

    
109
			context.getCounter("export", result.getClass().getName()).increment(1);
110
		}
111
	}
112

    
113
}
(4-4/10)