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 org.apache.hadoop.hbase.io.ImmutableBytesWritable;
12

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

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

    
21
public class OafHbaseUtils extends OafUtils {
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
110
}
(3-3/7)