Project

General

Profile

1
package eu.dnetlib.data.mapreduce.util;
2

    
3
import java.util.List;
4
import java.util.Set;
5

    
6
import com.google.common.base.Splitter;
7
import com.google.common.collect.Iterables;
8
import com.google.common.collect.Lists;
9
import com.google.common.collect.Sets;
10
import eu.dnetlib.data.proto.FieldTypeProtos.*;
11
import eu.dnetlib.data.proto.PersonProtos.Person;
12
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
13

    
14
import com.google.common.base.Function;
15
import com.google.protobuf.InvalidProtocolBufferException;
16

    
17
import eu.dnetlib.data.proto.OafProtos.Oaf;
18
import eu.dnetlib.data.transform.OafUtils;
19
import org.apache.hadoop.mapreduce.Mapper;
20
import org.apache.hadoop.mapreduce.Reducer;
21

    
22
public class OafHbaseUtils extends OafUtils {
23

    
24
	public static OafDecoder decode(final ImmutableBytesWritable oaf) {
25
		return new OafDecoder(oaf.copyBytes());
26
	}
27

    
28
	public static Function<ImmutableBytesWritable, OafDecoder> decoder() {
29
		return input -> OafDecoder.decode(input.copyBytes());
30
	}
31

    
32
	public static Iterable<Oaf> asOaf(final Iterable<ImmutableBytesWritable> in) {
33
		return Iterables.transform(in, oafDecoder());
34
	}
35

    
36
	public static Function<ImmutableBytesWritable, Oaf> oafDecoder() {
37
		return input -> parse(input);
38
	}
39

    
40
	public static Oaf parse(final ImmutableBytesWritable input) {
41
		try {
42
			return Oaf.parseFrom(input.copyBytes());
43
		} catch (final InvalidProtocolBufferException e) {
44
			throw new IllegalArgumentException(e);
45
		}
46
	}
47

    
48
	public static <T> String getValue(T t) {
49
		return mapValue(t);
50
	}
51

    
52
	public static <T> String getKey(T t) {
53
		return mapKey(t);
54
	}
55

    
56
	public static <T> String getValue(Iterable<T> ts) {
57
		return Iterables.getFirst(listValues(ts), "");
58
	}
59

    
60
	public static <T> Set<String> hashSetValues(Iterable<T> ts) {
61
		return Sets.newHashSet(Iterables.transform(ts, t -> mapValue(t)));
62
	}
63

    
64
	public static <T> List<String> listValues(Iterable<T> ts) {
65
		return Lists.newArrayList(Iterables.transform(ts, t -> mapValue(t)));
66
	}
67

    
68
	public static <T> String getKey(Iterable<T> ts) {
69
		return Iterables.getFirst(listKeys(ts), "");
70
	}
71

    
72
	public static <T> List<String> listKeys(Iterable<T> ts) {
73
		return Lists.newArrayList(Iterables.transform(ts, t -> mapKey(t)));
74
	}
75

    
76
	public static <T> Set<String> hashSetKeys(Iterable<T> ts) {
77
		return Sets.newHashSet(Iterables.transform(ts, t -> mapKey(t)));
78
	}
79

    
80
	private static <T> String mapKey(final T t) {
81
		if (t instanceof KeyValue) return ((KeyValue) t).getKey();
82
		if (t instanceof String) return (String) t;
83
		if (t instanceof Qualifier) return ((Qualifier) t).getClassid();
84

    
85
		throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
86
	}
87

    
88
	private static <T> String mapValue(final T t) {
89
		if (t instanceof StructuredProperty) return ((StructuredProperty) t).getValue();
90
		if (t instanceof KeyValue) return ((KeyValue) t).getValue();
91
		if (t instanceof String) return (String) t;
92
		if (t instanceof Person) return ((Person) t).getMetadata().getFullname().getValue();
93
		if (t instanceof StringField) return ((StringField) t).getValue();
94
		if (t instanceof Qualifier) return ((Qualifier) t).getClassname();
95
		if (t instanceof Author) return ((Author) t).getFullname();
96

    
97
		throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
98
	}
99

    
100
	public static List<String> getPropertyValues(final Reducer.Context context, final String name) {
101
		return doGetPropertyValues(context.getConfiguration().get(name, ""));
102
	}
103

    
104
	public static List<String> getPropertyValues(final Mapper.Context context, final String name) {
105
		return doGetPropertyValues(context.getConfiguration().get(name, ""));
106
	}
107

    
108
	private static List<String> doGetPropertyValues(final String s) {
109
		return Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split(s));
110
	}
111

    
112
}
(3-3/7)