org.elasticsearch.transport.client.PreBuiltTransportClient Java Examples

The following examples show how to use org.elasticsearch.transport.client.PreBuiltTransportClient. 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: ElasticsearchGis.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 7 votes vote down vote up
@SuppressWarnings({"unchecked", "resource"})
public static void main(String[] args) throws Exception {
	// 先构建client,两个参数分别是:cluster.name 固定参数代表后面参数的含义,集群名称
	// client.transport.sniff 表示设置自动探查集群的集群节点
	Settings settings = Settings.builder()
			.put("cluster.name", "youmeek-cluster")
			.put("client.transport.sniff", true)
			.build();

	//单个节点的写法
	TransportClient transportClient = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.127"), 9300));

	//batchCreate(transportClient);
	query(transportClient);

	transportClient.close();
}
 
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 InetSocketTransportAddress(InetAddress.getByName(hostAddress.getHost()), port));
        } catch (UnknownHostException uhe){
            throw new ProvisionException("Invalid host" + hostAddress.getHost(), uhe);
        }
    }
    return tc;
}
 
Example #3
Source File: ElasticsearchContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void transportClientClusterHealth() {
    // transportClientContainer {
    // Create the elasticsearch container.
    try (ElasticsearchContainer container = new ElasticsearchContainer()) {
        // Start the container. This step might take some time...
        container.start();

        // Do whatever you want with the transport client
        TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
        String expectedClusterName = "docker-cluster";
        Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
        try (TransportClient transportClient = new PreBuiltTransportClient(settings)
            .addTransportAddress(transportAddress)) {
            ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
            String clusterName = healths.getClusterName();
            // }}}
            assertThat(clusterName, is(expectedClusterName));
            // transportClientContainer {{{
        }
    }
    // }
}
 
Example #4
Source File: Elasticsearch5Module.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 = new PreBuiltTransportClient(settings);
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #5
Source File: CaseController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private Client initTransportClient() throws UnknownHostException {
    TransportClient client = null;
    try {
        Settings settings = Settings.builder()
                                    .put("cluster.name", "docker-node")
                                    .put("client.transport.sniff", false)
                                    .build();

        client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress
            .getByName(host), 9300));
    } catch (UnknownHostException e) {
        logger.error("create client error", e);
        throw e;
    }
    return client;
}
 
Example #6
Source File: DefaultMahutaTest.java    From Mahuta with Apache License 2.0 6 votes vote down vote up
@Test
public void connectViaTransportClient() throws Exception {

    String host = ContainerUtils.getHost("elasticsearch");
    Integer port = ContainerUtils.getPort("elasticsearch");
    String clusterName = ContainerUtils.getConfig("elasticsearch", "cluster-name");
            
    PreBuiltTransportClient pbtc = new PreBuiltTransportClient(
            Settings.builder().put("cluster.name", clusterName).build());
    TransportClient transportClient = pbtc
            .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
    
    IndexingService esService = ElasticSearchService.connect(transportClient).withIndex("test");
    
    assertNotNull( esService.getIndexes());
}
 
Example #7
Source File: ElasticSearchService.java    From Mahuta with Apache License 2.0 6 votes vote down vote up
public static ElasticSearchService connect(String host, Integer port, String clusterName) {
    ElasticSearchSettings settings = ElasticSearchSettings.of(host, port, clusterName);

    try {
        // WARNING pbtc is never closed -> Close the transportClient as well...
        PreBuiltTransportClient pbtc = new PreBuiltTransportClient(
                Settings.builder().put("cluster.name", clusterName).build());
        TransportClient transportClient = pbtc
                .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));

        log.info("Connected to ElasticSearch [host: {}, port: {}, cluster: {}] : {}", host, port, clusterName,
                transportClient.listedNodes().toString());

        return new ElasticSearchService(settings, transportClient);

    } catch (UnknownHostException ex) {
        log.error("Error while connecting to ElasticSearch [host: {}, port: {}, cluster: {}]", host, port,
                clusterName, ex);
        throw new ConnectionException("Error whilst connecting to ElasticSearch", ex);
    }
}
 
Example #8
Source File: ElasticSearchConfig.java    From cloud-service with MIT License 6 votes vote down vote up
/**
    * 使用elasticsearch实现类时才触发
    *
    * @return
    */
@Bean
   @ConditionalOnBean(value = EsLogServiceImpl.class)
