Java Code Examples for org.elasticsearch.client.transport.TransportClient#addTransportAddress()

The following examples show how to use org.elasticsearch.client.transport.TransportClient#addTransportAddress() . 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: SearchClientServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
protected Client createClient() {
	if (client == null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Creating client for Search!");
		}
		// Try starting search client at context loading
		try {
			final Settings settings = ImmutableSettings
					.settingsBuilder()
					.put(ElasticSearchReservedWords.CLUSTER_NAME.getText(),
							searchServerClusterName).build();
			TransportClient transportClient = new TransportClient(settings);
			transportClient = transportClient
					.addTransportAddress(new InetSocketTransportAddress(
							"localhost", 9300));
			if (transportClient.connectedNodes().size() == 0) {
				logger.error("There are no active nodes available for the transport, it will be automatically added once nodes are live!");
			}
			client = transportClient;
		} catch (final Exception ex) {
			// ignore any exception, dont want to stop context loading
			logger.error("Error occured while creating search client!", ex);
		}
	}
	return client;
}
 
Example 2
Source File: ElasticSearchTransportClientProvider.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public Client get() {

    Settings settings = Settings.builder()
            .put("client.transport.ignore_cluster_name", true)
            .put("client.transport.sniff", true)
            .build();

    TransportClient tc = new PreBuiltTransportClient(settings);

    List<URI> clusterAddresses = configuration.getURIs();

    if (clusterAddresses.isEmpty()) {
        logger.warn(ElasticSearchConfiguration.ELASTIC_SEARCH_URL_PROPERTY_NAME +
                " is not set.  Indexing will remain DISABLED.");
    }
    for (URI hostAddress : clusterAddresses) {
        int port = Optional.ofNullable(hostAddress.getPort()).orElse(9200);
        try {
            tc.addTransportAddress(new TransportAddress(InetAddress.getByName(hostAddress.getHost()), port));
        } catch (Exception e) {
            throw new ProvisionException("Invalid host" + hostAddress.getHost(), e);
        }
    }
    return tc;
}
 
Example 3
Source File: ElasticSearchConfiguration.java    From Decision with Apache License 2.0 6 votes vote down vote up
@Bean
public Client elasticsearchClient(){

    Settings settings = ImmutableSettings.settingsBuilder()
            .put("client.transport.ignore_cluster_name", true)
            .put("cluster.name", "elasticsearch")
            .build();
    TransportClient tc = new TransportClient(settings);

    for (String elasticSearchHost : configurationContext.getElasticSearchHosts()) {
        String[] elements = elasticSearchHost.split(":");
        tc.addTransportAddress(new InetSocketTransportAddress(elements[0], Integer.parseInt(elements[1])));
    }


    log.debug("Creating Spring Bean for elasticsearchClient");

    return  tc;

}
 
Example 4
Source File: Elasticsearch2Module.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public Client get()
{
    try {
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();
        TransportClient client = TransportClient.builder().settings(settings).build();
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        LOG.info("Connection to instance %s at %s established, user %s");
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example 5
Source File: EsUpdateSettingsTest.java    From io with Apache License 2.0 5 votes vote down vote up
private TransportClient createTransportClient() {
    Settings sts = Settings.settingsBuilder()
    		.put("path.home", ".")
            .put("cluster.name", "testingCluster").build();
    TransportClient client = TransportClient.builder().settings(sts).build();
    client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("localhost", 9399)));
    return client;
}
 
Example 6
Source File: EsProvider.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 * Create the transport client
 * @return
 */
private Client createTransportClient() {
    final String clusterName = indexFig.getClusterName();
    final int port = indexFig.getPort();

    ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put( "cluster.name", clusterName )
        .put( "client.transport.sniff", true );

    String nodeName = indexFig.getNodeName();

    if ( "default".equals( nodeName ) ) {
        // no nodeName was specified, use hostname
        try {
            nodeName = InetAddress.getLocalHost().getHostName();
        }
        catch ( UnknownHostException ex ) {
            nodeName = "client-" + RandomStringUtils.randomAlphabetic( 8 );
            logger.warn( "Couldn't get hostname to use as ES node name, using {}", nodeName );
        }
    }

    settings.put( "node.name", nodeName );


    TransportClient transportClient = new TransportClient( settings.build() );

    // we will connect to ES on all configured hosts
    for ( String host : indexFig.getHosts().split( "," ) ) {
        transportClient.addTransportAddress( new InetSocketTransportAddress( host, port ) );
    }

    return transportClient;
}
 
