Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.nio.ByteBuffer;
5
import java.util.Map;
6

    
7
import com.google.protobuf.InvalidProtocolBufferException;
8
import eu.dnetlib.data.mapreduce.JobParams;
9
import eu.dnetlib.data.mapreduce.util.DedupUtils;
10
import eu.dnetlib.data.proto.DedupProtos.Dedup;
11
import eu.dnetlib.data.proto.KindProtos.Kind;
12
import eu.dnetlib.data.proto.OafProtos.Oaf;
13
import eu.dnetlib.data.proto.OafProtos.OafRel.Builder;
14
import eu.dnetlib.data.proto.TypeProtos.Type;
15
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
16
import eu.dnetlib.pace.config.DedupConfig;
17
import org.apache.hadoop.hbase.client.Put;
18
import org.apache.hadoop.hbase.client.Result;
19
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
20
import org.apache.hadoop.hbase.mapreduce.TableMapper;
21
import org.apache.hadoop.hbase.util.Bytes;
22

    
23
public class DedupFindRootsMapper extends TableMapper<ImmutableBytesWritable, Put> {
24

    
25
	private DedupConfig dedupConf;
26

    
27
	@Override
28
	protected void setup(final Context context) throws IOException, InterruptedException {
29
		dedupConf = DedupConfig.load(context.getConfiguration().get(JobParams.DEDUP_CONF));
30
		System.out.println("dedup findRoots mapper\nwf conf: " + dedupConf.toString());
31
	}
32

    
33
	@Override
34
	protected void map(final ImmutableBytesWritable rowkey, final Result value, final Context context) throws IOException, InterruptedException {
35
		// System.out.println("Find root mapping: " + new String(rowkey.copyBytes()));
36

    
37
		final Type type = Type.valueOf(dedupConf.getWf().getEntityType());
38
		final Map<byte[], byte[]> similarRels = value.getFamilyMap(DedupUtils.getSimilarityCFBytes(type));
39

    
40
		if ((similarRels != null) && !similarRels.isEmpty()) {
41
			final ByteBuffer min = findMin(ByteBuffer.wrap(rowkey.get()), similarRels.keySet());
42

    
43
			final byte[] row = rowkey.copyBytes();
44
			final byte[] root = DedupUtils.newIdBytes(min, dedupConf.getWf().getDedupRun());
45

    
46
			// System.out.println("Found root: " + new String(root));
47

    
48
			emitDedupRel(context, DedupUtils.getDedupCF_mergedInBytes(type), row, root, buildRel(row, root, Dedup.RelName.isMergedIn));
49
			emitDedupRel(context, DedupUtils.getDedupCF_mergesBytes(type), root, row, buildRel(root, row, Dedup.RelName.merges));
50

    
51
			context.getCounter(dedupConf.getWf().getEntityType(), "dedupRel (x2)").increment(1);
52

    
53
			// marks the original body deleted
54
			emitBody(context, row, value.getValue(Bytes.toBytes(dedupConf.getWf().getEntityType()), DedupUtils.BODY_B));
55

    
56
		} else {
57
			context.getCounter(dedupConf.getWf().getEntityType(), "row not in similarity mesh").increment(1);
58
		}
59
	}
60

    
61
	private ByteBuffer findMin(ByteBuffer min, final Iterable<byte[]> keys) {
62
		for (final byte[] q : keys) {
63
			final ByteBuffer iq = ByteBuffer.wrap(q);
64
			if (min.compareTo(iq) > 0) {
65
				min = iq;
66
			}
67
		}
68
		return min;
69
	}
70

    
71
	private void emitBody(final Context context, final byte[] row, final byte[] body) throws InvalidProtocolBufferException, IOException, InterruptedException {
72
		if (body == null) {
73
			context.getCounter(dedupConf.getWf().getEntityType(), "missing body").increment(1);
74
			System.err.println("missing body: " + new String(row));
75
			return;
76
		}
77
		final Oaf prototype = Oaf.parseFrom(body);
78

    
79
		if (prototype.getDataInfo().getDeletedbyinference()) {
80
			context.getCounter(dedupConf.getWf().getEntityType(), "bodies already deleted").increment(1);
81
		} else {
82
			final Oaf.Builder oafRoot = Oaf.newBuilder(prototype);
83
			oafRoot.getDataInfoBuilder().setDeletedbyinference(true).setInferred(true).setInferenceprovenance(dedupConf.getWf().getConfigurationId());
84
			final byte[] family = Bytes.toBytes(dedupConf.getWf().getEntityType());
85
			final Put put = new Put(row).add(family, DedupUtils.BODY_B, oafRoot.build().toByteArray());
86
			put.setWriteToWAL(JobParams.WRITE_TO_WAL);
87
			context.write(new ImmutableBytesWritable(row), put);
88
			context.getCounter(dedupConf.getWf().getEntityType(), "bodies marked deleted").increment(1);
89
		}
90
	}
91

    
92
	private byte[] buildRel(final byte[] from, final byte[] to, final Dedup.RelName relClass) {
93
		final Builder oafRel = DedupUtils.getDedup(dedupConf, new String(from), new String(to), relClass);
94
		final Oaf oaf =
95
				Oaf.newBuilder()
96
						.setKind(Kind.relation)
97
						.setLastupdatetimestamp(System.currentTimeMillis())
98
						.setDataInfo(
99
								AbstractDNetXsltFunctions.getDataInfo(null, "", "0.8", false, true).setInferenceprovenance(
100
										dedupConf.getWf().getConfigurationId())).setRel(oafRel)
101
				.build();
102
		return oaf.toByteArray();
103
	}
104

    
105
	private void emitDedupRel(final Context context, final byte[] cf, final byte[] from, final byte[] to, final byte[] value) throws IOException,
106
			InterruptedException {
107
		final Put put = new Put(from).add(cf, to, value);
108
		put.setWriteToWAL(JobParams.WRITE_TO_WAL);
109
		context.write(new ImmutableBytesWritable(from), put);
110
	}
111

    
112
}
(5-5/16)