org.elasticsearch.common.transport.InetSocketTransportAddress Java Examples

The following examples show how to use org.elasticsearch.common.transport.InetSocketTransportAddress. 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: ClientFactory.java    From storm-trident-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public TransportClient makeClient(Map conf) {

    String clusterHosts = (String)conf.get(HOSTS);
    String clusterName  = (String)conf.get(NAME);

    Preconditions.checkNotNull(clusterHosts,"no setting found for Transport Client, make sure that you set property " + HOSTS);

    TransportClient client = new TransportClient(buildSettings(clusterName));

    for(String hostAndPort : StringUtils.split(clusterHosts, HOST_SEPARATOR)) {
        int portPos = hostAndPort.indexOf(PORT_SEPARATOR);
        boolean noPortDefined = portPos == -1;
        int port =  ( noPortDefined ) ? DEFAULT_PORT : Integer.parseInt(hostAndPort.substring(portPos + 1, hostAndPort.length()));
        String host  = (noPortDefined) ? hostAndPort : hostAndPort.substring(0, portPos);
        client.addTransportAddress(new InetSocketTransportAddress(host, port));
    }
    return client;
}
 
Example #3
Source File: ElasticSearchTransportClient.java    From ingestion with Apache License 2.0 6 votes vote down vote up
/**
 * Open client to elaticsearch cluster
 * 
 * @param clusterName
 */
private void openClient(String clusterName) {
  logger.info("Using ElasticSearch hostnames: {} ",
      Arrays.toString(serverAddresses));
  Settings settings = ImmutableSettings.settingsBuilder()
      .put("cluster.name", clusterName).build();

  TransportClient transportClient = new TransportClient(settings);
  for (InetSocketTransportAddress host : serverAddresses) {
    transportClient.addTransportAddress(host);
  }
  if (client != null) {
    client.close();
  }
  client = transportClient;
}
 
Example #4
Source File: BaseTransportClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
protected boolean connect(Collection<InetSocketTransportAddress> addresses, boolean autodiscover) {
    logger.info("trying to connect to {}", addresses);
    client.addTransportAddresses(addresses);
    if (client.connectedNodes() != null) {
        List<DiscoveryNode> nodes = client.connectedNodes();
        if (!nodes.isEmpty()) {
            logger.info("connected to {}", nodes);
            if (autodiscover) {
                logger.info("trying to auto-discover all cluster nodes...");
                ClusterStateRequestBuilder clusterStateRequestBuilder =
                        new ClusterStateRequestBuilder(client, ClusterStateAction.INSTANCE);
                ClusterStateResponse clusterStateResponse = clusterStateRequestBuilder.execute().actionGet();
                DiscoveryNodes discoveryNodes = clusterStateResponse.getState().getNodes();
                client.addDiscoveryNodes(discoveryNodes);
                logger.info("after auto-discovery connected to {}", client.connectedNodes());
            }
            return true;
        }
        return false;
    }
    return false;
}
 
Example #5
Source File: TestElasticSearchSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldParseConfigurationUsingDefaults() {
  parameters.put(HOSTNAMES, "10.5.5.27");
  parameters.remove(INDEX_NAME);
  parameters.remove(INDEX_TYPE);
  parameters.remove(CLUSTER_NAME);

  fixture = new ElasticSearchSink();
  fixture.configure(new Context(parameters));

  InetSocketTransportAddress[] expected = { new InetSocketTransportAddress(
      "10.5.5.27", DEFAULT_PORT) };

  assertEquals(DEFAULT_INDEX_NAME, fixture.getIndexName());
  assertEquals(DEFAULT_INDEX_TYPE, fixture.getIndexType());
  assertEquals(DEFAULT_CLUSTER_NAME, fixture.getClusterName());
  assertArrayEquals(expected, fixture.getServerAddresses());
}
 
