Search in sources :

Example 46 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project incubator-inlong by apache.

the class TestEsCallbackListener method testBeforeBulk.

/**
 * testBeforeBulk
 */
@Test
public void testBeforeBulk() {
    // prepare
    ProfileEvent event = TestEsSinkContext.mockProfileEvent();
    EsIndexRequest indexRequest = context.getIndexRequestHandler().parse(context, event);
    long executionId = 0;
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(indexRequest);
    // test
    EsCallbackListener listener = new EsCallbackListener(context);
    listener.beforeBulk(executionId, bulkRequest);
}
Also used : ProfileEvent(org.apache.inlong.sort.standalone.channel.ProfileEvent) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 47 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project Zpider by zeroized.

the class ElasticClient method bulkIndex.

public List<String> bulkIndex(List<DataEntity> docs) throws IOException {
    BulkRequest bulkRequest = new BulkRequest();
    for (DataEntity doc : docs) {
        IndexRequest indexRequest = new IndexRequest(doc.getIndexId(), "doc", doc.getId());
        indexRequest.source(doc.getData());
        bulkRequest.add(indexRequest);
    }
    BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest);
    return Arrays.stream(bulkResponse.getItems()).map(BulkItemResponse::getId).collect(Collectors.toList());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DataEntity(com.zeroized.spider.domain.observable.DataEntity) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 48 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project Zpider by zeroized.

the class ElasticClient method bulkIndex.

public List<String> bulkIndex(List<Map<String, ?>> docs, String index) throws IOException {
    BulkRequest bulkRequest = new BulkRequest();
    for (Map<String, ?> doc : docs) {
        IndexRequest indexRequest = new IndexRequest(index, "doc");
        indexRequest.source(doc);
        bulkRequest.add(indexRequest);
    }
    BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest);
    return Arrays.stream(bulkResponse.getItems()).map(BulkItemResponse::getId).collect(Collectors.toList());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 49 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project vertx-zero by silentbalanceyh.

the class AbstractEsClient method doBatch.

protected Boolean doBatch(final JsonArray documents, final String idField, final Supplier<BulkRequest> executor) {
    if (Ut.isNil(documents)) {
        /*
             * No data, not needed
             */
        return true;
    } else {
        final RestHighLevelClient client = this.client();
        boolean result;
        try {
            final BulkRequest request = executor.get();
            final BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
            if (bulkResponse.hasFailures()) {
                this.logger().warn("Failure found: {0}", bulkResponse.buildFailureMessage());
                result = false;
            } else {
                this.logger().info("Documents have been indexed ( size = {0} ) successfully!", documents.size());
                result = true;
            }
        } catch (final IOException ioe) {
            this.logger().jvm(ioe);
            result = false;
        }
        this.helper.closeClient(client);
        return result;
    }
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException)

Example 50 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project vertx-zero by silentbalanceyh.

the class EsAmbitUpdate method process.

@Override
public Boolean process(final JsonArray documents, final String idField) {
    return this.doBatch(documents, idField, () -> {
        final BulkRequest request = new BulkRequest();
        Ut.itJArray(documents).forEach(json -> {
            final String documentId = json.getString(idField);
            if (Ut.notNil(documentId)) {
                final UpdateRequest indexRequest = new UpdateRequest().index(this.index).id(documentId).doc(this.toDocument(json));
                request.add(indexRequest);
            }
        });
        return request;
    });
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Aggregations

BulkRequest (org.elasticsearch.action.bulk.BulkRequest)158 IndexRequest (org.elasticsearch.action.index.IndexRequest)77 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)73 IOException (java.io.IOException)47 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)40 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)28 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)27 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)25 ArrayList (java.util.ArrayList)24 List (java.util.List)18 SearchRequest (org.elasticsearch.action.search.SearchRequest)17 Test (org.junit.jupiter.api.Test)17 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)16 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)16 Map (java.util.Map)15 Test (org.junit.Test)15 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 BulkProcessor (org.elasticsearch.action.bulk.BulkProcessor)13 ActionListener (org.elasticsearch.action.ActionListener)12