public TransportClient getESClient() {
	// 设置集群名字
	Settings settings = Settings.builder().put("cluster.name", this.clusterName).build();
	TransportClient client = new PreBuiltTransportClient(settings);
	try {
		// 读取的ip列表是以逗号分隔的
		for (String clusterNode : this.clusterNodes.split(",")) {
			String ip = clusterNode.split(":")[0];
			String port = clusterNode.split(":")[1];
			((TransportClient) client)
					.addTransportAddress(new TransportAddress(InetAddress.getByName(ip), Integer.parseInt(port)));
		}
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}

	return client;
}
 
Example #9
Source File: IndexderAppConfig.java    From elasticactors with Apache License 2.0 6 votes vote down vote up
@Bean(name = "indexingElasticsearchClient")
public Client createElasticsearchClient() {
    String[] elasticsearchHosts = environment.getRequiredProperty("actor.indexing.elasticsearch.hosts", String[].class);
    Integer elasticsearchPort = environment.getProperty("actor.indexing.elasticsearch.port", Integer.class, 9300);
    String elasticsearchClusterName = environment.getProperty("actor.indexing.elasticsearch.cluster.name", String.class, "elasticsearch");

    logger.info("Creating elasticsearch client with hosts <{}>", Arrays.toString(elasticsearchHosts));

    Settings settings = Settings.builder()
            .put("cluster.name", elasticsearchClusterName).build();

    client = new PreBuiltTransportClient(settings);

    for (String elasticsearchHost : elasticsearchHosts) {
        try {
            client.addTransportAddress(new TransportAddress(InetAddress.getByName(elasticsearchHost), elasticsearchPort));
        } catch (UnknownHostException e) {
            throw new BeanCreationException("Could not add elasticsearch host <" + elasticsearchHost + "> to client configuration. Aborting", e);
        }
    }

    return client;
}
 
Example #10
Source File: ElasticSearchBulk.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void initClient() throws UnknownHostException {


    Settings.Builder settingsBuilder = Settings.builder();
    settingsBuilder.put( Settings.Builder.EMPTY_SETTINGS );
    meta.getSettingsMap().entrySet().stream().forEach( ( s ) -> settingsBuilder.put( s.getKey(),
            environmentSubstitute( s.getValue() ) ) );

    PreBuiltTransportClient tClient = new PreBuiltTransportClient( settingsBuilder.build() );

    for ( Server server : meta.getServers() ) {
      tClient.addTransportAddress( new TransportAddress(
              InetAddress.getByName( environmentSubstitute( server.getAddress() ) ),
              server.getPort() ) );
    }

    client = tClient;

    /** With the upgrade to elasticsearch 6.3.0, removed the NodeBuilder,
     *  which was removed from the elasticsearch 5.0 API, see:
     *  https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_java_api_changes
     *  .html#_nodebuilder_removed
     */

  }
 
Example #11
Source File: Elasticsearch6Module.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 = new PreBuiltTransportClient(settings);
        for (String ip : hosts.split(",")) {
            client.addTransportAddress(
                    new TransportAddress(InetAddress.getByName(ip.split(":")[0]),
                            Integer.parseInt(ip.split(":")[1])));
        }
        return client;
    }
    catch (IOException e) {
        throw new PrestoException(UNEXPECTED_ES_ERROR, "Failed to get connection to Elasticsearch", e);
    }
}
 
Example #12
Source File: SharedElasticsearchResource.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doInitialize(ResourceSpecifier specifier, Map<String, Object> additionalParams)
    throws ResourceInitializationException {

  int esPort = ConfigUtils.stringToInteger(esPortString, 9300);

  // Use the transport client
  Settings.Builder settings = Settings.builder();
  settings.put("cluster.name", esCluster);

  client = new PreBuiltTransportClient(settings.build());
  client.addTransportAddress(
      new InetSocketTransportAddress(new InetSocketAddress(esHost, esPort)));

  return client != null;
}
 
Example #13
Source File: ElasticsearchServiceImpl.java    From canal-elasticsearch with Apache License 2.0 6 votes vote down vote up
public ElasticsearchServiceImpl(EsConf esConf) throws UnknownHostException {

        String clusterName = esConf.getClusterName();
        String address = esConf.getAddress();
        String[] hostPort = address.split(":");

        logger.info("Connect to elasticsearch  {}:{}", clusterName, address);

        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();
        transportClient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.valueOf(hostPort[1])));


        logger.info("Complete the connection to elasticsearch");
    }
 
