2020-04-29 17:29:49 +02:00
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
package eu.dnetlib.dhp.blacklist;
|
2020-04-29 17:29:49 +02:00
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Function;
|
2020-04-30 10:26:50 +02:00
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
2020-04-29 17:29:49 +02:00
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
|
import org.apache.hadoop.fs.FSDataOutputStream;
|
|
|
|
import org.apache.hadoop.fs.FileSystem;
|
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-04-29 17:29:49 +02:00
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2020-05-04 12:26:17 +02:00
|
|
|
import eu.dnetlib.dhp.common.DbClient;
|
2020-04-30 10:26:50 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
|
|
|
import eu.dnetlib.dhp.schema.common.RelationInverse;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.Relation;
|
2020-04-29 17:29:49 +02:00
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
public class ReadBlacklistFromDB implements Closeable {
|
2020-04-29 17:29:49 +02:00
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
private final DbClient dbClient;
|
|
|
|
private static final Log log = LogFactory.getLog(ReadBlacklistFromDB.class);
|
|
|
|
private final Configuration conf;
|
|
|
|
private final BufferedWriter writer;
|
2020-04-30 18:26:58 +02:00
|
|
|
private final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
2020-04-30 10:26:50 +02:00
|
|
|
|
|
|
|
private final static String query = "SELECT source_type, unnest(original_source_objects) as source, " +
|
|
|
|
"target_type, unnest(original_target_objects) as target, " +
|
|
|
|
"relationship FROM blacklist WHERE status = 'ACCEPTED'";
|
|
|
|
|
|
|
|
public static void main(final String[] args) throws Exception {
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
ReadBlacklistFromDB.class
|
|
|
|
.getResourceAsStream(
|
|
|
|
"/eu/dnetlib/dhp/blacklist/blacklist_parameters.json")));
|
|
|
|
|
|
|
|
parser.parseArgument(args);
|
|
|
|
|
|
|
|
final String dbUrl = parser.get("postgresUrl");
|
|
|
|
final String dbUser = parser.get("postgresUser");
|
|
|
|
final String dbPassword = parser.get("postgresPassword");
|
|
|
|
final String hdfsPath = parser.get("hdfsPath");
|
|
|
|
final String hdfsNameNode = parser.get("hdfsNameNode");
|
|
|
|
|
|
|
|
try (final ReadBlacklistFromDB rbl = new ReadBlacklistFromDB(hdfsPath, hdfsNameNode, dbUrl, dbUser,
|
|
|
|
dbPassword)) {
|
|
|
|
|
|
|
|
log.info("Processing blacklist...");
|
|
|
|
rbl.execute(query, rbl::processBlacklistEntry);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute(final String sql, final Function<ResultSet, List<Relation>> producer)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
final Consumer<ResultSet> consumer = rs -> producer.apply(rs).forEach(r -> writeRelation(r));
|
|
|
|
|
|
|
|
dbClient.processResults(sql, consumer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Relation> processBlacklistEntry(ResultSet rs) {
|
|
|
|
try {
|
|
|
|
Relation direct = new Relation();
|
|
|
|
Relation inverse = new Relation();
|
|
|
|
|
|
|
|
String source_prefix = ModelSupport.entityIdPrefix.get(rs.getString("source_type"));
|
|
|
|
String target_prefix = ModelSupport.entityIdPrefix.get(rs.getString("target_type"));
|
|
|
|
|
|
|
|
String source_direct = source_prefix + "|" + rs.getString("source");
|
|
|
|
direct.setSource(source_direct);
|
|
|
|
inverse.setTarget(source_direct);
|
|
|
|
|
|
|
|
String target_direct = target_prefix + "|" + rs.getString("target");
|
|
|
|
direct.setTarget(target_direct);
|
|
|
|
inverse.setSource(target_direct);
|
|
|
|
|
|
|
|
String encoding = rs.getString("relationship");
|
|
|
|
RelationInverse ri = ModelSupport.relationInverseMap.get(encoding);
|
|
|
|
direct.setRelClass(ri.getRelation());
|
|
|
|
inverse.setRelClass(ri.getInverse());
|
|
|
|
direct.setRelType(ri.getRelType());
|
|
|
|
inverse.setRelType(ri.getRelType());
|
|
|
|
direct.setSubRelType(ri.getSubReltype());
|
|
|
|
inverse.setSubRelType(ri.getSubReltype());
|
|
|
|
|
|
|
|
return Arrays.asList(direct, inverse);
|
|
|
|
|
|
|
|
} catch (final Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
dbClient.close();
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ReadBlacklistFromDB(
|
|
|
|
final String hdfsPath, String hdfsNameNode, final String dbUrl, final String dbUser, final String dbPassword)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
this.dbClient = new DbClient(dbUrl, dbUser, dbPassword);
|
|
|
|
this.conf = new Configuration();
|
|
|
|
this.conf.set("fs.defaultFS", hdfsNameNode);
|
|
|
|
FileSystem fileSystem = FileSystem.get(this.conf);
|
|
|
|
Path hdfsWritePath = new Path(hdfsPath);
|
2020-04-30 18:26:58 +02:00
|
|
|
FSDataOutputStream fsDataOutputStream = null;
|
|
|
|
if (fileSystem.exists(hdfsWritePath)) {
|
|
|
|
fsDataOutputStream = fileSystem.append(hdfsWritePath);
|
|
|
|
} else {
|
|
|
|
fsDataOutputStream = fileSystem.create(hdfsWritePath);
|
|
|
|
}
|
|
|
|
|
2020-04-30 10:26:50 +02:00
|
|
|
this.writer = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void writeRelation(final Relation r) {
|
|
|
|
try {
|
2020-04-30 18:26:58 +02:00
|
|
|
writer.write(OBJECT_MAPPER.writeValueAsString(r));
|
2020-04-30 10:26:50 +02:00
|
|
|
writer.newLine();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 17:29:49 +02:00
|
|
|
|
|
|
|
}
|