无法在弹性搜索中创建TransportClient [英] Unable to create TransportClient in Elastic-Search

查看:783
本文介绍了无法在弹性搜索中创建TransportClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在弹性搜索中创建一个TrasportClient,但是无法运行它。



Java代码:

  package es_example.custom; 

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.node.NodeBuilder;


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class EsRoutingTransportClient
{
//两个IP地址都被代码中的有效IP地址
String ZEN_DISCOVERY_UNICAST_HOSTS_NO_JSON =10.100.100.100,10.111.111.111 ;

String ES_PATH_HOME =/ app / devuser / elasticsearch / dummy-path-home /;

String ES_CLUSTER_NAME =my-cluster; //已验证的集群存在

整数ES_PORT = 9200;

私人客户端客户端;

public EsRoutingTransportClient()
{
String elasticSearchHost = ZEN_DISCOVERY_UNICAST_HOSTS_NO_JSON;
整数defaultPort = ES_PORT;
String elasticSearchCluster = ES_CLUSTER_NAME;

设置设置= Settings.settingsBuilder()
.put(http.enabled,true)
.put(discovery.zen.ping.multicast.enabled false)
//这两个IP地址都用代码
.put(discovery.zen.ping.unicast.hosts,[\10.100.100.100\ ,\10.111.111.111\])
.put(discovery.zen.minimum_master_nodes,1)
.put(path.home,ES_PATH_HOME)
。建立();
client = TransportClient.builder()。settings(settings).build();
parseAndAddHostPort(elasticSearchHost,defaultPort);
}

private void parseAndAddHostPort(String elasticSearchHost,Integer defaultPort)
{
String [] esHosts = elasticSearchHost.split(,);
for(String esHost:esHosts)
{
String [] hostPort = esHost.split(:);
整数esPort = defaultPort;

if(hostPort.length == 2)
{
esHost = hostPort [0];
esPort = Integer.parseInt(hostPort [1]);
}
try
{
client =((TransportClient)client).addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(esHost),esPort));
System.out.println(添加+ esHost +,端口+ esPort +作为传输地址);
} catch(UnknownHostException e){
e.printStackTrace();
}
}
}

public void sendMessage(String msg,String indexName,String type,String id,String routing)
{
尝试
{
byte [] byteBuffer = msg.getBytes();
IndexResponse response = client.prepareIndex(indexName,type,id)
.setSource(byteBuffer)
.execute()
.actionGet();
}
catch(异常e)
{
e.printStackTrace();
}
}

public static void main(String args [])
{
EsRoutingTransportClient esClient = new EsRoutingTransportClient();
esClient.sendMessage(Hello,storm,json-trip,null,null);
}
}

pom.xml:

 <?xml version =1.0encoding =UTF-8?>< project xmlns =http://maven.apache。 org / POM / 4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http ://maven.apache.org/xsd/maven-4.0.0.xsd> 
< modelVersion> 4.0.0< / modelVersion>
< groupId> es_example< / groupId>
< artifactId> es-proj< / artifactId>
< version> 0.0.3-SNAPSHOT< / version>
< packaging> jar< / packaging>
< name> es-proj< / name>
<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>
<依赖关系>
<依赖关系>
< groupId> org.elasticsearch< / groupId>
< artifactId> elasticsearch< / artifactId>
< version> 2.2.0< / version>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-shade-plugin< / artifactId>
< version> 2.4.3< / version>
< configuration />
<执行>
< execution>
< phase> package< / phase>
< goals>
< goal> shade< / goal>
< / goals>
< configuration>
< artifactSet>
< excludes>
< exlcude> com.esotericsoftware.kryo:kryo:*< / exlcude>
< / excludes>
< / artifactSet>
<变压器>
< transformer implementation =org.apache.maven.plugins.shade.resource.ServicesResourceTransformer/>
< transformer implementation =org.apache.maven.plugins.shade.resource.ManifestResourceTransformer>
< mainClass> es_example.custom.EsRoutingTransportClient< / mainClass>
< / transformer>
< / transformers>
< / configuration>
< / execution>
< / executions>
< / plugin>
< / plugins>
< / build>
< / project>

我运行如下:

  mvn clean package 
java -jar target / es-proj-0.0.3-SNAPSHOT.jar

这是我得到的例外:

  2016年3月7日4:17 :43 PM org.elasticsearch.plugins.PluginsService< init> 
信息:[Bullseye]模块[],插件[],网站[]
2010年3月7日4:17:48 org.elasticsearch.client.transport.TransportClientNodesService $ SimpleNodeSampler doSample
信息:[Bullseye]无法获取{#transport#-1} {10.100.100.100} {10.100.100.100:9200}的节点信息,断开连接...
ReceiveTimeoutTransportException [[] [10.100.100.100:9200] [cluster:monitor / nodes / liveness] request_id [0]在[5003ms]之后超时)
在org.elasticsearch.transport.TransportService $ TimeoutHandler.run(TransportService.java:645)
在java。 util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)
在java.lang.Thread.run( Thread.java:745)

