2020-04-27 14:52:31 +02:00
|
|
|
|
2020-04-10 17:53:07 +02:00
|
|
|
package eu.dnetlib.dhp.oa.graph.raw;
|
2020-01-17 15:26:21 +01:00
|
|
|
|
2020-04-10 17:53:07 +02:00
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2021-05-04 18:06:21 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.mongodb.MongoClient;
|
|
|
|
import com.mongodb.MongoClientURI;
|
2020-03-02 16:12:14 +01:00
|
|
|
|
2020-04-28 11:23:29 +02:00
|
|
|
import eu.dnetlib.dhp.application.ArgumentApplicationParser;
|
2021-02-24 15:07:24 +01:00
|
|
|
import eu.dnetlib.dhp.common.MdstoreClient;
|
2021-05-04 18:06:21 +02:00
|
|
|
import eu.dnetlib.dhp.common.MdstoreTx;
|
2021-02-25 12:30:30 +01:00
|
|
|
import eu.dnetlib.dhp.oa.graph.raw.common.AbstractMigrationApplication;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2021-05-04 18:06:21 +02:00
|
|
|
public class MigrateMongoMdstoresApplication extends AbstractMigrationApplication implements Closeable {
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2021-05-04 18:06:21 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(MigrateMongoMdstoresApplication.class);
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private final MdstoreClient mdstoreClient;
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public static void main(final String[] args) throws Exception {
|
|
|
|
final ArgumentApplicationParser parser = new ArgumentApplicationParser(
|
|
|
|
IOUtils
|
|
|
|
.toString(
|
|
|
|
MigrateMongoMdstoresApplication.class
|
2020-06-10 10:04:00 +02:00
|
|
|
.getResourceAsStream("/eu/dnetlib/dhp/oa/graph/migrate_mongo_mstores_parameters.json")));
|
2020-04-27 14:52:31 +02:00
|
|
|
parser.parseArgument(args);
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final String mongoBaseUrl = parser.get("mongoBaseUrl");
|
|
|
|
final String mongoDb = parser.get("mongoDb");
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final String mdFormat = parser.get("mdFormat");
|
|
|
|
final String mdLayout = parser.get("mdLayout");
|
|
|
|
final String mdInterpretation = parser.get("mdInterpretation");
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
final String hdfsPath = parser.get("hdfsPath");
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2021-05-04 18:06:21 +02:00
|
|
|
final MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoBaseUrl));
|
|
|
|
|
|
|
|
try (MigrateMongoMdstoresApplication app = new MigrateMongoMdstoresApplication(hdfsPath, mongoClient,
|
2020-04-27 14:52:31 +02:00
|
|
|
mongoDb)) {
|
|
|
|
app.execute(mdFormat, mdLayout, mdInterpretation);
|
|
|
|
}
|
|
|
|
}
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public MigrateMongoMdstoresApplication(
|
2021-05-04 18:06:21 +02:00
|
|
|
final String hdfsPath, final MongoClient mongoClient, final String mongoDb) throws Exception {
|
2020-04-27 14:52:31 +02:00
|
|
|
super(hdfsPath);
|
2021-05-04 18:06:21 +02:00
|
|
|
this.mdstoreClient = new MdstoreClient(mongoClient, mongoDb);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2021-05-04 18:06:21 +02:00
|
|
|
public void execute(final String format, final String layout, final String interpretation) throws IOException {
|
2020-04-27 14:52:31 +02:00
|
|
|
final Map<String, String> colls = mdstoreClient.validCollections(format, layout, interpretation);
|
2021-05-04 18:06:21 +02:00
|
|
|
log.info("Found {} mdstores", colls.size());
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
for (final Entry<String, String> entry : colls.entrySet()) {
|
2021-05-04 18:06:21 +02:00
|
|
|
log.info("Processing mdstore {}", entry.getKey());
|
|
|
|
|
|
|
|
final String mdID = entry.getKey();
|
|
|
|
try (final MdstoreTx tx = mdstoreClient.readLock(mdID)) {
|
|
|
|
|
|
|
|
log.info("locked collection {}", tx.getCurrentId());
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2021-05-04 18:06:21 +02:00
|
|
|
for (final String xml : tx) {
|
|
|
|
emit(xml, String.format("%s-%s-%s", format, layout, interpretation));
|
|
|
|
}
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
super.close();
|
|
|
|
mdstoreClient.close();
|
|
|
|
}
|
2020-01-17 15:26:21 +01:00
|
|
|
}
|