2022-07-20 11:38:40 +02:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.oa.graph.dump.complete;
|
|
|
|
|
2023-08-04 16:22:23 +02:00
|
|
|
import java.io.*;
|
2022-07-20 11:38:40 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-08-04 16:22:23 +02:00
|
|
|
import java.util.ArrayList;
|
2022-07-20 11:38:40 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
|
import org.apache.hadoop.fs.FSDataOutputStream;
|
|
|
|
import org.apache.hadoop.fs.FileSystem;
|
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2023-08-04 16:22:23 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
2022-07-20 11:38:40 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2023-10-31 10:42:43 +01:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.UtilCommunityAPI;
|
2022-07-20 11:38:40 +02:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.Utils;
|
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.exceptions.MyRuntimeException;
|
2023-08-04 16:22:23 +02:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.subset.MasterDuplicate;
|
2022-08-03 11:42:56 +02:00
|
|
|
import eu.dnetlib.dhp.oa.model.graph.*;
|
2022-07-20 11:38:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the set of new Relation between the context and datasources. At the moment the relation between the context
|
|
|
|
* and the project is not created because of a low coverage in the profiles of openaire ids related to projects
|
|
|
|
*/
|
|
|
|
public class CreateContextRelation implements Serializable {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(CreateContextRelation.class);
|
|
|
|
private final transient Configuration conf;
|
|
|
|
private final transient BufferedWriter writer;
|
2023-10-31 10:42:43 +01:00
|
|
|
|
2022-07-20 11:38:40 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
String jsonConfiguration = IOUtils
|
|
|
|
.toString(
|
|
|
|
Objects
|
|
|
|
.requireNonNull(
|
|
|
|
CreateContextRelation.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/oa/graph/dump/input_entity_parameter.json")));
|
|
|
|
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
Boolean isSparkSessionManaged = Optional
|
|
|
|
.ofNullable(parser.get("isSparkSessionManaged"))
|
|
|
|
.map(Boolean::valueOf)
|
|
|
|
.orElse(Boolean.TRUE);
|
|
|
|
log.info("isSparkSessionManaged: {}", isSparkSessionManaged);
|
|
|
|
|
|
|
|
final String hdfsPath = parser.get("hdfsPath");
|
|
|
|
log.info("hdfsPath: {}", hdfsPath);
|
|
|
|
|
|
|
|
final String hdfsNameNode = parser.get("nameNode");
|
2023-08-04 16:22:23 +02:00
|
|
|
log.info("hdfsNameNode: {}", hdfsNameNode);
|
2022-07-20 11:38:40 +02:00
|
|
|
|
2023-08-04 16:22:23 +02:00
|
|
|
final String masterDuplicatePath = parser.get("masterDuplicate");
|
|
|
|
log.info("masterDuplicatePath: {}", masterDuplicatePath);
|
|
|
|
|
2023-10-30 14:28:55 +01:00
|
|
|
final CreateContextRelation cce = new CreateContextRelation(hdfsPath, hdfsNameNode);
|
2022-07-20 11:38:40 +02:00
|
|
|
|
2023-10-30 14:28:55 +01:00
|
|
|
log.info("Creating relation for datasources and projects...");
|
2022-07-20 11:38:40 +02:00
|
|
|
cce
|
|
|
|
.execute(
|
2023-10-30 14:28:55 +01:00
|
|
|
Process::getRelation);
|
2022-07-20 11:38:40 +02:00
|
|
|
|
|
|
|
cce.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:22:23 +02:00
|
|
|
private List<MasterDuplicate> readMasterDuplicate(String masterDuplicatePath) throws IOException {
|
|
|
|
FileSystem fileSystem = FileSystem.get(conf);
|
|
|
|
Path hdfsReadPath = new Path(masterDuplicatePath);
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(fileSystem.open(hdfsReadPath)));
|
|
|
|
List<MasterDuplicate> mdlist = new ArrayList<>();
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
String line;
|
|
|
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
mdlist.add(mapper.readValue(line, MasterDuplicate.class));
|
|
|
|
}
|
|
|
|
return mdlist;
|
|
|
|
}
|
|
|
|
|
2022-07-20 11:38:40 +02:00
|
|
|
private void close() throws IOException {
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:28:55 +01:00
|
|
|
public CreateContextRelation(String hdfsPath, String hdfsNameNode)
|
2023-10-31 10:42:43 +01:00
|
|
|
throws IOException {
|
2022-07-20 11:38:40 +02:00
|
|
|
this.conf = new Configuration();
|
|
|
|
this.conf.set("fs.defaultFS", hdfsNameNode);
|
|
|
|
|
|
|
|
FileSystem fileSystem = FileSystem.get(this.conf);
|
|
|
|
Path hdfsWritePath = new Path(hdfsPath);
|
|
|
|
FSDataOutputStream fsDataOutputStream = null;
|
|
|
|
if (fileSystem.exists(hdfsWritePath)) {
|
|
|
|
fsDataOutputStream = fileSystem.append(hdfsWritePath);
|
|
|
|
} else {
|
|
|
|
fsDataOutputStream = fileSystem.create(hdfsWritePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.writer = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:28:55 +01:00
|
|
|
public void execute(final Function<ContextInfo, List<Relation>> producer) throws IOException {
|
2023-08-04 16:22:23 +02:00
|
|
|
|
2022-07-20 11:38:40 +02:00
|
|
|
final Consumer<ContextInfo> consumer = ci -> producer.apply(ci).forEach(this::writeEntity);
|
|
|
|
|
2023-10-30 14:28:55 +01:00
|
|
|
UtilCommunityAPI queryCommunityAPI = new UtilCommunityAPI();
|
2023-10-31 10:38:09 +01:00
|
|
|
queryCommunityAPI.getContextRelation().forEach(ci -> consumer.accept(ci));
|
2022-07-20 11:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void writeEntity(final Relation r) {
|
|
|
|
try {
|
|
|
|
writer.write(Utils.OBJECT_MAPPER.writeValueAsString(r));
|
|
|
|
writer.newLine();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
throw new MyRuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|