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.OafEntity;
14
import eu.dnetlib.dhp.schema.oaf.Relation;
15
import org.apache.commons.lang.StringUtils;
16
import org.apache.hadoop.hbase.client.Result;
17
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
18
import org.apache.hadoop.hbase.mapreduce.TableMapper;
19
import org.apache.hadoop.hbase.util.Bytes;
20
import org.apache.hadoop.io.Text;
21
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
22

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

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

    
37
	private Text keyOut;
38

    
39
	private Text valueOut;
40

    
41
	private MultipleOutputs multipleOutputs;
42

    
43
	private ObjectMapper objectMapper;
44

    
45
	@Override
46
	protected void setup(final Context context) throws IOException, InterruptedException {
47
		super.setup(context);
48

    
49
		keyOut = new Text();
50
		valueOut = new Text();
51
		multipleOutputs = new MultipleOutputs(context);
52
		objectMapper = new ObjectMapper()
53
				.setSerializationInclusion(JsonInclude.Include.NON_NULL);
54
	}
55

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

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

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

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

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

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

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

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

    
114
			boolean deleted = result.getDataInfo().getDeletedbyinference();
115
		
116
			if (result instanceof Relation) {
117
				Relation r = (Relation) result;
118
				String reltype = r.getRelType() + "_" + r.getSubRelType() + "_" + r.getRelClass();
119
				context.getCounter("export", String.format("%s deleted:%s", reltype, deleted)).increment(1);
120
			} else if (result instanceof OafEntity) {
121

    
122
				context.getCounter("export", String.format("%s deleted:%s", type, deleted)).increment(1);
123
			}
124
		}
125

    
126
	}
127

    
128
}
(4-4/10)