ElasticSearch Java API:NoNodeAvailableException: N

2020-01-31 03:14发布

public static void main(String[] args) throws IOException {
    Settings settings = ImmutableSettings.settingsBuilder()
            .put("cluster.name", "foxzen")
            .put("node.name", "yu").build();
    Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("XXX.XXX.XXX.XXX", 9200));
            // XXX is my server's ip address
    IndexResponse response = client.prepareIndex("twitter", "tweet")
            .setSource(XContentFactory.jsonBuilder()
                    .startObject()
                    .field("productId", "1")
                    .field("productName", "XXX").endObject()).execute().actionGet();
    System.out.println(response.getIndex());
    System.out.println(response.getType());
    System.out.println(response.getVersion());
    client.close();
}

I access server from my computer

curl -get http://XXX.XXX.XXX.XXX:9200/

get this

{
    "status" : 200,
    "name" : "yu",
    "version" : {
        "number" : "1.1.0",
        "build_hash" : "2181e113dea80b4a9e31e58e9686658a2d46e363",
        "build_timestamp" : "2014-03-25T15:59:51Z",
        "build_snapshot" : false,
        "lucene_version" : "4.7"
    },
    "tagline" : "You Know, for Search"
}

Why get error by using Java API?

EDIT

There is the cluster and node part config of elasticsearch.yml

################################### Cluster ###################################

# Cluster name identifies your cluster for auto-discovery. If you're running
# multiple clusters on the same network, make sure you're using unique names.
#
cluster.name: foxzen


#################################### Node #####################################

# Node names are generated dynamically on startup, so you're relieved
# from configuring them manually. You can tie this node to a specific name:
#
node.name: yu

8条回答
时光不老,我们不散
2楼-- · 2020-01-31 03:39

I assume that you are setting the ES server on a remote host? In that case you will need to bind the publish address to the host's public IP address.

In your ES host edit /etc/elasticsearch/elasticsearch.yml and add its public IP after network.publish_host:

# Set the address other nodes will use to communicate with this node. If not
# set, it is automatically derived. It must point to an actual IP address.
#
network.publish_host: 192.168.0.1

And in your code connect to this host on port 9300. Note that you need the IP and not the domain name (at least according to my experience on Amazon EC2)

查看更多
SAY GOODBYE
3楼-- · 2020-01-31 03:40

For folks with similar problems, I received this because I had not set cluster.name in the TransportClient builder. Added the property and everything worked.

查看更多
登录 后发表回答