2021-01-28 09:51:17 +01:00
|
|
|
|
|
|
|
package eu.dnetlib.dhp.collection.worker;
|
|
|
|
|
2021-02-08 12:19:38 +01:00
|
|
|
import static eu.dnetlib.dhp.common.Constants.SEQUENCE_FILE_NAME;
|
2021-02-05 19:18:05 +01:00
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
|
import org.apache.hadoop.io.IntWritable;
|
|
|
|
import org.apache.hadoop.io.SequenceFile;
|
|
|
|
import org.apache.hadoop.io.Text;
|
2021-02-08 18:06:25 +01:00
|
|
|
import org.apache.hadoop.io.compress.GzipCodec;
|
2021-01-28 09:51:17 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
|
2021-02-08 12:19:38 +01:00
|
|
|
import eu.dnetlib.dhp.collection.ApiDescriptor;
|
2021-01-28 09:51:17 +01:00
|
|
|
import eu.dnetlib.dhp.collection.plugin.CollectorPlugin;
|
2021-02-05 19:18:05 +01:00
|
|
|
import eu.dnetlib.dhp.message.MessageSender;
|
2021-01-28 09:51:17 +01:00
|
|
|
|
|
|
|
public class CollectorWorker {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(CollectorWorker.class);
|
|
|
|
|
|
|
|
private final ApiDescriptor api;
|
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
private final Configuration conf;
|
2021-01-28 09:51:17 +01:00
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
private final MDStoreVersion mdStoreVersion;
|
2021-01-28 09:51:17 +01:00
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
private final HttpClientParams clientParams;
|
|
|
|
|
|
|
|
private final CollectorPluginReport report;
|
2021-02-04 15:51:15 +01:00
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
private final MessageSender messageSender;
|
2021-02-04 15:51:15 +01:00
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
public CollectorWorker(
|
|
|
|
final ApiDescriptor api,
|
2021-02-05 19:18:05 +01:00
|
|
|
final Configuration conf,
|
|
|
|
final MDStoreVersion mdStoreVersion,
|
|
|
|
final HttpClientParams clientParams,
|
|
|
|
final MessageSender messageSender,
|
|
|
|
final CollectorPluginReport report) {
|
2021-01-28 09:51:17 +01:00
|
|
|
this.api = api;
|
2021-02-05 19:18:05 +01:00
|
|
|
this.conf = conf;
|
|
|
|
this.mdStoreVersion = mdStoreVersion;
|
|
|
|
this.clientParams = clientParams;
|
2021-02-04 15:51:15 +01:00
|
|
|
this.messageSender = messageSender;
|
2021-02-05 19:18:05 +01:00
|
|
|
this.report = report;
|
2021-01-28 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
public void collect() throws UnknownCollectorPluginException, CollectorException, IOException {
|
2021-02-03 12:33:41 +01:00
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
final String outputPath = mdStoreVersion.getHdfsPath() + SEQUENCE_FILE_NAME;
|
|
|
|
log.info("outputPath path is {}", outputPath);
|
2021-02-03 12:33:41 +01:00
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
final CollectorPlugin plugin = CollectorPluginFactory.getPluginByProtocol(clientParams, api.getProtocol());
|
2021-02-03 12:33:41 +01:00
|
|
|
final AtomicInteger counter = new AtomicInteger(0);
|
2021-02-04 15:51:15 +01:00
|
|
|
|
2021-02-03 12:33:41 +01:00
|
|
|
try (SequenceFile.Writer writer = SequenceFile
|
|
|
|
.createWriter(
|
|
|
|
conf,
|
2021-02-05 19:18:05 +01:00
|
|
|
SequenceFile.Writer.file(new Path(outputPath)),
|
2021-02-03 12:33:41 +01:00
|
|
|
SequenceFile.Writer.keyClass(IntWritable.class),
|
2021-02-08 18:06:25 +01:00
|
|
|
SequenceFile.Writer.valueClass(Text.class),
|
|
|
|
SequenceFile.Writer.compression(SequenceFile.CompressionType.BLOCK, new GzipCodec()))) {
|
2021-02-03 12:33:41 +01:00
|
|
|
final IntWritable key = new IntWritable(counter.get());
|
|
|
|
final Text value = new Text();
|
|
|
|
plugin
|
2021-02-05 19:18:05 +01:00
|
|
|
.collect(api, report)
|
2021-02-03 12:33:41 +01:00
|
|
|
.forEach(
|
|
|
|
content -> {
|
|
|
|
key.set(counter.getAndIncrement());
|
2021-02-05 19:18:05 +01:00
|
|
|
if (counter.get() % 500 == 0)
|
2021-02-04 15:51:15 +01:00
|
|
|
messageSender.sendMessage(counter.longValue(), null);
|
2021-02-03 12:33:41 +01:00
|
|
|
value.set(content);
|
|
|
|
try {
|
|
|
|
writer.append(key, value);
|
2021-02-05 19:18:05 +01:00
|
|
|
} catch (Throwable e) {
|
|
|
|
report.put(e.getClass().getName(), e.getMessage());
|
|
|
|
log.warn("setting report to failed");
|
|
|
|
report.setSuccess(false);
|
2021-02-03 12:33:41 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
});
|
2021-02-05 19:18:05 +01:00
|
|
|
} catch (Throwable e) {
|
|
|
|
report.put(e.getClass().getName(), e.getMessage());
|
|
|
|
log.warn("setting report to failed");
|
|
|
|
report.setSuccess(false);
|
2021-02-03 12:33:41 +01:00
|
|
|
} finally {
|
2021-02-05 19:18:05 +01:00
|
|
|
messageSender.sendMessage(counter.longValue(), counter.longValue());
|
2021-01-28 09:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-05 19:18:05 +01:00
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
}
|