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
Some suggestions:
1 - Use port 9300. [9300-9400] is for node-to-node communication, [9200-9300] is for HTTP traffic.
2 - Ensure the version of the Java API you are using matches the version of elasticsearch running on the server.
3 - Ensure that the name of your cluster is foxzen
(check the elasticsearch.yml on the server).
4 - Remove put("node.name", "yu")
, you aren't joining the cluster as a node since you are using the TransportClient
, and even if you were it appears your server node is named yu
so you would want a different node name in any case.
You need to change your code to use port 9300 - correct line would be:
Client client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("XXX.XXX.XXX.XXX", 9300));
The reason is that the Java API is using the internal transport used for inter node communications and it defaults to port 9300. Port 9200 is the default for the REST API interface. Common issue to run into - check this sample code here towards the bottom of the page, under Transport Client:
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html
// on startup
Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress("host1", 9300))
.addTransportAddress(new InetSocketTransportAddress("host2", 9300));
// on shutdown
client.close();
I met this error too. I use ElasticSearch 2.4.1 as a standalone server (single node) in docker, programming with Grails 3/spring-data-elasticsearch. My fix is setting client.transport.sniff
to false
. Here is my core conf :
application.yml
spring.data.elasticsearch:
cluster-name: "my-es"
cluster-nodes: "localhost:9300"
properties:
"client.transport.ignore_cluster_name": true
"client.transport.nodes_sampler_interval": "5s"
"client.transport.ping_timeout": "5s"
"client.transport.sniff": false # XXX : notice here
repositories.enabled: false
See this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With