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.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 DedupMarkDeletedEntityMapper extends TableMapper<ImmutableBytesWritable, Put> {
24

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

    
27
	private DedupConfig dedupConf;
28

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

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

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

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

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

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

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

    
54

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

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

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

    
90
}
(8-8/16)