Example #14
Source File: ElasticsearchResource.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private Client getRemoteClient() {
    if (remoteClient == null) {
        Settings settings = Settings.builder()
            .put("cluster.name", System.getProperty("REMOTE_ES_CLUSTER_NAME", "elasticsearch"))
            .build();
        TransportAddress[] transportAddresses = Arrays.stream(getRemoteEsAddresses().split(","))
            .map(address -> {
                String[] parts = address.split(":");
                try {
                    InetAddress inetAddress = InetAddress.getByName(parts[0]);
                    int port = parts.length > 1 ? Integer.parseInt(parts[1]) : 9300;
                    return new InetSocketTransportAddress(inetAddress, port);
                } catch (Exception ex) {
                    throw new VertexiumException("cannot find host: " + address, ex);
                }
            })
            .toArray(TransportAddress[]::new);
        remoteClient = new PreBuiltTransportClient(settings)
            .addTransportAddresses(transportAddresses);
    }
    return remoteClient;
}
 
Example #15
Source File: ElasticClient.java    From Stargraph with MIT License 6 votes vote down vote up
private TransportClient createClient() {
    Config cfg = getModelCfg();
    Settings settings = Settings.builder().put("cluster.name", cfg.getString("elastic.cluster-name")).build();
    TransportClient client = new PreBuiltTransportClient(settings);

    List<String> servers = cfg.getStringList("elastic.servers");
    logger.debug(marker, "Elastic Servers: {}", servers);
    for (String addr : servers) {
        try {
            String[] a = addr.split(":");
            String host = a[0];
            int port = Integer.parseInt(a[1]);
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
        } catch (Exception e) {
            logger.error(marker, "Transport client creation failed for '{}'", addr, e);
        }
    }

    return client;
}
 
Example #16
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 #17
Source File: InferenceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #18
Source File: ElasticsearchStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #19
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #20
Source File: ElasticsearchStoreWALTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String[] getIndexes() {

		Settings settings = Settings.builder().put("cluster.name", "cluster1").build();
		try (TransportClient client = new PreBuiltTransportClient(settings)) {
			client.addTransportAddress(
					new TransportAddress(InetAddress.getByName("localhost"), embeddedElastic.getTransportTcpPort()));

			return client.admin()
					.indices()
					.getIndex(new GetIndexRequest())
					.actionGet()
					.getIndices();
		} catch (UnknownHostException e) {
			throw new IllegalStateException(e);
		}

	}
 
Example #21
Source File: ESClient.java    From spider with GNU General Public License v3.0 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
    
    Settings settings = Settings.builder()
    		.put("cluster.name", staticValue.getEsClusterName()).build();
    try {
    	client = new PreBuiltTransportClient(settings)
    			.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), staticValue.getEsPort()));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example #22
Source File: ElasticsearchClientTransport.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
@SuppressWarnings("squid:S2095")
public void setup() {
  final Settings.Builder settingsBuilder = Settings.builder();

  settingsBuilder.put("node.name", properties.getNodeName())
      .put("cluster.name", properties.getClusterName())
      .put("http.enabled", properties.isHttpEnabled());

  client = new PreBuiltTransportClient(settingsBuilder.build());
  try {
    client.addTransportAddress(new TransportAddress(InetAddress.getByName(properties.getHost()), properties.getPort()));
  } catch (UnknownHostException e) {
    log.error("Error connecting to the Elasticsearch cluster at {}:{}", properties.getHost(), properties.getPort(), e);
  }
}
 
Example #23
Source File: EsDataQuerierOld.java    From moql with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void connect(String[] serverIps, Properties properties)
    throws IOException {
  Validate.notEmpty(serverIps, "serverIps is empty!");
  Validate.notEmpty(properties, "properties is empty!");

  Settings.Builder builder = Settings.builder();
  for (Map.Entry<Object, Object> entry : properties.entrySet()) {
    String key = (String) entry.getKey();
    if (key.equals(CLIENT_TRANSPORT_SNIFF)) {
      builder.put(key, (Boolean)entry.getValue());
    } else {
      builder.put(key, (String) entry.getValue());
    }
  }
  Settings settings = builder.build();
  client = new PreBuiltTransportClient(settings);
  for (int i = 0; i < serverIps.length; i++) {
    try {
      client.addTransportAddresses(
          new TransportAddress(InetAddress.getByName(serverIps[i]), 9300));
    } catch (UnknownHostException e) {
      throw new IOException(e);
    }
  }
}
 
