Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.util.Map;
5

    
6
import eu.dnetlib.data.mapreduce.JobParams;
7
import eu.dnetlib.data.mapreduce.util.DedupUtils;
8
import eu.dnetlib.data.proto.DedupProtos.Dedup;
9
import eu.dnetlib.data.proto.KindProtos.Kind;
10
import eu.dnetlib.data.proto.OafProtos.Oaf;
11
import eu.dnetlib.data.proto.OafProtos.OafRel.Builder;
12
import eu.dnetlib.data.proto.TypeProtos.Type;
13
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
14
import eu.dnetlib.pace.config.DedupConfig;
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.client.Result;
20
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
21
import org.apache.hadoop.hbase.mapreduce.TableMapper;
22
import org.apache.hadoop.hbase.util.Bytes;
23

    
24
public class DedupMarkDeletedEntityMapper extends TableMapper<ImmutableBytesWritable, Put> {
25

    
26
	private static final Log log = LogFactory.getLog(DedupMarkDeletedEntityMapper.class);
27

    
28
	private DedupConfig dedupConf;
29

    
30
	@Override
31
	protected void setup(final Context context) throws IOException, InterruptedException {
32
		dedupConf = DedupConfig.load(context.getConfiguration().get(JobParams.DEDUP_CONF));
33
		log.info("dedup findRoots mapper\nwf conf: " + dedupConf.toString());
34
	}
35

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

    
40
		final Type type = Type.valueOf(dedupConf.getWf().getEntityType());
41
		final Map<byte[], byte[]> mergedIn = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(type));
42

    
43
		if ((mergedIn != null) && !mergedIn.isEmpty()) {
44

    
45
			final byte[] row = rowkey.copyBytes();
46

    
47
			// marks the original body deleted
48
			emitBody(context, row, value.getValue(Bytes.toBytes(type.toString()), DedupUtils.BODY_B));
49

    
50
		} else {
51
			context.getCounter(type.toString(), "row not merged").increment(1);
52
		}
53
	}
54

    
55

    
56
	private void emitBody(final Context context, final byte[] row, final byte[] body) throws IOException, InterruptedException {
57
		final String type = dedupConf.getWf().getEntityType();
58
		if (body == null) {
59
			context.getCounter(type, "missing body").increment(1);
60
			System.err.println("missing body: " + new String(row));
61
			return;
62
		}
63
		final Oaf prototype = Oaf.parseFrom(body);
64

    
65
		if (prototype.getDataInfo().getDeletedbyinference()) {
66
			context.getCounter(type, "bodies already deleted").increment(1);
67
		} else {
68
			final Oaf.Builder oafRoot = Oaf.newBuilder(prototype);
69
			oafRoot.getDataInfoBuilder().setDeletedbyinference(true).setInferred(true).setInferenceprovenance(dedupConf.getWf().getConfigurationId());
70
			final byte[] family = Bytes.toBytes(type);
71
			final Put put = new Put(row).add(family, DedupUtils.BODY_B, oafRoot.build().toByteArray());
72
			put.setDurability(Durability.USE_DEFAULT);
73
			context.write(new ImmutableBytesWritable(row), put);
74
			context.getCounter(type, "bodies marked deleted").increment(1);
75
		}
76
	}
77

    
78
	private byte[] buildRel(final byte[] from, final byte[] to, final Dedup.RelName relClass) {
79
		final Builder oafRel = DedupUtils.getDedup(dedupConf, new String(from), new String(to), relClass);
80
		final Oaf oaf =
81
				Oaf.newBuilder()
82
						.setKind(Kind.relation)
83
						.setLastupdatetimestamp(System.currentTimeMillis())
84
						.setDataInfo(
85
								AbstractDNetXsltFunctions.getDataInfo(null, "", "0.8", false, true).setInferenceprovenance(
86
										dedupConf.getWf().getConfigurationId())).setRel(oafRel)
87
				.build();
88
		return oaf.toByteArray();
89
	}
90

    
91
}
(8-8/16)