Java Code Examples for org.elasticsearch.client.Client#close()

The following examples show how to use org.elasticsearch.client.Client#close() . 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: ElasticsearchSinkTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the Elasticsearch sink works properly.
 */
public void runElasticsearchSinkTest() throws Exception {
	final String index = "elasticsearch-sink-test-index";

	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStreamSource<Tuple2<Integer, String>> source = env.addSource(new SourceSinkDataTestKit.TestDataSourceFunction());

	source.addSink(createElasticsearchSinkForEmbeddedNode(
			1,
			CLUSTER_NAME,
			new SourceSinkDataTestKit.TestElasticsearchSinkFunction(index)));

	env.execute("Elasticsearch Sink Test");

	// verify the results
	Client client = embeddedNodeEnv.getClient();
	SourceSinkDataTestKit.verifyProducedSinkData(client, index);

	client.close();
}
 
Example 2
Source File: ElasticsearchSinkTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the Elasticsearch sink works properly.
 */
public void runElasticsearchSinkTest() throws Exception {
	final String index = "elasticsearch-sink-test-index";

	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStreamSource<Tuple2<Integer, String>> source = env.addSource(new SourceSinkDataTestKit.TestDataSourceFunction());

	source.addSink(createElasticsearchSinkForEmbeddedNode(
			1,
			CLUSTER_NAME,
			new SourceSinkDataTestKit.TestElasticsearchSinkFunction(index)));

	env.execute("Elasticsearch Sink Test");

	// verify the results
	Client client = embeddedNodeEnv.getClient();
	SourceSinkDataTestKit.verifyProducedSinkData(client, index);

	client.close();
}
 
Example 3
Source File: ElasticsearchSinkTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
private void runElasticSearchSinkTest(String index, Function<String, ElasticsearchSinkFunction<Tuple2<Integer, String>>> functionFactory) throws Exception {
	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStreamSource<Tuple2<Integer, String>> source = env.addSource(new SourceSinkDataTestKit.TestDataSourceFunction());

	source.addSink(createElasticsearchSinkForEmbeddedNode(
			1,
			CLUSTER_NAME,
			functionFactory.apply(index)));

	env.execute("Elasticsearch Sink Test");

	// verify the results
	Client client = elasticsearchResource.getClient();
	SourceSinkDataTestKit.verifyProducedSinkData(client, index);

	client.close();
}
 
Example 4
Source File: CaseController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@GetMapping("/elasticsearch")
public String elasticsearch() throws Exception {
    Client client = initTransportClient();
    String indexName = UUID.randomUUID().toString();
    try {
        // create
        index(client, indexName);
        // get
        get(client, indexName);
        // search
        search(client, indexName);
        // update
        update(client, indexName);
        // delete
        delete(client, indexName);
        // remove index
        client.admin().indices().prepareDelete(indexName).execute();
    } finally {
        if (null != client) {
            client.close();
        }
    }
    return "Success";
}
 
Example 5
Source File: AbstractNodeTest.java    From elasticsearch-gatherer with Apache License 2.0 5 votes vote down vote up
public void closeAllNodes() {
    for (Client client : clients.values()) {
        client.close();
    }
    clients.clear();
    for (Node node : nodes.values()) {
        node.close();
    }
    nodes.clear();
}
 
Example 6
Source File: ElasticsearchSinkITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that behaviour of the deprecated {@link IndexRequestBuilder} constructor works properly.
 */
@Test
public void testDeprecatedIndexRequestBuilderVariant() throws Exception {
	final String index = "index-req-builder-test-index";

	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStreamSource<Tuple2<Integer, String>> source = env.addSource(new SourceSinkDataTestKit.TestDataSourceFunction());

	Map<String, String> userConfig = new HashMap<>();
	// This instructs the sink to emit after every element, otherwise they would be buffered
	userConfig.put(ElasticsearchSinkBase.CONFIG_KEY_BULK_FLUSH_MAX_ACTIONS, "1");
	userConfig.put("cluster.name", CLUSTER_NAME);
	userConfig.put("node.local", "true");

	List<TransportAddress> transports = new ArrayList<>();
	transports.add(new LocalTransportAddress("1"));

	source.addSink(new ElasticsearchSink<>(
		userConfig,
		transports,
		new TestIndexRequestBuilder(index))
	);

	env.execute("Elasticsearch Deprecated IndexRequestBuilder Bridge Test");

	// verify the results
	Client client = embeddedNodeEnv.getClient();
	SourceSinkDataTestKit.verifyProducedSinkData(client, index);

	client.close();
}
 
Example 7
Source File: AbstractNodeTest.java    From elasticsearch-gatherer with Apache License 2.0 5 votes vote down vote up
public void stopNode(String id) {
    Client client = clients.remove(id);
    if (client != null) {
        client.close();
    }
    Node node = nodes.remove(id);
    if (node != null) {
        node.close();
    }
}
 