Example 7
Source File: ElasticSearchSink.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
private void openClient() {
  Settings settings = ImmutableSettings.settingsBuilder()
      .put("cluster.name", clusterName).build();

  TransportClient transport = new TransportClient(settings);
  for (InetSocketTransportAddress host : serverAddresses) {
    transport.addTransportAddress(host);
  }
  client = transport;
}
 
Example 8
Source File: BulkProcessorObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void clientIsCalledWhenBatchItemIsAdded() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<TransportClient, BulkRequest> config = spy(builder.build());

    Settings settings = Settings.builder().build();
    TransportClient client = spy(new PreBuiltTransportClient(settings));
    client.addTransportAddress(new LocalTransportAddress("1"));
    when(config.createClient()).thenReturn(client);

    FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());

    BulkProcessorFactory bulkProcessorFactory = new BulkProcessorFactory();
    BatchEmitter batchEmitter = bulkProcessorFactory.createInstance(
                    1,
                    100,
                    config,
                    failoverPolicy);

    String payload1 = "test1";
    ActionRequest testRequest = createTestRequest(payload1);

    // when
    batchEmitter.add(testRequest);

    // then
    ArgumentCaptor<BulkRequest> captor = ArgumentCaptor.forClass(BulkRequest.class);
    verify(client, times(1)).bulk(captor.capture(), Mockito.any());

    assertEquals(payload1, new BulkRequestIntrospector().items(captor.getValue()).iterator().next());
}
 
Example 9
Source File: BulkProcessorObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void failoverIsExecutedAfterNonSuccessfulRequest() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<TransportClient, BulkRequest> config = spy(builder.build());

    FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());
    Function handler = spy(config.createFailureHandler(failoverPolicy));
    when(config.createFailureHandler(any())).thenReturn(handler);

    Settings settings = Settings.builder().build();
    TransportClient client = spy(new PreBuiltTransportClient(settings));
    client.addTransportAddress(new TransportAddress(new InetSocketAddress(9999)));
    when(config.createClient()).thenReturn(client);

    BulkProcessorFactory bulkProcessorFactory = new BulkProcessorFactory();
    BatchEmitter batchEmitter = bulkProcessorFactory.createInstance(
            1,
            100,
            config,
            failoverPolicy);

    String payload1 = "test1";
    ActionRequest testRequest = createTestRequest(payload1);

    // when
    batchEmitter.add(testRequest);

    // then
    ArgumentCaptor<BulkRequest> captor = ArgumentCaptor.forClass(BulkRequest.class);
    verify(handler, times(1)).apply(captor.capture());

    assertEquals(payload1, new BulkRequestIntrospector().items(captor.getValue()).iterator().next());
}
 
Example 10
Source File: Elasticsearch5ApiCallBridge.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TransportClient createClient(Map<String, String> clientConfig) {
	Settings settings = Settings.builder()
		.put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME)
		.put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME)
		.put(clientConfig)
		.build();

	TransportClient transportClient = new PreBuiltTransportClient(settings);
	for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) {
		transportClient.addTransportAddress(transport);
	}

	return transportClient;
}
 
Example 11
Source File: IndexService.java    From disthene with MIT License 5 votes vote down vote up
public IndexService(IndexConfiguration indexConfiguration, MBassador<DistheneEvent> bus) {
    this.indexConfiguration = indexConfiguration;

    bus.subscribe(this);

    Settings settings = ImmutableSettings.settingsBuilder()
            .put("cluster.name", indexConfiguration.getName())
            .build();
    client = new TransportClient(settings);
    for (String node : indexConfiguration.getCluster()) {
        client.addTransportAddress(new InetSocketTransportAddress(node, indexConfiguration.getPort()));
    }

    indexThread = new IndexThread(
            "distheneIndexThread",
            client,
            metrics,
            indexConfiguration.getIndex(),
            indexConfiguration.getType(),
            indexConfiguration.getBulk().getActions(),
            indexConfiguration.getBulk().getInterval()
    );

    indexThread.start();

    if (indexConfiguration.isCache()) {
        scheduler.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                expireCache();
            }
        }, indexConfiguration.getExpire(), indexConfiguration.getExpire(), TimeUnit.SECONDS);
    }
}
 
