Project

General

Profile

1 26600 sandro.lab
package eu.dnetlib.data.mapreduce.hbase.dedup;
2
3
import java.io.IOException;
4
import java.util.Map;
5
6 36670 claudio.at
import eu.dnetlib.data.mapreduce.JobParams;
7 28094 claudio.at
import eu.dnetlib.data.mapreduce.util.DedupUtils;
8
import eu.dnetlib.data.proto.DedupProtos.Dedup;
9 26600 sandro.lab
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 28411 claudio.at
import eu.dnetlib.data.proto.TypeProtos.Type;
13 40205 claudio.at
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
14 36670 claudio.at
import eu.dnetlib.pace.config.DedupConfig;
15 39616 claudio.at
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17 57443 claudio.at
import org.apache.hadoop.hbase.client.Durability;
18 39616 claudio.at
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 26600 sandro.lab
24 39616 claudio.at
public class DedupMarkDeletedEntityMapper extends TableMapper<ImmutableBytesWritable, Put> {
25 26600 sandro.lab
26 39616 claudio.at
	private static final Log log = LogFactory.getLog(DedupMarkDeletedEntityMapper.class);
27
28 26600 sandro.lab
	private DedupConfig dedupConf;
29
30
	@Override
31 28308 claudio.at
	protected void setup(final Context context) throws IOException, InterruptedException {
32 36670 claudio.at
		dedupConf = DedupConfig.load(context.getConfiguration().get(JobParams.DEDUP_CONF));
33 39616 claudio.at
		log.info("dedup findRoots mapper\nwf conf: " + dedupConf.toString());
34 26600 sandro.lab
	}
35
36
	@Override
37 28308 claudio.at
	protected void map(final ImmutableBytesWritable rowkey, final Result value, final Context context) throws IOException, InterruptedException {
38 26600 sandro.lab
		// System.out.println("Find root mapping: " + new String(rowkey.copyBytes()));
39
40 36670 claudio.at
		final Type type = Type.valueOf(dedupConf.getWf().getEntityType());
41 39616 claudio.at
		final Map<byte[], byte[]> mergedIn = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(type));
42 26600 sandro.lab
43 39616 claudio.at
		if ((mergedIn != null) && !mergedIn.isEmpty()) {
44 26600 sandro.lab
45 36670 claudio.at
			final byte[] row = rowkey.copyBytes();
46 26600 sandro.lab
47
			// marks the original body deleted
48 39616 claudio.at
			emitBody(context, row, value.getValue(Bytes.toBytes(type.toString()), DedupUtils.BODY_B));
49 26600 sandro.lab
50
		} else {
51 39616 claudio.at
			context.getCounter(type.toString(), "row not merged").increment(1);
52 26600 sandro.lab
		}
53
	}
54
55
56 39616 claudio.at
	private void emitBody(final Context context, final byte[] row, final byte[] body) throws IOException, InterruptedException {
57
		final String type = dedupConf.getWf().getEntityType();
58 26600 sandro.lab
		if (body == null) {
59 39616 claudio.at
			context.getCounter(type, "missing body").increment(1);
60 26600 sandro.lab
			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 39616 claudio.at
			context.getCounter(type, "bodies already deleted").increment(1);
67 26600 sandro.lab
		} else {
68 36670 claudio.at
			final Oaf.Builder oafRoot = Oaf.newBuilder(prototype);
69 37824 claudio.at
			oafRoot.getDataInfoBuilder().setDeletedbyinference(true).setInferred(true).setInferenceprovenance(dedupConf.getWf().getConfigurationId());
70 39616 claudio.at
			final byte[] family = Bytes.toBytes(type);
71 36670 claudio.at
			final Put put = new Put(row).add(family, DedupUtils.BODY_B, oafRoot.build().toByteArray());
72 57443 claudio.at
			put.setDurability(Durability.USE_DEFAULT);
73 26600 sandro.lab
			context.write(new ImmutableBytesWritable(row), put);
74 39616 claudio.at
			context.getCounter(type, "bodies marked deleted").increment(1);
75 26600 sandro.lab
		}
76
	}
77
78 28308 claudio.at
	private byte[] buildRel(final byte[] from, final byte[] to, final Dedup.RelName relClass) {
79 36670 claudio.at
		final Builder oafRel = DedupUtils.getDedup(dedupConf, new String(from), new String(to), relClass);
80
		final Oaf oaf =
81 37824 claudio.at
				Oaf.newBuilder()
82
						.setKind(Kind.relation)
83 40314 claudio.at
						.setLastupdatetimestamp(System.currentTimeMillis())
84 37824 claudio.at
						.setDataInfo(
85 40205 claudio.at
								AbstractDNetXsltFunctions.getDataInfo(null, "", "0.8", false, true).setInferenceprovenance(
86 37824 claudio.at
										dedupConf.getWf().getConfigurationId())).setRel(oafRel)
87
				.build();
88 26600 sandro.lab
		return oaf.toByteArray();
89
	}
90
91
}