添加10.100.100.100端口9200作为传输地址
2010年3月7日下午4:17:53 org.elasticsearch.client.transport.TransportClientNodesService $ SimpleNodeSampler doSample
信息:[Bullseye]无法获取节点inf o对于{#transport#-1} {10.100.100.100} {10.100.100.100:9200},断开连接...
ReceiveTimeoutTransportException [[] [10.100.100.100:9200] [cluster:monitor/nodes/liveness] request_id [1]在[5002ms]之后超时)
在org.elasticsearch.transport.TransportService $ TimeoutHandler.run(TransportService.java:645)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor .java:1142)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)
在java.lang.Thread.run(Thread.java:745)

Mar 07,2016 4:17:58 PM org.elasticsearch.client.transport.TransportClientNodesService $ SimpleNodeSampler doSample
INFO:[Bullseye]无法获取{#transport#-1}的节点信息{ 10.100.100.100} {10.100.100.100:9200},断开连接...
ReceiveTimeoutTransportException [[] [10.100.100.100:9200] [cluster:monitor/nodes/liveness] request_id [2]在[5000ms]之后超时]
在org.elasticsearch.transport.TransportService $ TimeoutHandle r.run(TransportService.java:645)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor。 java:617)
在java.lang.Thread.run(Thread.java:745)

Mar 07,2016 4:18:03 PM org.elasticsearch.client.transport.TransportClientNodesService $ SimpleNodeSampler doSample
INFO:[Bullseye]无法获取{#transport#-2} {10.111.111.111} {10.111.111.111:9200}的节点信息,断开连接...
ReceiveTimeoutTransportException [[] [10.111.111.111:9200] [cluster:monitor/nodes/liveness] request_id [3]在[5001ms]之后超时)
在org.elasticsearch.transport.TransportService $ TimeoutHandler.run(TransportService.java:645)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)
在java .lang.Thread.run(Thread.java:745)

