Java Code Examples for org.elasticsearch.transport.client.PreBuiltTransportClient

The following examples show how to use org.elasticsearch.transport.client.PreBuiltTransportClient. These examples are extracted from open source projects. 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
@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 Project: cloud-service   Source File: ElasticSearchConfig.java    License: 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 3
Source Project: Mahuta   Source File: ElasticSearchService.java    License: 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 4
Source Project: Mahuta   Source File: DefaultMahutaTest.java    License: 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 5
Source Project: presto-connectors   Source File: Elasticsearch6Module.java    License: 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 6
Source Project: presto-connectors   Source File: Elasticsearch5Module.java    License: 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 7
Source Project: Stargraph   Source File: ElasticClient.java    License: 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 8
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 9
Source Project: Gather-Platform   Source File: ESClient.java    License: 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 10
Source Project: uavstack   Source File: ESClient.java    License: 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 11
@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 12
@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 13
Source Project: rdf4j   Source File: InferenceTest.java    License: 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 14
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 15
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 16
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 17
Source Project: spider   Source File: ESClient.java    License: 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 18
@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 19
Source Project: moql   Source File: EsDataQuerierOld.java    License: 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 20
Source Project: vertexium   Source File: ElasticsearchResource.java    License: 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 21
Source Project: baleen   Source File: SharedElasticsearchResource.java    License: 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 22
Source Project: testcontainers-java   Source File: ElasticsearchContainerTest.java    License: 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 23
Source Project: skywalking   Source File: CaseController.java    License: 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 24
Source Project: elasticactors   Source File: IndexderAppConfig.java    License: 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 25
Source Project: pentaho-kettle   Source File: ElasticSearchBulk.java    License: 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 26
Source Project: Flink-CEPplus   Source File: Elasticsearch5ApiCallBridge.java    License: 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 27
Source Project: flink   Source File: Elasticsearch5ApiCallBridge.java    License: 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 28
Source Project: dk-fitting   Source File: ElasticsearchUtil.java    License: Apache License 2.0 5 votes vote down vote up
public Client getEsClient(Map<String, Integer> esAddresses,Map<String, String> esConfig){
    Settings settings = Settings.builder().put(esConfig).put("client.transport.sniff", true).build();
    TransportClient transportClient = new PreBuiltTransportClient(settings);
    for (Map.Entry<String, Integer> esIpAndPort: esAddresses.entrySet()) {
        transportClient.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(esIpAndPort.getKey(), esIpAndPort.getValue())));
    }
    List<DiscoveryNode> discoveryNodes = transportClient.connectedNodes();
    if (discoveryNodes.isEmpty()) {
        throw new RuntimeException("Cannot connect to any elasticsearch nodes");
    }
   return transportClient;
}
 
Example 29
Source Project: dk-fitting   Source File: ElasticsearchUtil.java    License: Apache License 2.0 5 votes vote down vote up
public Client getEsClient(Map<String, Integer> esAddresses,Map<String, String> esConfig){
    Settings settings = Settings.builder().put(esConfig).put("client.transport.sniff", true).build();
    TransportClient transportClient = new PreBuiltTransportClient(settings);
    for (Map.Entry<String, Integer> esIpAndPort: esAddresses.entrySet()) {
        transportClient.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(esIpAndPort.getKey(), esIpAndPort.getValue())));
    }
    List<DiscoveryNode> discoveryNodes = transportClient.connectedNodes();
    if (discoveryNodes.isEmpty()) {
        throw new RuntimeException("Cannot connect to any elasticsearch nodes");
    }
   return transportClient;
}
 
Example 30
Source Project: canal-1.1.3   Source File: ESTest.java    License: 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))));
    }
}