Search in sources :

Example 1 with ElasticSearchMutation

use of org.janusgraph.diskstorage.es.ElasticSearchMutation in project janusgraph by JanusGraph.

the class RestElasticSearchClient method bulkRequest.

@Override
public void bulkRequest(List<ElasticSearchMutation> requests, String ingestPipeline) throws IOException {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (final ElasticSearchMutation request : requests) {
        final Map actionData = ImmutableMap.of(request.getRequestType().name().toLowerCase(), ImmutableMap.of("_index", request.getIndex(), "_type", request.getType(), "_id", request.getId()));
        outputStream.write(mapWriter.writeValueAsBytes(actionData));
        outputStream.write("\n".getBytes(UTF8_CHARSET));
        if (request.getSource() != null) {
            outputStream.write(mapWriter.writeValueAsBytes(request.getSource()));
            outputStream.write("\n".getBytes(UTF8_CHARSET));
        }
    }
    final StringBuilder builder = new StringBuilder();
    if (ingestPipeline != null) {
        APPEND_OP.apply(builder).append("pipeline=").append(ingestPipeline);
    }
    if (bulkRefresh != null && !bulkRefresh.toLowerCase().equals("false")) {
        APPEND_OP.apply(builder).append("refresh=").append(bulkRefresh);
    }
    builder.insert(0, REQUEST_SEPARATOR + "_bulk");
    final Response response = performRequest(REQUEST_TYPE_POST, builder.toString(), outputStream.toByteArray());
    try (final InputStream inputStream = response.getEntity().getContent()) {
        final RestBulkResponse bulkResponse = mapper.readValue(inputStream, RestBulkResponse.class);
        final List<Object> errors = bulkResponse.getItems().stream().flatMap(item -> item.values().stream()).filter(item -> item.getError() != null && item.getStatus() != 404).map(RestBulkItemResponse::getError).collect(Collectors.toList());
        if (!errors.isEmpty()) {
            errors.forEach(error -> log.error("Failed to execute ES query: {}", error));
            throw new IOException("Failure(s) in Elasticsearch bulk request: " + errors);
        }
    }
}
Also used : ElasticSearchMutation(org.janusgraph.diskstorage.es.ElasticSearchMutation) RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) RestClient(org.elasticsearch.client.RestClient) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) SimpleModule(org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElasticSearchMutation(org.janusgraph.diskstorage.es.ElasticSearchMutation) ElasticSearchClient(org.janusgraph.diskstorage.es.ElasticSearchClient) LoggerFactory(org.slf4j.LoggerFactory) ObjectReader(org.apache.tinkerpop.shaded.jackson.databind.ObjectReader) HashMap(java.util.HashMap) Geoshape(org.janusgraph.core.attribute.Geoshape) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Function(java.util.function.Function) UTF8_CHARSET(org.janusgraph.util.encoding.StringEncoding.UTF8_CHARSET) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) TypeReference(org.apache.tinkerpop.shaded.jackson.core.type.TypeReference) JsonIgnoreProperties(org.apache.tinkerpop.shaded.jackson.annotation.JsonIgnoreProperties) SerializationFeature(org.apache.tinkerpop.shaded.jackson.databind.SerializationFeature) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) IndexMappings(org.janusgraph.diskstorage.es.IndexMappings) List(java.util.List) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) Response(org.elasticsearch.client.Response) ObjectWriter(org.apache.tinkerpop.shaded.jackson.databind.ObjectWriter) ElasticMajorVersion(org.janusgraph.diskstorage.es.ElasticMajorVersion) Collections(java.util.Collections) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 HttpEntity (org.apache.http.HttpEntity)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 ContentType (org.apache.http.entity.ContentType)1 JsonIgnoreProperties (org.apache.tinkerpop.shaded.jackson.annotation.JsonIgnoreProperties)1 TypeReference (org.apache.tinkerpop.shaded.jackson.core.type.TypeReference)1 ObjectMapper (org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper)1 ObjectReader (org.apache.tinkerpop.shaded.jackson.databind.ObjectReader)1