Elasticsearch TransportClient连接[Java]

时间:2019-07-17 08:10:51

标签: java elasticsearch

我正在使用 Maven Java项目中使用 Elasticsearch

...
 <elasticsearch.version>6.7.0</elasticsearch.version>
...
    <!-- Elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>transport-netty4-client</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-to-slf4j</artifactId>
        <version>2.8.2</version>
    </dependency>
    <!-- Elasticsearch -->        

当我尝试初始化TransportClient以便为文档建立索引时,它给了我错误:

    NoNodeAvailableException[None of the configured nodes are available: 
    [{#transport#-1}{BHMBbfcrSUOM_Pyaf1LcnA}{localhost}{127.0.0.1:9300}]]

也许需要在config / elasticsearch.yaml中添加有关transportartion的更多信息,否则当前配置是错误的。

Java代码:

    TransportAddress address;
    TransportClient client;
    Settings settings;

    try {
        address = new TransportAddress(InetAddress.getByName("localhost"), 9300);
        settings = Settings
                    .builder()
                    .put("cluster.name", "lib2life")
                    .put("client.transport.sniff", true)
                    .build();

        /* Initiate Transport Client */
        client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(address);

        /* Verify it cluster is healthy */
        ClusterHealthResponse clusterResponse = client
                    .admin()
                    .cluster()
                    .prepareHealth()
                    .setWaitForGreenStatus()
                    .setTimeout(TimeValue.timeValueSeconds(5))
                    .execute() //Error here
                    .actionGet();

         ... (more code)
    }

elasticsearch.yaml:

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: lib2life
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1

network.host:localhost
network.transport.tcp.port:9300

#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#

(elasticsearch.yaml中的其他信息已注释)

localhost:9200 给我:

{
  "name" : "IRINAMW7",
  "cluster_name" : "lib2life",
  "cluster_uuid" : "-wL1-xdESnyoknD2ZqALDQ",
  "version" : {
    "number" : "7.1.1",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "7a013de",
    "build_date" : "2019-05-23T14:04:00.380842Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2 个答案:

答案 0 :(得分:1)

问题出在端口配置上,您将端口9300用于REST调用,该端口实际上用于Elasticsearch集群中的节点间通信。请更改以下Java代码以使用端口9200

address = new TransportAddress(InetAddress.getByName("localhost"), 9200);

请详细了解es https://discuss.elastic.co/t/elasticsearch-port-9200-or-9300/72080/2的端口

答案 1 :(得分:0)

我解决了这个问题。问题是我将Elasticsearch 6.7.0版本与TransportClient一起使用,该版本已弃用,并由RestHighLevelClient代替。另外,我必须使用端口9200,并取消注释http.port:9200和Discovery.seed_hosts:来自elasticsearch.yaml的[“ host1”,“ host2”]