Example 8
Source File: SearchClientServiceMockImpl.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
private void closeAllNodes() {
	for (final Client client : clients.values()) {
		client.close();
	}
	clients.clear();
	for (final Node node : nodes.values()) {
		node.close();
	}
	nodes.clear();
}
 
Example 9
Source File: SearchClientServiceMockImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
public void closeAllNodes() {
    for (Client client : clients.values()) {
        client.close();
    }
    clients.clear();
    for (Node node : nodes.values()) {
        node.close();
    }
    nodes.clear();
}
 
Example 10
Source File: SearchClientServiceMockImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
public void closeNode(String id) {
    Client client = clients.remove(id);
    if (client != null) {
        client.close();
    }
    Node node = nodes.remove(id);
    if (node != null) {
        node.close();
    }
}
 
Example 11
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@GetMapping("/healthcheck")
public String healthcheck() throws Exception {
    Client client = initTransportClient();
    ClusterHealthResponse response = client.admin().cluster().prepareHealth().setWaitForYellowStatus().get();
    if (response.isTimedOut()) {
        String message = "elastic search node start fail!";
        logger.error(message);
        throw new RuntimeException(message);
    }
    client.close();
    return "Success";
}
 
Example 12
Source File: ElasticSearchClientProducerTest.java    From elasticsearch-reindex-tool with Apache License 2.0 5 votes vote down vote up
@Test
public void validateCreatedLocalElasticClientWithSniff() throws Exception {
  //given
  ElasticDataPointer dataPointer = ElasticDataPointerBuilder.builder()
      .setAddress("http://localhost:9300/"+INDEX_NAME+"/type")
      .setClusterName(CLUSTER_NAME)
      .setSniff(true)
      .build();
  //when
  Client client = ElasticSearchClientFactory.createClient(dataPointer);
  //then
  Assertions.assertThat(client.settings().get("cluster.name")).isEqualTo(CLUSTER_NAME);
  client.close();
}
 
Example 13
Source File: ElasticSearchClientProducerTest.java    From elasticsearch-reindex-tool with Apache License 2.0 5 votes vote down vote up
@Test
public void validateCreatedLocalElasticClientWithoutSniff() throws Exception {
  //given
  ElasticDataPointer dataPointer = ElasticDataPointerBuilder.builder()
      .setAddress("http://localhost:9300/"+INDEX_NAME+"/type")
      .setClusterName(CLUSTER_NAME)
      .setSniff(false)
      .build();
  //when
  Client client = ElasticSearchClientFactory.createClient(dataPointer);
  //then
  Assertions.assertThat(client.settings().get("cluster.name")).isEqualTo(CLUSTER_NAME);
  client.close();
}
 
Example 14
Source File: ElasticSearchClientProducerTest.java    From elasticsearch-reindex-tool with Apache License 2.0 5 votes vote down vote up
@Test
public void validateCreatedLocalElasticClientWithProperClusterName() throws Exception {
  //given
  ElasticDataPointer dataPointer = ElasticDataPointerBuilder.builder()
      .setAddress("http://localhost:9300/"+INDEX_NAME+"/type")
      .setClusterName(CLUSTER_NAME)
      .build();
  //when
  Client client = ElasticSearchClientFactory.createClient(dataPointer);
  //then
  Assertions.assertThat(client.settings().get("cluster.name")).isEqualTo(CLUSTER_NAME);
  client.close();
}
 
Example 15
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void shutDown() throws IOException {
	Client toCloseClient = client;
	client = null;
	if (toCloseClient != null) {
		toCloseClient.close();
	}
}
 
Example 16
Source File: ClientProviderWithDebugStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
synchronized public void close() throws Exception {
	if (!closed) {
		closed = true;
		if (client != null) {
			Client temp = client;
			client = null;
			temp.close();
		}
	}
}
 
Example 17
Source File: SingletonClientProvider.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
synchronized public void close() throws Exception {
	if (!closed) {
		closed = true;
		if (client != null) {
			Client temp = client;
			client = null;
			temp.close();
		}
	}
}
 
Example 18
Source File: AbstractElasticsearch5TransportClientProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Dispose of ElasticSearch client
 */
public void closeClient() {
    Client client = esClient.get();
    if (client != null) {
        getLogger().info("Closing ElasticSearch Client");
        esClient.set(null);
        client.close();
    }
}
 
Example 19
Source File: EsClientHelper.java    From jlogstash-input-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 释放ES Client
 *
 * @param client
 */
public static void releaseClient(Client client) {
    if (client != null) {
        client.close();
    }
}
 
Example 20
Source File: ElasticSearch.java    From hsweb-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {


        Client client = EsClient.getClient();

//      CreateIndex(client);

//      GetResponse(client);

//      DeleteResponse(client);

//      UpdateRequest(client);
        client.close();

//      MultiGet(client);

//      BulkIndex(client);

//        SearchIndex(client);


    }