Example #24
Source File: ESClient.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * init
 * 
 * @param esAddrs
 * @param clusterName
 */
private void init(String[] esAddrs, String clusterName) {

    Settings settings = Settings.EMPTY;

    if (!StringHelper.isEmpty(clusterName)) {
        settings = Settings.builder().put("cluster.name", clusterName).build();
    }

    client = new PreBuiltTransportClient(settings);

    for (String esAddr : esAddrs) {
        String[] ipport = esAddr.split(":");
        client.addTransportAddress(new InetSocketTransportAddress(
                new InetSocketAddress(ipport[0], DataConvertHelper.toInt(ipport[1], 9300))));
    }
}
 
Example #25
Source File: ESClient.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    try {
        LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
        Settings settings = Settings.builder()
                .put("cluster.name", staticValue.getEsClusterName()).build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), 9300));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example #26
Source File: ClientProviderWithDebugStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ClientProviderWithDebugStats(String hostname, int port, String clusterName) {
	try {
		Settings settings = Settings.builder().put("cluster.name", clusterName).build();
		TransportClient client = new PreBuiltTransportClient(settings);
		client.addTransportAddress(new TransportAddress(InetAddress.getByName(hostname), port));
		this.client = new ClientWithStats(client);
	} catch (UnknownHostException e) {
		throw new SailException(e);
	}
}
 
Example #27
Source File: ESTest.java    From canal with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws UnknownHostException {
    Settings.Builder settingBuilder = Settings.builder();
    settingBuilder.put("cluster.name", TestConstant.clusterName);
    Settings settings = settingBuilder.build();
    transportClient = new PreBuiltTransportClient(settings);
    String[] hostArray = TestConstant.esHosts.split(",");
    for (String host : hostArray) {
        int i = host.indexOf(":");
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.substring(0, i)),
            Integer.parseInt(host.substring(i + 1))));
    }
}
 
Example #28
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 #29
Source File: Client.java    From elasticsearch-mysql with MIT License 5 votes vote down vote up
private Client(String clusterName, Set<Node> nodes) {
    Settings settings = Settings.builder().put("client.transport.sniff", true)
            .put("cluster.name", clusterName).build();
    client = new PreBuiltTransportClient(settings);
    nodes.stream().forEach(node -> {
        try {
            client.addTransportAddress(new InetSocketTransportAddress(
                    InetAddress.getByName(node.getIp()), node.getPort()));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    });
}
 
Example #30
Source File: ElasticSearchConfig.java    From metacat with Apache License 2.0 5 votes vote down vote up
/**
 * The ElasticSearch client.
 *
 * @param config System config
 * @return Configured client or error
 */
@Bean
@ConditionalOnMissingBean(Client.class)
public Client elasticSearchClient(final Config config) {
    final String clusterName = config.getElasticSearchClusterName();
    if (StringUtils.isBlank(clusterName)) {
        throw new IllegalStateException("No cluster name set. Unable to continue");
    }
    final Settings settings = Settings.builder()
        .put("cluster.name", clusterName)
        .put("client.transport.sniff", true) //to dynamically add new hosts and remove old ones
        .put("transport.tcp.connect_timeout", "60s")
        .build();
    final TransportClient client = new PreBuiltTransportClient(settings);
    // Add the transport address if exists
    final String clusterNodesStr = config.getElasticSearchClusterNodes();
    if (StringUtils.isNotBlank(clusterNodesStr)) {
        final int port = config.getElasticSearchClusterPort();
        final Iterable<String> clusterNodes = Splitter.on(',').split(clusterNodesStr);
        clusterNodes.
            forEach(
                clusterNode -> {
                    try {
                        client.addTransportAddress(
                            new InetSocketTransportAddress(InetAddress.getByName(clusterNode), port)
                        );
                    } catch (UnknownHostException exception) {
                        log.error("Skipping unknown host {}", clusterNode);
                    }
                }
            );
    }

    if (client.transportAddresses().isEmpty()) {
        throw new IllegalStateException("No Elasticsearch cluster nodes added. Unable to create client.");
    }
    return client;
}