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.Collections;
6
import java.util.HashSet;
7
import java.util.Map;
8
import java.util.Set;
9

    
10
import com.google.common.base.Function;
11
import com.google.common.collect.Sets;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.apache.hadoop.hbase.client.Result;
15
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
16
import org.apache.hadoop.hbase.mapreduce.TableMapper;
17
import org.apache.hadoop.hbase.util.Bytes;
18
import org.apache.hadoop.io.Text;
19

    
20
import com.google.common.collect.Iterables;
21
import com.google.protobuf.InvalidProtocolBufferException;
22

    
23
import eu.dnetlib.data.mapreduce.JobParams;
24
import eu.dnetlib.data.mapreduce.util.DedupUtils;
25
import eu.dnetlib.data.mapreduce.util.OafDecoder;
26
import eu.dnetlib.data.mapreduce.util.UpdateMerger;
27
import eu.dnetlib.data.proto.KindProtos.Kind;
28
import eu.dnetlib.data.proto.OafProtos.Oaf;
29
import eu.dnetlib.data.proto.OafProtos.OafEntity;
30
import eu.dnetlib.data.proto.RelTypeProtos.RelType;
31
import eu.dnetlib.data.proto.TypeProtos.Type;
32
import eu.dnetlib.data.transform.OafUtils;
33
import eu.dnetlib.pace.config.DedupConfig;
34

    
35
public class DedupBuildRootsMapper extends TableMapper<Text, ImmutableBytesWritable> {
36

    
37
	private static final Log log = LogFactory.getLog(DedupBuildRootsMapper.class);
38

    
39
	private DedupConfig dedupConf;
40

    
41
	private ImmutableBytesWritable ibw;
42

    
43
	private Set<String> entityNames;
44

    
45
	@Override
46
	protected void setup(final Context context) {
47
		dedupConf = DedupConfig.load(context.getConfiguration().get(JobParams.DEDUP_CONF));
48
		System.out.println("dedup buildRoots mapper\nwf conf: " + dedupConf.toString());
49

    
50
		entityNames = OafUtils.entities();
51
		ibw = new ImmutableBytesWritable();
52
	}
53

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

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

    
61
		final Map<byte[], byte[]> mergedIn = value.getFamilyMap(DedupUtils.getDedupCF_mergedInBytes(Type.valueOf(dedupConf.getWf().getEntityType())));
62

    
63
		if ((mergedIn != null) && !mergedIn.isEmpty()) {
64

    
65
			final Set<String> ids = getStrings(mergedIn);
66

    
67
			if (ids.size() > 1) {
68
				context.getCounter(dedupConf.getWf().getEntityType(), "mergedIn > 1").increment(1);
69
			}
70
			final Text rootId = new Text(Collections.min(ids));
71

    
72
			// byte[] rootIdBytes = rootId.copyBytes();
73
			// byte[] rowkeyBytes = rowkey.copyBytes();
74

    
75
			context.getCounter(dedupConf.getWf().getEntityType(), "merged").increment(1);
76
			for (final String family : dedupConf.getWf().getRootBuilder()) {
77

    
78
				// if (checkHack(rowkeyBytes, rootIdBytes, family)) {
79
				// context.getCounter("hack", "personResult skipped").increment(1);
80
				// } else {
81

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

    
85
					// if we're dealing with the entity CF
86
					if (entityNames.contains(family)) {
87
						final Oaf body = UpdateMerger.mergeBodyUpdates(context, map);
88

    
89
						emit(context, rootId, body.toByteArray());
90
					} else {
91
						for (final byte[] o : map.values()) {
92

    
93
							if (!isRelMarkedDeleted(context, o)) {
94
								emit(context, rootId, o);
95
							} else {
96
								context.getCounter(family, "rel marked deleted").increment(1);
97
							}
98
						}
99
					}
100
				} // else {
101
				// System.err.println("empty family: " + family + "\nkey: " + sKey);
102
				// context.getCounter("DedupBuildRootsMapper", "empty family '" + family + "'").increment(1);
103
				// }
104
				// }
105
			}
106
		} else {
107
			//context.getCounter(dedupConf.getWf().getEntityType(), "not in duplicate group").increment(1);
108

    
109
			final Map<byte[], byte[]> mergesRels = value.getFamilyMap(DedupUtils.getDedupCF_mergesBytes(Type.valueOf(dedupConf.getWf().getEntityType())));
110
			if (mergesRels != null && !mergesRels.isEmpty()) {
111
				final byte[] body = value.getValue(dedupConf.getWf().getEntityType().getBytes(), DedupUtils.BODY_B);
112
				if (body != null) {
113

    
114
					context.getCounter(dedupConf.getWf().getEntityType(), "root").increment(1);
115
					emit(context, new Text(rowkey.copyBytes()), body);
116
				}
117
			}
118
		}
119

    
120
	}
121

    
122
	private HashSet<String> getStrings(final Map<byte[], byte[]> mergedIn) {
123
		return Sets.newHashSet(Iterables.transform(mergedIn.keySet(), new Function<byte[], String>() {
124

    
125
			@Override
126
			public String apply(final byte[] input) {
127
				return new String(input);
128
			}
129
		}));
130
	}
131

    
132
	private boolean checkDataset(final Result value) {
133
		final Map<byte[], byte[]> bodyMap = value.getFamilyMap(dedupConf.getWf().getEntityType().getBytes());
134

    
135
		if ((bodyMap == null) || bodyMap.isEmpty()) return true;
136

    
137
		final byte[] bodyB = bodyMap.get(DedupUtils.BODY_B);
138

    
139
		if (bodyB == null) return true;
140

    
141
		final OafEntity entity = OafDecoder.decode(bodyB).getEntity();
142

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

    
145
		return false;
146
	}
147

    
148
	private void emit(final Context context, final Text rootId, final byte[] value) throws IOException, InterruptedException {
149
		ibw.set(value);
150
		context.write(rootId, ibw);
151
	}
152

    
153
	private boolean checkHack(final byte[] rowkey, final byte[] rootId, final String family) {
154
		return dedupConf.getWf().getEntityType().equals(Type.result.toString()) && 	// we're deduplicating the results
155
				family.equals(RelType.personResult.toString()) && 			// we're dealing with a personResult relation
156
				!rootId.equals(DedupUtils.newIdBytes(new String(rowkey), dedupConf.getWf().getDedupRun()));
157
	}
158

    
159
	private boolean isRelMarkedDeleted(final Context context, final byte[] o) {
160
		try {
161
			final Oaf oaf = Oaf.parseFrom(o);
162
			return oaf.getKind().equals(Kind.relation) && oaf.getDataInfo().getDeletedbyinference();
163
		} catch (final InvalidProtocolBufferException e) {
164
			context.getCounter("error", e.getClass().getName()).increment(1);
165
			return true;
166
		}
167
	}
168

    
169
}
(1-1/22)