Project

General

Profile

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

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

    
7
import org.apache.hadoop.hbase.client.Result;
8
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
9
import org.apache.hadoop.hbase.mapreduce.TableMapper;
10
import org.apache.hadoop.hbase.util.Bytes;
11
import org.apache.hadoop.io.Text;
12

    
13
import com.google.common.collect.Iterables;
14
import com.google.protobuf.InvalidProtocolBufferException;
15

    
16
import eu.dnetlib.data.mapreduce.util.DedupUtils;
17
import eu.dnetlib.data.mapreduce.util.OafDecoder;
18
import eu.dnetlib.data.mapreduce.util.UpdateMerger;
19
import eu.dnetlib.data.proto.KindProtos.Kind;
20
import eu.dnetlib.data.proto.OafProtos.Oaf;
21
import eu.dnetlib.data.proto.OafProtos.OafEntity;
22
import eu.dnetlib.data.proto.RelTypeProtos.RelType;
23
import eu.dnetlib.data.proto.TypeProtos.Type;
24
import eu.dnetlib.data.transform.OafUtils;
25
import eu.dnetlib.pace.util.DedupConfig;
26
import eu.dnetlib.pace.util.DedupConfigLoader;
27

    
28
public class DedupBuildRootsMapper extends TableMapper<Text, ImmutableBytesWritable> {
29

    
30
	private DedupConfig dedupConf;
31

    
32
	private ImmutableBytesWritable ibw;
33

    
34
	private Set<String> entityNames;
35

    
36
	@Override
37
	protected void setup(final Context context) {
38
		dedupConf = DedupConfigLoader.load(context.getConfiguration().get("dedup.wf.conf"));
39
		System.out.println("dedup buildRoots mapper\nwf conf: " + dedupConf.toString());
40

    
41
		entityNames = OafUtils.entities();
42
		ibw = new ImmutableBytesWritable();
43
	}
44

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

    
49
		if (!DedupUtils.isRoot(rowkey)) {
50

    
51
			// TODO: remove this hack - here because we don't want to dedup datasets
52
			if (checkDataset(value)) return;
53

    
54
			final Map<byte[], byte[]> dedupRels = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(Type.valueOf(dedupConf.getEntityType())));
55

    
56
			if ((dedupRels != null) && !dedupRels.isEmpty()) {
57

    
58
				final Text rootId = findRoot(dedupRels);
59
				// byte[] rootIdBytes = rootId.copyBytes();
60
				// byte[] rowkeyBytes = rowkey.copyBytes();
61

    
62
				context.getCounter(dedupConf.getEntityType(), "merged").increment(1);
63
				for (final String family : dedupConf.getRootBuilderFamilies()) {
64

    
65
					// if (checkHack(rowkeyBytes, rootIdBytes, family)) {
66
					// context.getCounter("hack", "personResult skipped").increment(1);
67
					// } else {
68

    
69
					final Map<byte[], byte[]> map = value.getFamilyMap(Bytes.toBytes(family));
70
					if ((map != null) && !map.isEmpty()) {
71

    
72
						// if we're dealing with the entity CF
73
						if (entityNames.contains(family)) {
74
							final Oaf body = UpdateMerger.mergeBodyUpdates(context, map);
75

    
76
							emit(context, rootId, body.toByteArray());
77
						} else {
78
							for (final byte[] o : map.values()) {
79

    
80
								if (!isRelMarkedDeleted(context, o)) {
81
									emit(context, rootId, o);
82
								} else {
83
									context.getCounter(family, "rel marked deleted").increment(1);
84
								}
85
							}
86
						}
87
					} // else {
88
					// System.err.println("empty family: " + family + "\nkey: " + sKey);
89
					// context.getCounter("DedupBuildRootsMapper", "empty family '" + family + "'").increment(1);
90
					// }
91
					// }
92
				}
93
			} else {
94
				context.getCounter(dedupConf.getEntityType(), "not in duplicate group").increment(1);
95
			}
96
		} else {
97
			context.getCounter(dedupConf.getEntityType(), "root").increment(1);
98
		}
99
	}
100

    
101
	private boolean checkDataset(final Result value) {
102
		final Map<byte[], byte[]> bodyMap = value.getFamilyMap(dedupConf.getEntityNameBytes());
103

    
104
		if ((bodyMap == null) || bodyMap.isEmpty()) return true;
105

    
106
		final byte[] bodyB = bodyMap.get(DedupUtils.BODY_B);
107

    
108
		if (bodyB == null) return true;
109

    
110
		final OafEntity entity = OafDecoder.decode(bodyB).getEntity();
111

    
112
		if (entity.getType().equals(Type.result) && entity.getResult().getMetadata().getResulttype().getClassid().equals("dataset")) return true;
113

    
114
		return false;
115
	}
116

    
117
	private void emit(final Context context, final Text rootId, final byte[] value) throws IOException, InterruptedException {
118
		ibw.set(value);
119
		context.write(rootId, ibw);
120
	}
121

    
122
	private boolean checkHack(final byte[] rowkey, final byte[] rootId, final String family) {
123
		return dedupConf.getEntityType().equals(Type.result.toString()) && 	// we're deduplicating the results
124
				family.equals(RelType.personResult.toString()) && 			// we're dealing with a personResult relation
125
				!rootId.equals(DedupUtils.newIdBytes(new String(rowkey), dedupConf.getDedupRun()));
126
	}
127

    
128
	private boolean isRelMarkedDeleted(final Context context, final byte[] o) {
129
		try {
130
			final Oaf oaf = Oaf.parseFrom(o);
131
			return oaf.getKind().equals(Kind.relation) && oaf.getDataInfo().getDeletedbyinference();
132
		} catch (final InvalidProtocolBufferException e) {
133
			context.getCounter("error", e.getClass().getName()).increment(1);
134
			return true;
135
		}
136
	}
137

    
138
	private Text findRoot(final Map<byte[], byte[]> dedupRels) {
139
		return new Text(Iterables.getOnlyElement(dedupRels.keySet()));
140
	}
141
}
(2-2/23)