2020-10-20 12:19:46 +02:00
|
|
|
|
2020-03-27 10:42:17 +01:00
|
|
|
package eu.dnetlib.dhp.oa.dedup;
|
2019-12-10 14:57:16 +01:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Iterator;
|
2020-05-21 11:52:14 +02:00
|
|
|
import java.util.List;
|
2020-09-29 15:31:46 +02:00
|
|
|
|
2020-04-18 12:06:23 +02:00
|
|
|
import org.apache.spark.api.java.function.MapFunction;
|
|
|
|
import org.apache.spark.api.java.function.MapGroupsFunction;
|
|
|
|
import org.apache.spark.sql.Dataset;
|
2019-12-10 14:57:16 +01:00
|
|
|
import org.apache.spark.sql.Encoders;
|
|
|
|
import org.apache.spark.sql.SparkSession;
|
|
|
|
|
2020-09-29 15:31:46 +02:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
2020-10-08 17:29:29 +02:00
|
|
|
import eu.dnetlib.dhp.oa.merge.AuthorMerger;
|
2020-09-29 15:31:46 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
|
|
|
import scala.Tuple2;
|
2020-07-22 17:29:48 +02:00
|
|
|
|
2019-12-10 14:57:16 +01:00
|
|
|
public class DedupRecordFactory {
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
|
|
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
private DedupRecordFactory() {
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public static <T extends OafEntity> Dataset<T> createDedupRecord(
|
|
|
|
final SparkSession spark,
|
|
|
|
final DataInfo dataInfo,
|
|
|
|
final String mergeRelsInputPath,
|
|
|
|
final String entitiesInputPath,
|
|
|
|
final Class<T> clazz) {
|
|
|
|
|
|
|
|
long ts = System.currentTimeMillis();
|
|
|
|
|
|
|
|
// <id, json_entity>
|
|
|
|
Dataset<Tuple2<String, T>> entities = spark
|
|
|
|
.read()
|
|
|
|
.textFile(entitiesInputPath)
|
|
|
|
.map(
|
|
|
|
(MapFunction<String, Tuple2<String, T>>) it -> {
|
|
|
|
T entity = OBJECT_MAPPER.readValue(it, clazz);
|
|
|
|
return new Tuple2<>(entity.getId(), entity);
|
|
|
|
},
|
|
|
|
Encoders.tuple(Encoders.STRING(), Encoders.kryo(clazz)));
|
|
|
|
|
|
|
|
// <source, target>: source is the dedup_id, target is the id of the mergedIn
|
|
|
|
Dataset<Tuple2<String, String>> mergeRels = spark
|
|
|
|
.read()
|
|
|
|
.load(mergeRelsInputPath)
|
|
|
|
.as(Encoders.bean(Relation.class))
|
|
|
|
.where("relClass == 'merges'")
|
|
|
|
.map(
|
|
|
|
(MapFunction<Relation, Tuple2<String, String>>) r -> new Tuple2<>(r.getSource(), r.getTarget()),
|
|
|
|
Encoders.tuple(Encoders.STRING(), Encoders.STRING()));
|
|
|
|
|
|
|
|
return mergeRels
|
|
|
|
.joinWith(entities, mergeRels.col("_2").equalTo(entities.col("_1")), "inner")
|
|
|
|
.map(
|
|
|
|
(MapFunction<Tuple2<Tuple2<String, String>, Tuple2<String, T>>, Tuple2<String, T>>) value -> new Tuple2<>(
|
|
|
|
value._1()._1(), value._2()._2()),
|
|
|
|
Encoders.tuple(Encoders.STRING(), Encoders.kryo(clazz)))
|
|
|
|
.groupByKey(
|
2021-08-11 12:13:22 +02:00
|
|
|
(MapFunction<Tuple2<String, T>, String>) Tuple2::_1, Encoders.STRING())
|
2020-04-27 14:52:31 +02:00
|
|
|
.mapGroups(
|
|
|
|
(MapGroupsFunction<String, Tuple2<String, T>, T>) (key,
|
2020-05-21 11:52:14 +02:00
|
|
|
values) -> entityMerger(key, values, ts, dataInfo, clazz),
|
2020-04-27 14:52:31 +02:00
|
|
|
Encoders.bean(clazz));
|
|
|
|
}
|
|
|
|
|
2020-05-21 11:52:14 +02:00
|
|
|
public static <T extends OafEntity> T entityMerger(
|
2020-05-22 12:34:00 +02:00
|
|
|
String id, Iterator<Tuple2<String, T>> entities, long ts, DataInfo dataInfo, Class<T> clazz)
|
|
|
|
throws IllegalAccessException, InstantiationException {
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2020-05-21 11:52:14 +02:00
|
|
|
T entity = clazz.newInstance();
|
2020-04-27 14:52:31 +02:00
|
|
|
|
|
|
|
final Collection<String> dates = Lists.newArrayList();
|
2020-05-21 11:52:14 +02:00
|
|
|
final List<List<Author>> authors = Lists.newArrayList();
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
entities
|
|
|
|
.forEachRemaining(
|
|
|
|
t -> {
|
|
|
|
T duplicate = t._2();
|
2020-07-22 17:29:48 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
entity.mergeFrom(duplicate);
|
|
|
|
if (ModelSupport.isSubClass(duplicate, Result.class)) {
|
|
|
|
Result r1 = (Result) duplicate;
|
2021-08-11 12:13:22 +02:00
|
|
|
if (r1.getAuthor() != null && !r1.getAuthor().isEmpty())
|
2020-05-21 11:52:14 +02:00
|
|
|
authors.add(r1.getAuthor());
|
|
|
|
if (r1.getDateofacceptance() != null)
|
2020-04-27 14:52:31 +02:00
|
|
|
dates.add(r1.getDateofacceptance().getValue());
|
|
|
|
}
|
2020-07-22 17:29:48 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
});
|
|
|
|
|
2020-05-22 12:34:00 +02:00
|
|
|
// set authors and date
|
2020-04-27 14:52:31 +02:00
|
|
|
if (ModelSupport.isSubClass(entity, Result.class)) {
|
|
|
|
((Result) entity).setDateofacceptance(DatePicker.pick(dates));
|
2020-05-21 11:52:14 +02:00
|
|
|
((Result) entity).setAuthor(AuthorMerger.merge(authors));
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 18:06:07 +02:00
|
|
|
entity.setId(id);
|
2020-07-22 17:29:48 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
entity.setLastupdatetimestamp(ts);
|
|
|
|
entity.setDataInfo(dataInfo);
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
2020-07-22 17:29:48 +02:00
|
|
|
|
2019-12-10 14:57:16 +01:00
|
|
|
}
|