Search in sources :

Example 6 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project logging-log4j2 by apache.

the class LogstashIT method appendAndCollect.

private static <K> Map<K, Object> appendAndCollect(final List<LogEvent> logEvents, final Layout<?> layout, final int port, final Function<Map<String, Object>, K> keyMapper, final Set<String> excludedKeys) throws IOException {
    try (final RestHighLevelClient client = createClient()) {
        final Appender appender = createStartedAppender(layout, port);
        try {
            // Append the event.
            LOGGER.info("appending events");
            logEvents.forEach(appender::append);
            LOGGER.info("completed appending events");
            // Wait the message to arrive.
            Awaitility.await().atMost(Duration.ofSeconds(60)).pollDelay(Duration.ofSeconds(2)).until(() -> queryDocumentCount(client) == LOG_EVENT_COUNT);
            // Retrieve the persisted messages.
            return queryDocuments(client).stream().collect(Collectors.toMap(keyMapper, (final Map<String, Object> source) -> {
                excludedKeys.forEach(source::remove);
                return source;
            }));
        } finally {
            appender.stop();
        }
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 7 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project flink by apache.

the class Elasticsearch7SinkBuilder method getBulkProcessorBuilderFactory.

@Override
protected BulkProcessorBuilderFactory getBulkProcessorBuilderFactory() {
    return new BulkProcessorBuilderFactory() {

        @Override
        public BulkProcessor.Builder apply(RestHighLevelClient client, BulkProcessorConfig bulkProcessorConfig, BulkProcessor.Listener listener) {
            BulkProcessor.Builder builder = BulkProcessor.builder(new // This cannot be inlined as a
            BulkRequestConsumerFactory() {

                // lambda because then
                // deserialization fails
                @Override
                public void accept(BulkRequest bulkRequest, ActionListener<BulkResponse> bulkResponseActionListener) {
                    client.bulkAsync(bulkRequest, RequestOptions.DEFAULT, bulkResponseActionListener);
                }
            }, listener);
            if (bulkProcessorConfig.getBulkFlushMaxActions() != -1) {
                builder.setBulkActions(bulkProcessorConfig.getBulkFlushMaxActions());
            }
            if (bulkProcessorConfig.getBulkFlushMaxMb() != -1) {
                builder.setBulkSize(new ByteSizeValue(bulkProcessorConfig.getBulkFlushMaxMb(), ByteSizeUnit.MB));
            }
            if (bulkProcessorConfig.getBulkFlushInterval() != -1) {
                builder.setFlushInterval(new TimeValue(bulkProcessorConfig.getBulkFlushInterval()));
            }
            BackoffPolicy backoffPolicy;
            final TimeValue backoffDelay = new TimeValue(bulkProcessorConfig.getBulkFlushBackOffDelay());
            final int maxRetryCount = bulkProcessorConfig.getBulkFlushBackoffRetries();
            switch(bulkProcessorConfig.getFlushBackoffType()) {
                case CONSTANT:
                    backoffPolicy = BackoffPolicy.constantBackoff(backoffDelay, maxRetryCount);
                    break;
                case EXPONENTIAL:
                    backoffPolicy = BackoffPolicy.exponentialBackoff(backoffDelay, maxRetryCount);
                    break;
                case NONE:
                    backoffPolicy = BackoffPolicy.noBackoff();
                    break;
                default:
                    throw new IllegalArgumentException("Received unknown backoff policy type " + bulkProcessorConfig.getFlushBackoffType());
            }
            builder.setBackoffPolicy(backoffPolicy);
            return builder;
        }
    };
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) BulkProcessor(org.elasticsearch.action.bulk.BulkProcessor) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) TimeValue(org.elasticsearch.core.TimeValue)

Example 8 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project flink by apache.

the class Elasticsearch7ApiCallBridge method createClient.

@Override
public RestHighLevelClient createClient(Map<String, String> clientConfig) {
    RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
    restClientFactory.configureRestClientBuilder(builder);
    RestHighLevelClient rhlClient = new RestHighLevelClient(builder);
    return rhlClient;
}
Also used : HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 9 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project flink by apache.

the class Elasticsearch6ApiCallBridge method createClient.

@Override
public RestHighLevelClient createClient(Map<String, String> clientConfig) {
    RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
    restClientFactory.configureRestClientBuilder(builder);
    RestHighLevelClient rhlClient = new RestHighLevelClient(builder);
    return rhlClient;
}
Also used : HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 10 with RestHighLevelClient

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestHighLevelClient in project beam by apache.

the class HadoopFormatIOElasticIT method prepareElasticIndex.

private static void prepareElasticIndex() throws IOException {
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(options.getElasticServerIp(), options.getElasticServerPort(), "http")));
    for (int i = 0; i < 1000; i++) {
        IndexRequest request = new IndexRequest(ELASTIC_INDEX_NAME).source(createElasticRow(i));
        client.index(request, RequestOptions.DEFAULT);
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)56 HttpHost (org.apache.http.HttpHost)22 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)19 IOException (java.io.IOException)12 RestClient (org.elasticsearch.client.RestClient)11 HashMap (java.util.HashMap)9 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 Test (org.junit.jupiter.api.Test)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 RequestOptions (org.elasticsearch.client.RequestOptions)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 Test (org.junit.Test)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)4