org.elasticsearch.action.bulk.BulkAction Java Examples

The following examples show how to use org.elasticsearch.action.bulk.BulkAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpInvoker.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
public HttpInvoker(Settings settings, ThreadPool threadPool, Headers headers, URL url) {
    super(settings, threadPool, headers);
    this.contexts = new HashMap<>();
    this.bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool()));
    bootstrap.setPipelineFactory(new HttpInvoker.HttpClientPipelineFactory());
    bootstrap.setOption("tcpNoDelay", true);

    registerAction(BulkAction.INSTANCE, HttpBulkAction.class);
    registerAction(CreateIndexAction.INSTANCE, HttpCreateIndexAction.class);
    registerAction(RefreshAction.INSTANCE, HttpRefreshIndexAction.class);
    registerAction(ClusterUpdateSettingsAction.INSTANCE, HttpClusterUpdateSettingsAction.class);
    registerAction(UpdateSettingsAction.INSTANCE, HttpUpdateSettingsAction.class);
    registerAction(SearchAction.INSTANCE, HttpSearchAction.class);

    this.url = url;
}
 
Example #2
Source File: TestPutElasticsearch.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void createElasticsearchClient(ProcessContext context) throws ProcessException {
    final Client mockClient = mock(Client.class);
    BulkRequestBuilder bulkRequestBuilder = spy(new BulkRequestBuilder(mockClient, BulkAction.INSTANCE));
    if (exceptionToThrow != null) {
        doThrow(exceptionToThrow).when(bulkRequestBuilder).execute();
    } else {
        doReturn(new MockBulkRequestBuilderExecutor(responseHasFailures)).when(bulkRequestBuilder).execute();
    }
    when(mockClient.prepareBulk()).thenReturn(bulkRequestBuilder);

    when(mockClient.prepareIndex(anyString(), anyString(), anyString())).thenAnswer(new Answer<IndexRequestBuilder>() {
        @Override
        public IndexRequestBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String arg1 = (String) args[0];
            if (arg1.isEmpty()) {
                throw new NoNodeAvailableException("Needs index");
            }
            String arg2 = (String) args[1];
            if (arg2.isEmpty()) {
                throw new NoNodeAvailableException("Needs doc type");
            } else {
                IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(mockClient, IndexAction.INSTANCE);
                return indexRequestBuilder;
            }
        }
    });

    esClient.set(mockClient);
}
 
Example #3
Source File: TestPutElasticsearch5.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Client getTransportClient(Settings.Builder settingsBuilder, String xPackPath,
                                    String username, String password,
                                    List<InetSocketAddress> esHosts, ComponentLog log)
        throws MalformedURLException {
    final Client mockClient = mock(Client.class);
    BulkRequestBuilder bulkRequestBuilder = spy(new BulkRequestBuilder(mockClient, BulkAction.INSTANCE));
    if (exceptionToThrow != null) {
        doThrow(exceptionToThrow).when(bulkRequestBuilder).execute();
    } else {
        doReturn(new MockBulkRequestBuilderExecutor(responseHasFailures, esHosts.get(0))).when(bulkRequestBuilder).execute();
    }
    when(mockClient.prepareBulk()).thenReturn(bulkRequestBuilder);

    when(mockClient.prepareIndex(anyString(), anyString(), anyString())).thenAnswer(new Answer<IndexRequestBuilder>() {
        @Override
        public IndexRequestBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String arg1 = (String) args[0];
            if (arg1.isEmpty()) {
                throw new NoNodeAvailableException("Needs index");
            }
            String arg2 = (String) args[1];
            if (arg2.isEmpty()) {
                throw new NoNodeAvailableException("Needs doc type");
            } else {
                IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(mockClient, IndexAction.INSTANCE);
                return indexRequestBuilder;
            }
        }
    });

    return mockClient;
}
 
Example #4
Source File: TestPutElasticsearch.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void createElasticsearchClient(ProcessContext context) throws ProcessException {
    final Client mockClient = mock(Client.class);
    BulkRequestBuilder bulkRequestBuilder = spy(new BulkRequestBuilder(mockClient, BulkAction.INSTANCE));
    if (exceptionToThrow != null) {
        doThrow(exceptionToThrow).when(bulkRequestBuilder).execute();
    } else {
        doReturn(new MockBulkRequestBuilderExecutor(responseHasFailures)).when(bulkRequestBuilder).execute();
    }
    when(mockClient.prepareBulk()).thenReturn(bulkRequestBuilder);

    when(mockClient.prepareIndex(anyString(), anyString(), anyString())).thenAnswer(new Answer<IndexRequestBuilder>() {
        @Override
        public IndexRequestBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String arg1 = (String) args[0];
            if (arg1.isEmpty()) {
                throw new NoNodeAvailableException("Needs index");
            }
            String arg2 = (String) args[1];
            if (arg2.isEmpty()) {
                throw new NoNodeAvailableException("Needs doc type");
            } else {
                IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(mockClient, IndexAction.INSTANCE);
                return indexRequestBuilder;
            }
        }
    });

    esClient.set(mockClient);
}
 
