AriadnePlus/dnet-ariadneplus-graphdb-pu.../src/main/java/eu/dnetlib/ariadneplus/elasticsearch/BulkUpload.java

63 lines
2.2 KiB
Java
Raw Normal View History

package eu.dnetlib.ariadneplus.elasticsearch;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.annotation.PostConstruct;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import eu.dnetlib.ariadneplus.elasticsearch.model.AriadneCatalogEntry;
import eu.dnetlib.ariadneplus.reader.ResourceManager;
@Service
public class BulkUpload {
@Value("${elasticsearch.url:localhost:9200}")
private String elasticsearchUrl;
private RestHighLevelClient client;
@PostConstruct
private void init() throws IOException {
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost",9200,"http")));
}
public void index(ResourceManager manager) {
BulkRequest request = new BulkRequest();
while (manager.hasNext()){
try {
AriadneCatalogEntry ace = ((AriadneCatalogEntry) manager.next());
request.add(new IndexRequest("prova_via_code").id(ace.getOriginalId())
.source(ace.toJson(),XContentType.JSON));
System.out.println("indexing to ES record "+ace.getOriginalId());
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}