Search in sources :

Example 41 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project graylog2-server by Graylog2.

the class FixtureImporterES7 method importNode.

private void importNode(JsonNode root) throws IOException {
    /* This supports the nosqlunit DataSet structure:
         *
         *  {
         *    "documents": [
         *      {
         *        "document": [
         *          {
         *            "index": {
         *              "indexName": "graylog_0",
         *              "indexId": "0"
         *            }
         *          },
         *          {
         *            "data": {
         *              "source": "example.org",
         *              "message": "Hi",
         *              "timestamp": "2015-01-01 01:00:00.000"
         *            }
         *          }
         *        ]
         *      }
         *    ]
         *  }
         */
    final BulkRequest bulkRequest = new BulkRequest();
    final Set<String> targetIndices = new HashSet<>();
    for (final JsonNode document : root.path("documents")) {
        final List<JsonNode> indexes = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        for (JsonNode entry : document.path("document")) {
            if (entry.hasNonNull("index")) {
                indexes.add(entry.path("index"));
            } else if (entry.hasNonNull("data")) {
                data = OBJECT_MAPPER.convertValue(entry.path("data"), TypeReferences.MAP_STRING_OBJECT);
            }
        }
        for (final JsonNode index : indexes) {
            final IndexRequest indexRequest = new IndexRequest().source(data);
            final String indexName = index.path("indexName").asText(null);
            if (indexName == null) {
                throw new IllegalArgumentException("Missing indexName in " + index);
            }
            targetIndices.add(indexName);
            indexRequest.index(indexName);
            if (index.hasNonNull("indexId")) {
                indexRequest.id(index.path("indexId").asText());
            }
            bulkRequest.add(indexRequest);
        }
    }
    for (String indexName : targetIndices) {
        if (!indexExists(indexName)) {
            createIndex(indexName);
        }
    }
    final BulkResponse result = client.execute((c, requestOptions) -> c.bulk(bulkRequest, requestOptions), "Unable to import fixtures.");
    if (result.hasFailures()) {
        throw new IllegalStateException("Error while bulk indexing documents: " + result.buildFailureMessage());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse) GetIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexRequest) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) HashSet(java.util.HashSet)

Example 42 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project graylog2-server by Graylog2.

the class MessagesAdapterES7 method bulkIndexChunked.

private List<Messages.IndexingError> bulkIndexChunked(ChunkedBulkIndexer.Chunk command) throws ChunkedBulkIndexer.EntityTooLargeException {
    final List<IndexingRequest> messageList = command.requests;
    final int offset = command.offset;
    final int chunkSize = command.size;
    if (messageList.isEmpty()) {
        return Collections.emptyList();
    }
    final Iterable<List<IndexingRequest>> chunks = Iterables.partition(messageList.subList(offset, messageList.size()), chunkSize);
    int chunkCount = 1;
    int indexedSuccessfully = 0;
    final List<Messages.IndexingError> indexFailures = new ArrayList<>();
    for (List<IndexingRequest> chunk : chunks) {
        final BulkResponse result = runBulkRequest(indexedSuccessfully, chunk);
        indexedSuccessfully += chunk.size();
        final List<BulkItemResponse> failures = extractFailures(result);
        indexFailures.addAll(indexingErrorsFrom(failures, messageList));
        logDebugInfo(messageList, offset, chunkSize, chunkCount, result, failures);
        logFailures(result, failures.size());
        chunkCount++;
    }
    return indexFailures;
}
Also used : IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) ArrayList(java.util.ArrayList) BulkItemResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse) ArrayList(java.util.ArrayList) List(java.util.List) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse)

Example 43 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project graylog2-server by Graylog2.

the class MessagesAdapterES7 method runBulkRequest.

private BulkResponse runBulkRequest(int indexedSuccessfully, List<IndexingRequest> chunk) throws ChunkedBulkIndexer.EntityTooLargeException {
    final BulkRequest bulkRequest = createBulkRequest(chunk);
    final BulkResponse result;
    try {
        result = this.client.execute((c, requestOptions) -> c.bulk(bulkRequest, requestOptions));
    } catch (ElasticsearchException e) {
        for (ElasticsearchException cause : e.guessRootCauses()) {
            if (cause.status().equals(RestStatus.REQUEST_ENTITY_TOO_LARGE)) {
                throw new ChunkedBulkIndexer.EntityTooLargeException(indexedSuccessfully, indexingErrorsFrom(chunk));
            }
        }
        throw new org.graylog2.indexer.ElasticsearchException(e);
    }
    return result;
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest) MessagesAdapter(org.graylog2.indexer.messages.MessagesAdapter) ElasticsearchException(org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException) LoggerFactory(org.slf4j.LoggerFactory) XContentType(org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType) GetResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Meter(com.codahale.metrics.Meter) Indexable(org.graylog2.indexer.messages.Indexable) ResultMessage(org.graylog2.indexer.results.ResultMessage) Locale(java.util.Locale) Map(java.util.Map) Messages(org.graylog2.indexer.messages.Messages) AnalyzeResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.AnalyzeResponse) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) IndexingRequest(org.graylog2.indexer.messages.IndexingRequest) BulkItemResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse) AnalyzeRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.AnalyzeRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GetRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RestStatus(org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) ChunkedBulkIndexer(org.graylog2.indexer.messages.ChunkedBulkIndexer) Collections(java.util.Collections) MetricRegistry.name(com.codahale.metrics.MetricRegistry.name) DocumentNotFoundException(org.graylog2.indexer.messages.DocumentNotFoundException) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse) ElasticsearchException(org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException) ChunkedBulkIndexer(org.graylog2.indexer.messages.ChunkedBulkIndexer)