Example #6
Source File: DefaultClientFactory.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
@Override
public ShellNativeClient newTransportClient(String... addresses) {
    TransportAddress[] transportAddresses = new TransportAddress[addresses.length];
    for (int i = 0; i < addresses.length; i++) {
        String address = addresses[i];

        String[] splitAddress = address.split(":");
        String host = shellSettings.settings().get(ShellSettings.TRANSPORT_HOST);

        if (splitAddress.length>=1) {
            host = splitAddress[0];
        }

        int port = shellSettings.settings().getAsInt(ShellSettings.TRANSPORT_PORT, null);
        if (splitAddress.length>=2) {
            try {
                port = Integer.valueOf(splitAddress[1]);
            } catch(NumberFormatException e) {
                logger.warn("Unable to parse port [{}]", splitAddress[1], e);
            }
        }

        transportAddresses[i] = new InetSocketTransportAddress(host, port);
    }
    return newTransportClient(transportAddresses);
}
 
Example #7
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 #8
Source File: EsClient.java    From hsweb-learning with Apache License 2.0 6 votes vote down vote up
public static TransportClient getClient() {
    try {
        if (tclient == null) {
            String EsHosts = "192.168.1.41:9300,192.168.1.42:9300,192.168.1.43:9300";
            Settings settings = Settings.settingsBuilder()
                    .put("cluster.name", "dkes")//设置集群名称
                    .put("tclient.transport.sniff", true).build();//自动嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中

            tclient = TransportClient.builder().settings(settings).build();
            String[] nodes = EsHosts.split(",");
            for (String node : nodes) {
                if (node.length() > 0) {//跳过为空的node(当开头、结尾有逗号或多个连续逗号时会出现空node)
                    String[] hostPort = node.split(":");
                    tclient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));

                }
            }
        }//if
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tclient;
}
 
Example #9
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 #10
Source File: EshClientFactory.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
public TransportClient getClient()   {
    if (esClient == null) {
        synchronized (this) {
            if (esClient == null) {
                try {
                    //判断配置
                    Preconditions.checkNotNull(clusterName, "es 服务clusterName未配置");
                    Preconditions.checkNotNull(addresses, "es 服务ip未配置");
                    //Preconditions.checkArgument(esPort > 0, "es 服务服务port未配置");
                    //设置集群的名字
                    Settings settings = Settings.settingsBuilder().put("client.node", true).put("cluster.name", clusterName).put("client.transport.sniff", sniff).build();
                    //Settings settings = Settings.settingsBuilder().put("client.transport.sniff", sniff).build();
                    //创建集群client并添加集群节点地址
                    esClient = TransportClient.builder().settings(settings).build();
                    for (String address : addresses) {
                        String[] inetAddress = address.split(":");
                        esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(inetAddress[0]), new Integer(inetAddress[1])));
                    }
                  }catch (Exception e){
                     LOGGER.error("客户端连接初始化异常",e);
                }
            }
        }
    }
    return esClient;
}
 
Example #11
Source File: ElasticIndexer.java    From bluima with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
@Override
public void initialize(UimaContext context)
		throws ResourceInitializationException {
	super.initialize(context);

	Settings settings = settingsBuilder().put("cluster.name", clusterName)
			.build();
	client = new TransportClient(settings)
			.addTransportAddress(new InetSocketTransportAddress(host, port));
	if (((TransportClient) client).connectedNodes().size() == 0) {
		throw new RuntimeException(
				"Could not connect to ES cluster, using host: " + host);
	}
	bulkRequest = client.prepareBulk();
}
 
Example #12
Source File: TestElasticSearchSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldParseConfiguration() {
  parameters.put(HOSTNAMES, "10.5.5.27");
  parameters.put(CLUSTER_NAME, "testing-cluster-name");
  parameters.put(INDEX_NAME, "testing-index-name");
  parameters.put(INDEX_TYPE, "testing-index-type");
  parameters.put(TTL, "10");

  fixture = new ElasticSearchSink();
  fixture.configure(new Context(parameters));

  InetSocketTransportAddress[] expected = { new InetSocketTransportAddress(
      "10.5.5.27", DEFAULT_PORT) };

  assertEquals("testing-cluster-name", fixture.getClusterName());
  assertEquals("testing-index-name", fixture.getIndexName());
  assertEquals("testing-index-type", fixture.getIndexType());
  assertEquals(TimeUnit.DAYS.toMillis(10), fixture.getTTLMs());
  assertArrayEquals(expected, fixture.getServerAddresses());
}
 
Example #13
Source File: SearchES.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
/**
 * 多字段查询
 */