Adde d 10.111.111.111,端口9200作为传输地址
NoNodeAvailableException [没有配置的节点可用:[{#transport#-1} {10.100.100.100} {10.100.100.100:9200},{#transport# -2} {10.111.111.111} {10.111.111.111:9200}]]
在org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
在org.elasticsearch.client .transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
在org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
在org.elasticsearch.client.transport 。TransportTient.doExecute .java:85)
在org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:59)
在es_example.custom.EsRoutingTransportClient.sendMessage(EsRoutingTransportClient.java:82)
在es_example.custom.EsRoutingTransportClient.main(EsRoutingTransportClient.java:94)

Ping两个主机都在同一个终端上工作。



(我已经使用了10.100.100.100和10.111。在上述例子中为111.111。在实际的代码库中,它们是真正有效的IP地址)



上面列表中提到的一个节点是主节点,另一个节点是数据节点。 / p>

我不知道如何解决这个问题。



任何帮助将不胜感激。



谢谢!

解决方案

您为 TransportClient 不正确。它们都应该是ES节点上 elasticsearch.yml 文件中的设置。这些是配置ES服务器而不是ES TransportClient的设置。



因此,需要在 elasticsearch.yml

中设置以下设置, code>文件在10.100.100.100和10.111.111.111上运行的节点上

  cluster.name:my-cluster 
http.enabled:true
discovery.zen.ping.multicast.enabled:false
discovery.zen.ping.unicast.hosts:[10.100.100.100,10.111.111.111]
discovery.zen.minimum_master_nodes:1
path.home:/ path / to / es / home

此外,您还需要在每个主机上设置以下设置及其正确的IP地址(即每个主机将绑定的IP地址,以便其他节点和客户端可以看到它们并连接到它们): / p>

  network.host:10.100.100.100 

然后你可以这样构建你的ES客户端:

 设置设置= Settings.settingsBuilder ()。建立(); 
client = TransportClient.builder()。settings(settings).build();
parseAndAddHostPort(elasticSearchHost,defaultPort);

应该可以。


I have been trying to create a TrasportClient in Elastic-Search but somehow not able to run it.

Java code:

package es_example.custom;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.node.NodeBuilder;


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class EsRoutingTransportClient
{
    // Both the IP addresses are replaced with valid IP addresses in code
    String ZEN_DISCOVERY_UNICAST_HOSTS_NO_JSON = "10.100.100.100,10.111.111.111";

    String ES_PATH_HOME = "/app/devuser/elasticsearch/dummy-path-home/";

    String ES_CLUSTER_NAME = "my-cluster"; // Verified cluster exists

    Integer ES_PORT = 9200;

    private Client client;

    public EsRoutingTransportClient()
    {
        String elasticSearchHost = ZEN_DISCOVERY_UNICAST_HOSTS_NO_JSON;
        Integer defaultPort = ES_PORT;
        String elasticSearchCluster = ES_CLUSTER_NAME;

        Settings settings = Settings.settingsBuilder()
                .put("http.enabled", true)
                .put("discovery.zen.ping.multicast.enabled", false)
                // Both the IP addresses are replaced with valid IP addresses in code
                .put("discovery.zen.ping.unicast.hosts", "[\"10.100.100.100\",\"10.111.111.111\"]")
                .put("discovery.zen.minimum_master_nodes", 1)
                .put("path.home", ES_PATH_HOME)
                .build();
        client = TransportClient.builder().settings(settings).build();
        parseAndAddHostPort(elasticSearchHost, defaultPort);
    }

    private void parseAndAddHostPort(String elasticSearchHost, Integer defaultPort)
    {
        String [] esHosts = elasticSearchHost.split(",");
        for (String esHost: esHosts)
        {
            String [] hostPort = esHost.split(":");
            Integer esPort = defaultPort;

            if (hostPort.length == 2)
            {
                esHost = hostPort[0];
                esPort = Integer.parseInt(hostPort[1]);
            }
            try
            {
                client = ((TransportClient)client).addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName(esHost), esPort));
                System.out.println("Added " + esHost + " with port " + esPort + " as a transport address");
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }

    public void sendMessage (String msg, String indexName, String type, String id, String routing)
    {
        try
        {
            byte[] byteBuffer = msg.getBytes();
            IndexResponse response = client.prepareIndex(indexName, type, id)
                    .setSource(byteBuffer)
                    .execute()
                    .actionGet();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main (String args[])
    {
        EsRoutingTransportClient esClient = new EsRoutingTransportClient();
        esClient.sendMessage("Hello", "storm", "json-trips", null, null);
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>es_example</groupId>
  <artifactId>es-proj</artifactId>
  <version>0.0.3-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>es-proj</name>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>2.2.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <configuration />
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <excludes>
                  <exlcude>com.esotericsoftware.kryo:kryo:*</exlcude>
                </excludes>
              </artifactSet>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>es_example.custom.EsRoutingTransportClient</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

I run it as follows:

mvn clean package
java -jar target/es-proj-0.0.3-SNAPSHOT.jar

And this is the exception I get:

Mar 07, 2016 4:17:43 PM org.elasticsearch.plugins.PluginsService <init>
INFO: [Bullseye] modules [], plugins [], sites []
Mar 07, 2016 4:17:48 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
INFO: [Bullseye] failed to get node info for {#transport#-1}{10.100.100.100}{10.100.100.100:9200}, disconnecting...
ReceiveTimeoutTransportException[[][10.100.100.100:9200][cluster:monitor/nodes/liveness] request_id [0] timed out after [5003ms]]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:645)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Added 10.100.100.100 with port 9200 as a transport address
Mar 07, 2016 4:17:53 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
INFO: [Bullseye] failed to get node info for {#transport#-1}{10.100.100.100}{10.100.100.100:9200}, disconnecting...
ReceiveTimeoutTransportException[[][10.100.100.100:9200][cluster:monitor/nodes/liveness] request_id [1] timed out after [5002ms]]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:645)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Mar 07, 2016 4:17:58 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
INFO: [Bullseye] failed to get node info for {#transport#-1}{10.100.100.100}{10.100.100.100:9200}, disconnecting...
ReceiveTimeoutTransportException[[][10.100.100.100:9200][cluster:monitor/nodes/liveness] request_id [2] timed out after [5000ms]]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:645)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Mar 07, 2016 4:18:03 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
INFO: [Bullseye] failed to get node info for {#transport#-2}{10.111.111.111}{10.111.111.111:9200}, disconnecting...
ReceiveTimeoutTransportException[[][10.111.111.111:9200][cluster:monitor/nodes/liveness] request_id [3] timed out after [5001ms]]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:645)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Added 10.111.111.111 with port 9200 as a transport address
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{10.100.100.100}{10.100.100.100:9200}, {#transport#-2}{10.111.111.111}{10.111.111.111:9200}]]
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
    at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:286)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:351)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:85)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:59)
    at es_example.custom.EsRoutingTransportClient.sendMessage(EsRoutingTransportClient.java:82)
    at es_example.custom.EsRoutingTransportClient.main(EsRoutingTransportClient.java:94)

Ping to both the hosts is working from the same terminal.

(I have used 10.100.100.100 and 10.111.111.111 in the above example. In actually code-base, they are real valid IP addresses)

Also one of the nodes mentioned in the list above is master and the other one is a data node.

I am not sure how to fix this.

Any help would be greatly appreciated.

Thanks!

解决方案

The settings you're creating for the TransportClient are not correct. They all should be settings in the elasticsearch.yml file on your ES nodes. Those are settings to configure the ES server, not the ES TransportClient.

So the following settings need to be set in the elasticsearch.yml file on the nodes running on 10.100.100.100 and 10.111.111.111

cluster.name: my-cluster
http.enabled: true
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["10.100.100.100","10.111.111.111"]
discovery.zen.minimum_master_nodes: 1
path.home: /path/to/es/home

Also you'll need to set on each host the following setting with their proper IP address (that's the IP address each host will bind to so that other nodes and clients can see them and connect to them):

network.host: 10.100.100.100

Then you can build your ES client simply like this:

    Settings settings = Settings.settingsBuilder().build();
    client = TransportClient.builder().settings(settings).build();
    parseAndAddHostPort(elasticSearchHost, defaultPort);

That should work.

这篇关于无法在弹性搜索中创建TransportClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