Example #5
Source File: TestPutElasticsearch5.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Client getTransportClient(Settings.Builder settingsBuilder, String xPackPath,
                                    String username, String password,
                                    List<InetSocketAddress> esHosts, ComponentLog log)
        throws MalformedURLException {
    final Client mockClient = mock(Client.class);
    BulkRequestBuilder bulkRequestBuilder = spy(new BulkRequestBuilder(mockClient, BulkAction.INSTANCE));
    if (exceptionToThrow != null) {
        doThrow(exceptionToThrow).when(bulkRequestBuilder).execute();
    } else {
        doReturn(new MockBulkRequestBuilderExecutor(responseHasFailures, esHosts.get(0))).when(bulkRequestBuilder).execute();
    }
    when(mockClient.prepareBulk()).thenReturn(bulkRequestBuilder);

    when(mockClient.prepareIndex(anyString(), anyString(), anyString())).thenAnswer(new Answer<IndexRequestBuilder>() {
        @Override
        public IndexRequestBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String arg1 = (String) args[0];
            if (arg1.isEmpty()) {
                throw new NoNodeAvailableException("Needs index");
            }
            String arg2 = (String) args[1];
            if (arg2.isEmpty()) {
                throw new NoNodeAvailableException("Needs doc type");
            } else {
                IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(mockClient, IndexAction.INSTANCE);
                return indexRequestBuilder;
            }
        }
    });

    return mockClient;
}
 
Example #6
Source File: BulkProcessor.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
public void execute(BulkRequest bulkRequest, long executionId) {
    boolean afterCalled = false;
    try {
        listener.beforeBulk(executionId, bulkRequest);
        BulkResponse bulkResponse = client.execute(BulkAction.INSTANCE, bulkRequest).actionGet();
        afterCalled = true;
        listener.afterBulk(executionId, bulkRequest, bulkResponse);
    } catch (Throwable t) {
        if (!afterCalled) {
            listener.afterBulk(executionId, bulkRequest, t);
        }
    }
}
 
Example #7
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<BulkResponse> bulk(final BulkRequest request) {
    return execute(BulkAction.INSTANCE, request);
}
 
Example #8
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void bulk(final BulkRequest request, final ActionListener<BulkResponse> listener) {
    execute(BulkAction.INSTANCE, request, listener);
}
 
Example #9
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public BulkRequestBuilder prepareBulk() {
    return new BulkRequestBuilder(this, BulkAction.INSTANCE);
}
 
Example #10
Source File: SearchTest.java    From elasticsearch-helper with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearch() throws Exception {
    Client client = client("1");
    long t0 = System.currentTimeMillis();
    BulkRequestBuilder builder = new BulkRequestBuilder(client, BulkAction.INSTANCE);
    for (int i = 0; i < 1000; i++) {
        builder.add(indexRequest()
                .index("pages").type("row")
                .source(jsonBuilder()
                        .startObject()
                        .field("user1", "kimchy")
                        .field("user2", "kimchy")
                        .field("user3", "kimchy")
                        .field("user4", "kimchy")
                        .field("user5", "kimchy")
                        .field("user6", "kimchy")
                        .field("user7", "kimchy")
                        .field("user8", "kimchy")
                        .field("user9", "kimchy")
                        .field("rowcount", i)
                        .field("rs", 1234)));
    }
    client.bulk(builder.request()).actionGet();

    client.admin().indices().refresh(refreshRequest()).actionGet();

    long t1 = System.currentTimeMillis();
    logger.info("t1-t0 = {}", t1-t0);

    for (int i = 0; i < 100; i++) {
        t1 = System.currentTimeMillis();
        QueryBuilder queryStringBuilder =
                QueryBuilders.queryStringQuery("rs:" + 1234);
        SearchRequestBuilder requestBuilder = client.prepareSearch()
                .setIndices("pages")
                .setTypes("row")
                .setQuery(queryStringBuilder)
                .addSort("rowcount", SortOrder.DESC)
                .setFrom(i*10).setSize(10);
        SearchResponse response = requestBuilder.execute().actionGet();
        long t2 = System.currentTimeMillis();
        logger.info("t2-t1 = {}", t2-t1);
    }
}