public static void multisearch() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2","clients");
        SearchResponse searchResponse = searchRequestBuilder.
                setQuery(QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("id","5"))
                        .should(QueryBuilders.prefixQuery("content","oracle")))
                .setFrom(0).setSize(100).setExplain(true).execute().actionGet();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #14
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 #15
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 #16
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 #17
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 #18
Source File: ElasticsearchContainer.java    From logstash with Apache License 2.0 6 votes vote down vote up
public Client createClient() {
    final AtomicReference<Client> elasticsearchClient = new AtomicReference<>();
    await().atMost(30, TimeUnit.SECONDS).pollDelay(1, TimeUnit.SECONDS).until(() -> {
        Client c = new TransportClient(ImmutableSettings.settingsBuilder().put("cluster.name", elasticsearchClusterName).build()).addTransportAddress(new InetSocketTransportAddress(getIpAddress(), 9300));
        try {
            c.admin().cluster().health(Requests.clusterHealthRequest("_all")).actionGet();
        } catch (ElasticsearchException e) {
            c.close();
            return false;
        }
        elasticsearchClient.set(c);
        return true;
    });
    assertEquals(elasticsearchClusterName, elasticsearchClient.get().admin().cluster().health(Requests.clusterHealthRequest("_all")).actionGet().getClusterName());
    return elasticsearchClient.get();
}
 
Example #19
Source File: XmlPluginTest.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
@Test
public void testHealthResponse() throws Exception {
    InetSocketTransportAddress httpAddress = findHttpAddress(client("1"));
    if (httpAddress == null) {
        throw new IllegalArgumentException("no HTTP address found");
    }
    URL base = new URL("http://" + httpAddress.getHost() + ":" + httpAddress.getPort());
    URL url = new URL(base, "/_cluster/health?xml");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    if ((line = reader.readLine()) != null) {
        assertTrue(line.startsWith("<root xmlns=\"http://elasticsearch.org/ns/1.0/\">"));
        assertTrue(line.endsWith("</root>"));
    }
    reader.close();
}
 
Example #20
Source File: XmlPluginTest.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrettyHealthResponse() throws Exception {
    InetSocketTransportAddress httpAddress = findHttpAddress(client("1"));
    if (httpAddress == null) {
        throw new IllegalArgumentException("no HTTP address found");
    }
    URL base = new URL("http://" + httpAddress.getHost() + ":" + httpAddress.getPort());
    URL url = new URL(base, "/_cluster/health?xml&pretty");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    // check only first line
    if ((line = reader.readLine()) != null) {
        assertTrue(line.startsWith("<root xmlns=\"http://elasticsearch.org/ns/1.0/\">"));
    }
    reader.close();
}
 
Example #21
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
/**
	 * 创建TransportClient
	 * @return
	 */
	private static Client createTransportClient() {
		//创建settings
		Settings settings = ImmutableSettings.settingsBuilder()
			.put("cluster.name", "elasticsearch")//设置集群名称
//		    .put("shield.user", "admin:sysadmin")
			.build();
		Client client = null;
		try {
			client = new org.elasticsearch.client.transport.TransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return client;
	}
 
Example #22
Source File: IMAPImporterCl.java    From elasticsearch-imap with Apache License 2.0 5 votes vote down vote up
public static void start(Map<String, Object> settings, boolean embeddedMode) throws Exception {

        Settings.Builder builder = Settings.settingsBuilder();

        for(String key: settings.keySet()) {
            builder.put(key, String.valueOf(settings.get(key)));
        }
        
        if(embeddedMode) {
            try {
                FileUtils.forceDelete(new File("./data"));
            } catch (Exception e) {
                //ignore
            }
            builder.put("path.home",".");
            builder.put("node.local", true);
            builder.put("http.cors.enabled", true);
            builder.put("http.cors.allow-origin", "*");
            builder.put("cluster.name", "imap-embedded-"+System.currentTimeMillis());
            node = new PluginAwareNode(builder.build(), (Collection) Lists.newArrayList(MapperAttachmentsPlugin.class));
            node.start();
            client = node.client();
        }else
        {
            Settings eSettings = builder.build();
            client = new TransportClient.Builder().settings(eSettings).build();
            String[] hosts = eSettings.get("elasticsearch.hosts").split(",");
            
            for (int i = 0; i < hosts.length; i++) {
                String host = hosts[i];
                String hostOnly = host.split(":")[0];
                String portOnly = host.split(":")[1];
                System.out.println("Adding "+hostOnly+":"+portOnly);
                ((TransportClient)client).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostOnly), Integer.parseInt(portOnly)));
            }
        }
        imap = new IMAPImporter(settings, client);
        imap.start();
    }
 
