public void run() { Client client = null; TransportClient transportClient = null; try { transportClient = new TransportClient(); client = transportClient.addTransportAddress(new InetSocketTransportAddress("192.168.1.40", 9300)); SearchResponse response = client .prepareSearch("tms-allflat") .setTypes("personal") .setQuery(QueryBuilders.matchAllQuery()) .addAggregation( AggregationBuilders.terms("aggs1") .field("skill_1") .size(20) .order(Terms.Order.count(false))) .execute() .actionGet(); Terms terms = response.getAggregations().get("aggs1"); terms .getBuckets() .stream() .forEach(s -> System.out.println(s.getKeyAsText() + "(" + s.getDocCount() + ")")); } finally { transportClient.close(); client.close(); } }
@Test public void testWithSniffing() throws Exception { TransportClient client = new TransportClient( ImmutableSettings.builder() .put("client.transport.sniff", true) .put("cluster.name", "cluster1") .put("node.name", "transport_client_" + this.getTestName() + "_1") .put("client.transport.nodes_sampler_interval", "1s") .put( TransportModule.TRANSPORT_SERVICE_TYPE_KEY, InternalTransportService.class.getName()) .put(HEADER_SETTINGS) .build()); try { client.addTransportAddress(address); InternalTransportService service = (InternalTransportService) client.injector.getInstance(TransportService.class); if (!service.clusterStateLatch.await(5, TimeUnit.SECONDS)) { fail("takes way too long to get the cluster state"); } assertThat(client.connectedNodes().size(), is(1)); assertThat(client.connectedNodes().get(0).getAddress(), is((TransportAddress) address)); } finally { client.close(); } }
@Override public void destroy() throws Exception { try { logger.info("Closing elasticSearch client"); if (client != null) { client.close(); } } catch (final Exception e) { logger.error("Error closing ElasticSearch client: ", e); } }
synchronized void stop() throws InterruptedException { if (running()) { try { if (this.client != null) { client.close(); } } finally { process.destroy(); process.waitFor(); process = null; nodeInfo = null; } } }
public static void main(String[] args) { TransportClient client = new TransportClient(); client.addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); TestBulkProcessor test = new TestBulkProcessor(client); int count = 0; int processors = Runtime.getRuntime().availableProcessors() - 1; while (count++ < 20) { int count2 = 0; while (count2++ < 3) { test.testBulkProcessor(processors); } processors++; } client.close(); }
/* * (non-Javadoc) * * @see com.datatorrent.lib.db.Connectable#disconnect() */ @Override public void disconnect() throws IOException { if (client != null) { client.close(); } }