1 |
26600
|
sandro.lab
|
package eu.dnetlib.data.mapreduce.util;
|
2 |
|
|
|
3 |
54164
|
claudio.at
|
import java.util.*;
|
4 |
|
|
import java.util.function.Predicate;
|
5 |
|
|
import java.util.stream.Collectors;
|
6 |
|
|
import java.util.stream.Stream;
|
7 |
43383
|
claudio.at
|
|
8 |
|
|
import com.google.common.base.Splitter;
|
9 |
|
|
import com.google.common.collect.Iterables;
|
10 |
|
|
import com.google.common.collect.Lists;
|
11 |
43443
|
claudio.at
|
import com.google.common.collect.Sets;
|
12 |
49029
|
claudio.at
|
import eu.dnetlib.data.proto.FieldTypeProtos.*;
|
13 |
54164
|
claudio.at
|
import static eu.dnetlib.openaire.hadoop.utils.HBaseTableUtils.*;
|
14 |
|
|
|
15 |
|
|
import eu.dnetlib.data.proto.OafProtos;
|
16 |
|
|
import eu.dnetlib.data.proto.TypeProtos;
|
17 |
|
|
import eu.dnetlib.openaire.hadoop.utils.HBaseTableUtils;
|
18 |
|
|
import org.apache.commons.lang3.StringUtils;
|
19 |
|
|
import org.apache.hadoop.hbase.client.Durability;
|
20 |
|
|
import org.apache.hadoop.hbase.client.Put;
|
21 |
|
|
import org.apache.hadoop.hbase.client.Result;
|
22 |
26600
|
sandro.lab
|
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
23 |
|
|
|
24 |
|
|
import com.google.common.base.Function;
|
25 |
|
|
import com.google.protobuf.InvalidProtocolBufferException;
|
26 |
|
|
|
27 |
|
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
28 |
35127
|
claudio.at
|
import eu.dnetlib.data.transform.OafUtils;
|
29 |
54164
|
claudio.at
|
import org.apache.hadoop.hbase.util.Bytes;
|
30 |
43383
|
claudio.at
|
import org.apache.hadoop.mapreduce.Mapper;
|
31 |
|
|
import org.apache.hadoop.mapreduce.Reducer;
|
32 |
26600
|
sandro.lab
|
|
33 |
54164
|
claudio.at
|
|
34 |
|
|
|
35 |
35127
|
claudio.at
|
public class OafHbaseUtils extends OafUtils {
|
36 |
26600
|
sandro.lab
|
|
37 |
|
|
public static OafDecoder decode(final ImmutableBytesWritable oaf) {
|
38 |
|
|
return new OafDecoder(oaf.copyBytes());
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
public static Function<ImmutableBytesWritable, OafDecoder> decoder() {
|
42 |
49029
|
claudio.at
|
return input -> OafDecoder.decode(input.copyBytes());
|
43 |
26600
|
sandro.lab
|
}
|
44 |
|
|
|
45 |
43383
|
claudio.at
|
public static Iterable<Oaf> asOaf(final Iterable<ImmutableBytesWritable> in) {
|
46 |
|
|
return Iterables.transform(in, oafDecoder());
|
47 |
|
|
}
|
48 |
|
|
|
49 |
26600
|
sandro.lab
|
public static Function<ImmutableBytesWritable, Oaf> oafDecoder() {
|
50 |
49029
|
claudio.at
|
return input -> parse(input);
|
51 |
26600
|
sandro.lab
|
}
|
52 |
|
|
|
53 |
35127
|
claudio.at
|
public static Oaf parse(final ImmutableBytesWritable input) {
|
54 |
26600
|
sandro.lab
|
try {
|
55 |
|
|
return Oaf.parseFrom(input.copyBytes());
|
56 |
35127
|
claudio.at
|
} catch (final InvalidProtocolBufferException e) {
|
57 |
26600
|
sandro.lab
|
throw new IllegalArgumentException(e);
|
58 |
|
|
}
|
59 |
|
|
}
|
60 |
|
|
|
61 |
43394
|
claudio.at
|
public static <T> String getValue(T t) {
|
62 |
|
|
return mapValue(t);
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
public static <T> String getKey(T t) {
|
66 |
|
|
return mapKey(t);
|
67 |
|
|
}
|
68 |
|
|
|
69 |
43383
|
claudio.at
|
public static <T> String getValue(Iterable<T> ts) {
|
70 |
43443
|
claudio.at
|
return Iterables.getFirst(listValues(ts), "");
|
71 |
43383
|
claudio.at
|
}
|
72 |
|
|
|
73 |
43443
|
claudio.at
|
public static <T> Set<String> hashSetValues(Iterable<T> ts) {
|
74 |
49029
|
claudio.at
|
return Sets.newHashSet(Iterables.transform(ts, t -> mapValue(t)));
|
75 |
43443
|
claudio.at
|
}
|
76 |
|
|
|
77 |
|
|
public static <T> List<String> listValues(Iterable<T> ts) {
|
78 |
49029
|
claudio.at
|
return Lists.newArrayList(Iterables.transform(ts, t -> mapValue(t)));
|
79 |
43383
|
claudio.at
|
}
|
80 |
|
|
|
81 |
|
|
public static <T> String getKey(Iterable<T> ts) {
|
82 |
43443
|
claudio.at
|
return Iterables.getFirst(listKeys(ts), "");
|
83 |
43383
|
claudio.at
|
}
|
84 |
|
|
|
85 |
43443
|
claudio.at
|
public static <T> List<String> listKeys(Iterable<T> ts) {
|
86 |
49029
|
claudio.at
|
return Lists.newArrayList(Iterables.transform(ts, t -> mapKey(t)));
|
87 |
43383
|
claudio.at
|
}
|
88 |
|
|
|
89 |
43443
|
claudio.at
|
public static <T> Set<String> hashSetKeys(Iterable<T> ts) {
|
90 |
49029
|
claudio.at
|
return Sets.newHashSet(Iterables.transform(ts, t -> mapKey(t)));
|
91 |
43443
|
claudio.at
|
}
|
92 |
|
|
|
93 |
43394
|
claudio.at
|
private static <T> String mapKey(final T t) {
|
94 |
|
|
if (t instanceof KeyValue) return ((KeyValue) t).getKey();
|
95 |
|
|
if (t instanceof String) return (String) t;
|
96 |
|
|
if (t instanceof Qualifier) return ((Qualifier) t).getClassid();
|
97 |
|
|
|
98 |
|
|
throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
private static <T> String mapValue(final T t) {
|
102 |
|
|
if (t instanceof StructuredProperty) return ((StructuredProperty) t).getValue();
|
103 |
|
|
if (t instanceof KeyValue) return ((KeyValue) t).getValue();
|
104 |
|
|
if (t instanceof String) return (String) t;
|
105 |
|
|
if (t instanceof StringField) return ((StringField) t).getValue();
|
106 |
43397
|
claudio.at
|
if (t instanceof Qualifier) return ((Qualifier) t).getClassname();
|
107 |
49029
|
claudio.at
|
if (t instanceof Author) return ((Author) t).getFullname();
|
108 |
43394
|
claudio.at
|
|
109 |
|
|
throw new IllegalArgumentException(String.format("type %s not mapped", t.getClass()));
|
110 |
|
|
}
|
111 |
|
|
|
112 |
43383
|
claudio.at
|
public static List<String> getPropertyValues(final Reducer.Context context, final String name) {
|
113 |
|
|
return doGetPropertyValues(context.getConfiguration().get(name, ""));
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
public static List<String> getPropertyValues(final Mapper.Context context, final String name) {
|
117 |
|
|
return doGetPropertyValues(context.getConfiguration().get(name, ""));
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
private static List<String> doGetPropertyValues(final String s) {
|
121 |
|
|
return Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split(s));
|
122 |
|
|
}
|
123 |
|
|
|
124 |
54182
|
claudio.at
|
public static List<Oaf> rel(final Result value) {
|
125 |
54164
|
claudio.at
|
return value.list().stream()
|
126 |
|
|
.filter(kv -> {
|
127 |
|
|
final String q = new String(kv.getQualifier());
|
128 |
|
|
return q.matches(OafRowKeyDecoder.ID_REGEX);
|
129 |
54182
|
claudio.at
|
})
|
130 |
|
|
.filter(kv -> kv.getValue() != null && kv.getValue().length > 0)
|
131 |
|
|
.map(kv -> parseProto(kv.getValue()))
|
132 |
|
|
.collect(Collectors.toList());
|
133 |
54164
|
claudio.at
|
}
|
134 |
|
|
|
135 |
|
|
public static Oaf parseProto(final byte[] value) {
|
136 |
|
|
final OafDecoder d = OafDecoder.decode(value);
|
137 |
|
|
return d.getOaf();
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
public static Put asPut(final Oaf oaf) {
|
141 |
|
|
switch (oaf.getKind()) {
|
142 |
|
|
case entity:
|
143 |
|
|
|
144 |
|
|
final Put entity = getPut(oaf.getEntity().getId());
|
145 |
|
|
byte[] cf = Bytes.toBytes(oaf.getEntity().getType().toString());
|
146 |
|
|
return entity.add(cf, DedupUtils.BODY_B, oaf.toByteArray());
|
147 |
|
|
case relation:
|
148 |
|
|
final OafProtos.OafRel rel = oaf.getRel();
|
149 |
|
|
final Put putRel = getPut(rel.getSource());
|
150 |
|
|
|
151 |
|
|
final OafRelDecoder relDecoder = OafRelDecoder.decode(rel);
|
152 |
|
|
|
153 |
54187
|
claudio.at
|
final byte[] cfRel = Bytes.toBytes(relDecoder.getCFQ());
|
154 |
54164
|
claudio.at
|
final byte[] qualifier = Bytes.toBytes(rel.getTarget());
|
155 |
|
|
|
156 |
|
|
return putRel.add(cfRel, qualifier, oaf.toByteArray());
|
157 |
|
|
default:
|
158 |
|
|
throw new IllegalArgumentException("invalid kind");
|
159 |
|
|
}
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
private static Put getPut(final String rowkey) {
|
163 |
|
|
final Put put = new Put(Bytes.toBytes(rowkey));
|
164 |
|
|
put.setDurability(Durability.SKIP_WAL);
|
165 |
|
|
return put;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
public static byte[] getBodyB(final Result value, final TypeProtos.Type type) {
|
169 |
|
|
return value.getValue(Bytes.toBytes(type.toString()), DedupUtils.BODY_B);
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
public static Oaf getBody(final Result value, final TypeProtos.Type type) throws InvalidProtocolBufferException {
|
173 |
|
|
final byte[] body = getBodyB(value, type);
|
174 |
|
|
return body != null ? parseProto(body) : null;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
26600
|
sandro.lab
|
}
|