Example #23
Source File: ElasticSearchClientFactory.java    From elasticsearch-reindex-tool with Apache License 2.0 5 votes vote down vote up
public static Client createClient(ElasticDataPointer elasticDataPointer) {
  Settings settings = Settings.settingsBuilder()
      .put("client.transport.sniff", elasticDataPointer.isSniff())
      .put(ClusterName.SETTING, elasticDataPointer.getClusterName())
      .build();
  TransportClient client = TransportClient.builder().settings(settings).build();
  client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elasticDataPointer.getHost(), elasticDataPointer
      .getPort())));
  return client;
}
 
Example #24
Source File: TestElasticSearchSink.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseMultipleHostUsingDefaultPorts() {
  parameters.put(HOSTNAMES, "10.5.5.27,10.5.5.28,10.5.5.29");

  fixture = new ElasticSearchSink();
  fixture.configure(new Context(parameters));

  InetSocketTransportAddress[] expected = {
      new InetSocketTransportAddress("10.5.5.27", DEFAULT_PORT),
      new InetSocketTransportAddress("10.5.5.28", DEFAULT_PORT),
      new InetSocketTransportAddress("10.5.5.29", DEFAULT_PORT) };

  assertArrayEquals(expected, fixture.getServerAddresses());
}
 
Example #25
Source File: ElasticsearchJavaUtil.java    From kmanager with Apache License 2.0 5 votes vote down vote up
private void initClient(String stringHosts) {
	Settings settings = Settings.settingsBuilder().put("client.transport.ignore_cluster_name", true).build();
	client = TransportClient.builder().settings(settings).build();
	String[] hosts = stringHosts.split(",");
	for (String host : hosts) {
		String[] ha = host.split(":");
		if (ha.length < 2) {
			throw new RuntimeException("Elasticsearch host should be like-> 127.0.0.1:9300");
		}
		client.addTransportAddress(
				new InetSocketTransportAddress(new InetSocketAddress(ha[0], Integer.parseInt(ha[1]))));
	}
}
 
Example #26
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 #27
Source File: EsConfiguration.java    From SkyEye with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public TransportClient transportClient(Settings settings) {
    TransportClient client = TransportClient.builder().settings(settings).build();
    for (String ip : this.esProperties.getIps().split(Constants.COMMA)) {
        try {
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), this.esProperties.getPort()));
        } catch (UnknownHostException e) {
            LOGGER.error("es集群主机名错误, ip: {}", ip);
        }
    }
    return client;
}
 
Example #28
Source File: NodeTestUtils.java    From elasticsearch-analysis-baseform with Apache License 2.0 5 votes vote down vote up
protected void findNodeAddress() {
    NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
    NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
    Object obj = response.iterator().next().getTransport().getAddress()
            .publishAddress();
    if (obj instanceof InetSocketTransportAddress) {
        InetSocketTransportAddress address = (InetSocketTransportAddress) obj;
        host = address.address().getHostName();
        port = address.address().getPort();
    }
}
 
Example #29
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws Exception {
    Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch").put("client.transport.sniff", true)
            .put("client.transport.ignore_cluster_name", false).put("client.transport.ping_timeout", "5s")
            .put("client.transport.nodes_sampler_interval", "5s").build();

    TransportAddress addr = new InetSocketTransportAddress(InetAddress.getByName(null), 9300);
    client = TransportClient.builder().settings(settings).build()
            // .addTransportAddress(new
            // InetSocketTransportAddress(InetAddress.getByName("host1"),
            // 9300))
            .addTransportAddress(addr);
    // on shutdown
    System.out.println(client.toString());
}
 
Example #30
Source File: ElasticsearchClient.java    From canal_mysql_elasticsearch_sync with Apache License 2.0 5 votes vote down vote up
@Bean
public TransportClient getTransportClient() throws Exception {
    Settings settings = Settings.builder().put("cluster.name", clusterName)
            .put("client.transport.sniff", true)
            .build();
    transportClient = new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.valueOf(port)));
    logger.info("elasticsearch transportClient 连接成功");
    return transportClient;
}