Example 44 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project rssriver by dadoonet.

the class RssRiver method start.

@Override
public void start() {
    if (logger.isInfoEnabled())
        logger.info("Starting rss stream");
    try {
        client.admin().indices().prepareCreate(indexName).execute().actionGet();
    } catch (Exception e) {
        if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
        // that's fine
        } else if (ExceptionsHelper.unwrapCause(e) instanceof ClusterBlockException) {
        // ok, not recovered yet..., lets start indexing and hope we
        // recover by the first bulk
        // TODO: a smarter logic can be to register for cluster event
        // listener here, and only start sampling when the block is
        // removed...
        } else {
            logger.warn("failed to create index [{}], disabling river...", e, indexName);
            return;
        }
    }
    try {
        pushMapping(indexName, typeName, RssToJson.buildRssMapping(typeName, raw));
    } catch (Exception e) {
        logger.warn("failed to create mapping for [{}/{}], disabling river...", e, indexName, typeName);
        return;
    }
    // Creating bulk processor
    this.bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {

        @Override
        public void beforeBulk(long executionId, BulkRequest request) {
            logger.debug("Going to execute new bulk composed of {} actions", request.numberOfActions());
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
            logger.debug("Executed bulk composed of {} actions", request.numberOfActions());
            if (response.hasFailures()) {
                logger.warn("There was failures while executing bulk", response.buildFailureMessage());
                if (logger.isDebugEnabled()) {
                    for (BulkItemResponse item : response.getItems()) {
                        if (item.isFailed()) {
                            logger.debug("Error for {}/{}/{} for {} operation: {}", item.getIndex(), item.getType(), item.getId(), item.getOpType(), item.getFailureMessage());
                        }
                    }
                }
            }
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
            logger.warn("Error executing bulk", failure);
        }
    }).setBulkActions(bulkSize).setConcurrentRequests(maxConcurrentBulk).setFlushInterval(bulkFlushInterval).build();
    // We create as many Threads as there are feeds
    threads = new ArrayList<Thread>(feedsDefinition.size());
    int threadNumber = 0;
    for (RssRiverFeedDefinition feedDefinition : feedsDefinition) {
        Thread thread = EsExecutors.daemonThreadFactory(settings.globalSettings(), "rss_slurper_" + threadNumber).newThread(new RSSParser(feedDefinition));
        thread.start();
        threads.add(thread);
        threadNumber++;
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FeedException(com.rometools.rome.io.FeedException) IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Example 45 with BulkResponse

use of org.elasticsearch.action.bulk.BulkResponse in project MSEC by Tencent.

the class ESClientThread method run.

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        while (true) {
            ESClientThread.ESThreadRequest request = queue.take();
            BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (int i = 0; i < request.sourceList.size(); i++) {
                // System.out.println("take from queue: " + source);
                bulkRequest.add(client.prepareIndex(request.indexNameList.get(i), request.indexTypeList.get(i)).setSource(request.sourceList.get(i)));
                LOG.info("taken source: " + request.sourceList.get(i));
            }
            BulkResponse bulkResponse = bulkRequest.execute().actionGet();
            if (bulkResponse.hasFailures()) {
                System.out.println("bulk response errorss!" + bulkResponse.buildFailureMessage());
                bulkResponse = bulkRequest.execute().actionGet();
            }
        // Thread.sleep(10);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Aggregations

BulkResponse (org.elasticsearch.action.bulk.BulkResponse)111 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)60 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)40 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)28 IOException (java.io.IOException)21 IndexRequest (org.elasticsearch.action.index.IndexRequest)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 ArrayList (java.util.ArrayList)13 List (java.util.List)11 Map (java.util.Map)11 IndexResponse (org.elasticsearch.action.index.IndexResponse)10 Test (org.junit.Test)10 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 SearchHit (org.elasticsearch.search.SearchHit)9 ElasticsearchException (org.elasticsearch.ElasticsearchException)8 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)8 BulkProcessor (org.elasticsearch.action.bulk.BulkProcessor)8 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)8 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)7