1 |
26600
|
sandro.lab
|
package eu.dnetlib.data.mapreduce.util;
|
2 |
|
|
|
3 |
43383
|
claudio.at
|
import java.util.List;
|
4 |
43443
|
claudio.at
|
import java.util.Set;
|
5 |
43383
|
claudio.at
|
|
6 |
|
|
import com.google.common.base.Splitter;
|
7 |
|
|
import com.google.common.collect.Iterables;
|
8 |
|
|
import com.google.common.collect.Lists;
|
9 |
43443
|
claudio.at
|
import com.google.common.collect.Sets;
|
10 |
49356
|
claudio.at
|
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 |
26600
|
sandro.lab
|
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 |
35127
|
claudio.at
|
import eu.dnetlib.data.transform.OafUtils;
|
22 |
43383
|
claudio.at
|
import org.apache.hadoop.mapreduce.Mapper;
|
23 |
|
|
import org.apache.hadoop.mapreduce.Reducer;
|
24 |
26600
|
sandro.lab
|
|
25 |
35127
|
claudio.at
|
public class OafHbaseUtils extends OafUtils {
|
26 |
26600
|
sandro.lab
|
|
27 |
|
|
public static OafDecoder decode(final ImmutableBytesWritable oaf) {
|
28 |
|
|
return new OafDecoder(oaf.copyBytes());
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
public static Function<ImmutableBytesWritable, OafDecoder> decoder() {
|
32 |
49356
|
claudio.at
|
return new Function<ImmutableBytesWritable, OafDecoder>() {
|
33 |
|
|
|
34 |
|
|
@Override
|
35 |
|
|
public OafDecoder apply(final ImmutableBytesWritable input) {
|
36 |
|
|
return OafDecoder.decode(input.copyBytes());
|
37 |
|
|
}
|
38 |
|
|
};
|
39 |
26600
|
sandro.lab
|
}
|
40 |
|
|
|
41 |
43383
|
claudio.at
|
public static Iterable<Oaf> asOaf(final Iterable<ImmutableBytesWritable> in) {
|
42 |
|
|
return Iterables.transform(in, oafDecoder());
|
43 |
|
|
}
|
44 |
|
|
|
45 |
26600
|
sandro.lab
|
public static Function<ImmutableBytesWritable, Oaf> oafDecoder() {
|
46 |
49356
|
claudio.at
|
return new Function<ImmutableBytesWritable, Oaf>() {
|
47 |
|
|
|
48 |
|
|
@Override
|
49 |
|
|
public Oaf apply(final ImmutableBytesWritable input) {
|
50 |
|
|
return parse(input);
|
51 |
|
|
}
|
52 |
|
|
};
|
53 |
26600
|
sandro.lab
|
}
|
54 |
|
|
|
55 |
35127
|
claudio.at
|
public static Oaf parse(final ImmutableBytesWritable input) {
|
56 |
26600
|
sandro.lab
|
try {
|
57 |
|
|
return Oaf.parseFrom(input.copyBytes());
|
58 |
35127
|
claudio.at
|
} catch (final InvalidProtocolBufferException e) {
|
59 |
26600
|
sandro.lab
|
throw new IllegalArgumentException(e);
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
43394
|
claudio.at
|
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 |
43383
|
claudio.at
|
public static <T> String getValue(Iterable<T> ts) {
|
72 |
43443
|
claudio.at
|
return Iterables.getFirst(listValues(ts), "");
|
73 |
43383
|
claudio.at
|
}
|
74 |
|
|
|
75 |
43443
|
claudio.at
|
public static <T> Set<String> hashSetValues(Iterable<T> ts) {
|
76 |
49356
|
claudio.at
|
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 |
43443
|
claudio.at
|
}
|
83 |
|
|
|
84 |
|
|
public static <T> List<String> listValues(Iterable<T> ts) {
|
85 |
49356
|
claudio.at
|
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 |
43383
|
claudio.at
|
}
|
92 |
|
|
|
93 |
|
|
public static <T> String getKey(Iterable<T> ts) {
|
94 |
43443
|
claudio.at
|
return Iterables.getFirst(listKeys(ts), "");
|
95 |
43383
|
claudio.at
|
}
|
96 |
|
|
|
97 |
43443
|
claudio.at
|
public static <T> List<String> listKeys(Iterable<T> ts) {
|
98 |
49356
|
claudio.at
|
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 |
43383
|
claudio.at
|
}
|
105 |
|
|
|
106 |
43443
|
claudio.at
|
public static <T> Set<String> hashSetKeys(Iterable<T> ts) {
|
107 |
49356
|
claudio.at
|
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 |
43443
|
claudio.at
|
}
|
114 |
|
|
|
115 |
43394
|
claudio.at
|
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 |
49356
|
claudio.at
|
if (t instanceof Person) return ((Person) t).getMetadata().getFullname().getValue();
|
128 |
43394
|
claudio.at
|
if (t instanceof StringField) return ((StringField) t).getValue();
|
129 |
43397
|
claudio.at
|
if (t instanceof Qualifier) return ((Qualifier) t).getClassname();
|
130 |
43394
|
claudio.at
|
|
131 |
|
|
throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
|
132 |
|
|
}
|
133 |
|
|
|
134 |
43383
|
claudio.at
|
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 |
26600
|
sandro.lab
|
}
|