Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.List;
7
import java.util.stream.Collectors;
8

    
9
import com.google.common.collect.Iterables;
10
import com.google.common.collect.Lists;
11
import eu.dnetlib.data.mapreduce.util.DedupUtils;
12
import eu.dnetlib.data.mapreduce.util.OafDecoder;
13
import eu.dnetlib.data.proto.OafProtos.OafEntity;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.apache.hadoop.hbase.client.Result;
17
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
18
import org.apache.hadoop.hbase.mapreduce.TableMapper;
19
import org.apache.hadoop.hbase.util.Bytes;
20
import org.apache.hadoop.io.Text;
21

    
22
/**
23
 * Exports the result identifiers as json.
24
 *
25
 * @author claudio
26
 */
27
public class ExportResultIdentifiersMapper extends TableMapper<Text, Text> {
28

    
29
	/**
30
	 * logger.
31
	 */
32
	private static final Log log = LogFactory.getLog(ExportResultIdentifiersMapper.class); // NOPMD by marko on 11/24/08 5:02 PM
33

    
34
	private static final String CF = "result";
35

    
36
	private Text keyOut;
37

    
38
	private Text valueOut;
39

    
40
	@Override
41
	protected void setup(final Context context) throws IOException, InterruptedException {
42
		super.setup(context);
43

    
44
		keyOut = new Text("");
45
		valueOut = new Text();
46
	}
47

    
48
	@Override
49
	protected void map(final ImmutableBytesWritable keyIn, final Result value, final Context context) throws IOException, InterruptedException {
50
		try {
51
			final byte[] body = value.getValue(Bytes.toBytes(CF), DedupUtils.BODY_B);
52

    
53
			if (body == null) {
54
				context.getCounter(CF, "missing body").increment(1);
55
				return;
56
			}
57

    
58
			final OpenaireEntityId id = new OpenaireEntityId();
59
			final OafDecoder d = OafDecoder.decode(body);
60

    
61
			id.setDeleted(d.getOaf().getDataInfo().getDeletedbyinference());
62
			id.setId(d.getEntityId());
63
			id.setPids(d.getOaf().getEntity().getPidList().stream().map(p -> p.getValue()).collect(Collectors.toList()));
64

    
65
			final List<OafEntity> childrenList = d.getEntity().getChildrenList();
66
			if (childrenList != null && !childrenList.isEmpty()) {
67
				final ArrayList<String> mergedIds = Lists.newArrayList(Iterables.transform(childrenList, oafEntity -> oafEntity.getId()));
68
				Collections.sort(mergedIds);
69
				id.setMergedIds(mergedIds);
70
			}
71

    
72
			valueOut.set(id.toString());
73
			context.write(keyOut, valueOut);
74

    
75
		} catch (final Throwable e) {
76
			log.error("error exporting the following record from HBase: " + value.toString(), e);
77
			context.getCounter("error", e.getClass().getName()).increment(1);
78
			throw new RuntimeException(e);
79
		}
80
	}
81

    
82
}
(2-2/4)