Project

General

Profile

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

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

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

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

    
36
	private Text keyOut;
37

    
38
	private Text valueOut;
39

    
40
	private MultipleOutputs multipleOutputs;
41

    
42
	private ObjectMapper objectMapper;
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
		multipleOutputs = new MultipleOutputs(context);
51
		objectMapper = new ObjectMapper()
52
				.setSerializationInclusion(JsonInclude.Include.NON_NULL);
53
	}
54

    
55
	@Override
56
	protected void cleanup(Context context) throws IOException, InterruptedException {
57
		multipleOutputs.close();
58
	}
59

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

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

    
67
			if (oaf == null) {
68
				return;
69
			} else {
70
				emit(context, oaf);
71
			}
72

    
73
			final Map<byte[], NavigableMap<byte[], byte[]>> row = value.getNoVersionMap();
74
			
75
			for (byte[] cf : row.keySet()) {
76

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

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

    
109
			final String namedOutput = result.getClass().getSimpleName().toLowerCase();
110
			multipleOutputs.write(namedOutput, keyOut, valueOut, namedOutput + "/" + namedOutput);
111

    
112
			context.getCounter("export", result.getClass().getName()).increment(1);
113
		}
114
	}
115

    
116
}
(4-4/10)