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.KeyValue;
11
import eu.dnetlib.data.proto.FieldTypeProtos.Qualifier;
12
import eu.dnetlib.data.proto.FieldTypeProtos.StringField;
13
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
14
import eu.dnetlib.data.proto.PersonProtos.Person;
15
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
16

    
17
import com.google.common.base.Function;
18
import com.google.protobuf.InvalidProtocolBufferException;
19

    
20
import eu.dnetlib.data.proto.OafProtos.Oaf;
21
import eu.dnetlib.data.transform.OafUtils;
22
import org.apache.hadoop.mapreduce.Mapper;
23
import org.apache.hadoop.mapreduce.Reducer;
24

    
25
public class OafHbaseUtils extends OafUtils {
26

    
27
	public static OafDecoder decode(final ImmutableBytesWritable oaf) {
28
		return new OafDecoder(oaf.copyBytes());
29
	}
30

    
31
	public static Function<ImmutableBytesWritable, OafDecoder> decoder() {
32
		return new Function<ImmutableBytesWritable, OafDecoder>() {
33

    
34
			@Override
35
			public OafDecoder apply(final ImmutableBytesWritable input) {
36
				return OafDecoder.decode(input.copyBytes());
37
			}
38
		};
39
	}
40

    
41
	public static Iterable<Oaf> asOaf(final Iterable<ImmutableBytesWritable> in) {
42
		return Iterables.transform(in, oafDecoder());
43
	}
44

    
45
	public static Function<ImmutableBytesWritable, Oaf> oafDecoder() {
46
		return new Function<ImmutableBytesWritable, Oaf>() {
47

    
48
			@Override
49
			public Oaf apply(final ImmutableBytesWritable input) {
50
				return parse(input);
51
			}
52
		};
53
	}
54

    
55
	public static Oaf parse(final ImmutableBytesWritable input) {
56
		try {
57
			return Oaf.parseFrom(input.copyBytes());
58
		} catch (final InvalidProtocolBufferException e) {
59
			throw new IllegalArgumentException(e);
60
		}
61
	}
62

    
63
	public static <T> String getValue(T t) {
64
		return mapValue(t);
65
	}
66

    
67
	public static <T> String getKey(T t) {
68
		return mapKey(t);
69
	}
70

    
71
	public static <T> String getValue(Iterable<T> ts) {
72
		return Iterables.getFirst(listValues(ts), "");
73
	}
74

    
75
	public static <T> Set<String> hashSetValues(Iterable<T> ts) {
76
		return Sets.newHashSet(Iterables.transform(ts, new Function<T, String>() {
77
			@Override
78
			public String apply(final T t) {
79
				return mapValue(t);
80
			}
81
		}));
82
	}
83

    
84
	public static <T> List<String> listValues(Iterable<T> ts) {
85
		return Lists.newArrayList(Iterables.transform(ts, new Function<T, String>() {
86
			@Override
87
			public String apply(final T t) {
88
				return mapValue(t);
89
			}
90
		}));
91
	}
92

    
93
	public static <T> String getKey(Iterable<T> ts) {
94
		return Iterables.getFirst(listKeys(ts), "");
95
	}
96

    
97
	public static <T> List<String> listKeys(Iterable<T> ts) {
98
		return Lists.newArrayList(Iterables.transform(ts, new Function<T, String>() {
99
			@Override
100
			public String apply(final T t) {
101
				return mapKey(t);
102
			}
103
		}));
104
	}
105

    
106
	public static <T> Set<String> hashSetKeys(Iterable<T> ts) {
107
		return Sets.newHashSet(Iterables.transform(ts, new Function<T, String>() {
108
			@Override
109
			public String apply(final T t) {
110
				return mapKey(t);
111
			}
112
		}));
113
	}
114

    
115
	private static <T> String mapKey(final T t) {
116
		if (t instanceof KeyValue) return ((KeyValue) t).getKey();
117
		if (t instanceof String) return (String) t;
118
		if (t instanceof Qualifier) return ((Qualifier) t).getClassid();
119

    
120
		throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
121
	}
122

    
123
	private static <T> String mapValue(final T t) {
124
		if (t instanceof StructuredProperty) return ((StructuredProperty) t).getValue();
125
		if (t instanceof KeyValue) return ((KeyValue) t).getValue();
126
		if (t instanceof String) return (String) t;
127
		if (t instanceof Person) return ((Person) t).getMetadata().getFullname().getValue();
128
		if (t instanceof StringField) return ((StringField) t).getValue();
129
		if (t instanceof Qualifier) return ((Qualifier) t).getClassname();
130

    
131
		throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
132
	}
133

    
134
	public static List<String> getPropertyValues(final Reducer.Context context, final String name) {
135
		return doGetPropertyValues(context.getConfiguration().get(name, ""));
136
	}
137

    
138
	public static List<String> getPropertyValues(final Mapper.Context context, final String name) {
139
		return doGetPropertyValues(context.getConfiguration().get(name, ""));
140
	}
141

    
142
	private static List<String> doGetPropertyValues(final String s) {
143
		return Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split(s));
144
	}
145

    
146
}
(3-3/8)