Example 12
Source File: BulkProcessorObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void clientIsCalledWhenBatchItemIsAdded() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<TransportClient, BulkRequest> config = spy(builder.build());

    Settings settings = Settings.builder().build();
    TransportClient client = spy(new PreBuiltTransportClient(settings));
    client.addTransportAddress(new TransportAddress(new InetSocketAddress(9999)));
    when(config.createClient()).thenReturn(client);

    FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());

    BulkProcessorFactory bulkProcessorFactory = new BulkProcessorFactory();
    BatchEmitter batchEmitter = bulkProcessorFactory.createInstance(
                    1,
                    100,
                    config,
                    failoverPolicy);

    String payload1 = "test1";
    ActionRequest testRequest = createTestRequest(payload1);

    // when
    batchEmitter.add(testRequest);

    // then
    ArgumentCaptor<BulkRequest> captor = ArgumentCaptor.forClass(BulkRequest.class);
    verify(client, times(1)).bulk(captor.capture(), Mockito.any());

    assertEquals(payload1, new BulkRequestIntrospector().items(captor.getValue()).iterator().next());
}
 
Example 13
Source File: Elasticsearch5Sink.java    From sylph with Apache License 2.0 5 votes vote down vote up
@Override
public boolean open(long partitionId, long version)
        throws Exception
{
    TransportClient client = clientFactory.createClient();
    for (String ip : config.getHosts().split(",")) {
        client.addTransportAddress(new InetSocketTransportAddress(
                InetAddress.getByName(ip.split(":")[0]), Integer.valueOf(ip.split(":")[1])));
    }
    this.client = client;
    this.bulkBuilder = client.prepareBulk();
    return true;
}
 
Example 14
Source File: SearchClientServiceImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
protected Client createClient()
{
    if(client == null)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("Creating client for Search!");
        }            
        //Try starting search client at context loading
        try
        {
            Settings settings = ImmutableSettings.settingsBuilder().put(ElasticSearchReservedWords.CLUSTER_NAME.getText(), searchServerClusterName).build();
            
            TransportClient transportClient = new TransportClient(settings);

            transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
            
            if(transportClient.connectedNodes().size() == 0)
            {
                logger.error("There are no active nodes available for the transport, it will be automatically added once nodes are live!");
            }
            client = transportClient;
        }
        catch(Exception ex)
        {
            //ignore any exception, dont want to stop context loading
            logger.error("Error occured while creating search client!", ex);
        }
    }
    return client;
}
 
Example 15
Source File: Elasticsearch5ApiCallBridge.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TransportClient createClient(Map<String, String> clientConfig) {
	Settings settings = Settings.builder().put(clientConfig)
		.put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME)
		.put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME)
		.build();

	TransportClient transportClient = new PreBuiltTransportClient(settings);
	for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) {
		transportClient.addTransportAddress(transport);
	}

	// verify that we actually are connected to a cluster
	if (transportClient.connectedNodes().isEmpty()) {

		// close the transportClient here
		IOUtils.closeQuietly(transportClient);

		throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!");
	}

	if (LOG.isInfoEnabled()) {
		LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes());
	}

	return transportClient;
}
 
Example 16
Source File: ElasticSearchConnectable.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void connect() throws IOException
{
  client = new TransportClient();
  client.addTransportAddress(new InetSocketTransportAddress(hostName, port));
}
 
Example 17
Source File: DoTestESClient.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);

        client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9300)));

        IndexRequestBuilder irb = client.prepareIndex("uav_test_db", "uav_test_table");

        Map<String, Object> item = new HashMap<String, Object>();

        item.put("name", "zz");
        item.put("age", 1);

        irb.setSource(item);

        IndexResponse ir = irb.get();

        System.out.println(ir.status());

        client.close();
    }
 
Example 18
Source File: ClientFactory.java    From storm-trident-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public TransportClient makeClient(Map conf) {
    TransportClient client = new TransportClient(buildSettings());
    client.addTransportAddress(new LocalTransportAddress("1"));
    return client;
}
 
Example 19
Source File: SearchClientServiceImpl.java    From elasticsearch-tutorial with MIT License 4 votes vote down vote up
public void addNewNode(String name)
{
    TransportClient transportClient = (TransportClient) client;
    transportClient.addTransportAddress(new InetSocketTransportAddress(name, 9300));
}
 
Example 20
Source File: BaseElasticSearchSink.java    From FlinkExperiments with MIT License 4 votes vote down vote up
private TransportClient createClient() throws Exception {

        // Create a new Connection:
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);

        client.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));

        // Ensure we have connected nodes:
        List<DiscoveryNode> nodes = client.connectedNodes();

        if (nodes.isEmpty()) {
            throw new RuntimeException("Client is not connected to any Elasticsearch nodes!");
        }

        return client;
    }