作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我无法通过 Java API 连接到 vanilla ElasticSearch 集群。
复制:
#start elasticsearch
elasticsearch -f
#checking in a new window
$ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{\
"user" : "kimchy",\
"post_date" : "2009-11-15T14:12:12",\
"message" : "trying out Elastic Search"\
}'
结果:
{
"ok": true,
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_version": 3
}
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_score": 0.30685282,
"_source": {
"user": "kimchy",
"post_date": "2009-11-15T14:12:12",
"message": "trying out Elastic Search"
}
}
]
}
}
所以,一切都通过 HTTP 进行。通过 Java 尝试(根据 this page ):
public static void main(String[] args) {
Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress("localhost", 9200));
IndexResponse response = null;
try {
response = client.prepareIndex("twitter", "tweet", "1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("user", "john")
.field("postDate", new Date())
.field("message", "who dont it work")
.endObject()
)
.execute()
.actionGet();
} catch (ElasticSearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(response);
}
我得到以下堆栈跟踪:
May 21, 2013 8:27:42 AM org.elasticsearch.plugins
INFO: [Bes] loaded [], sites []
May 21, 2013 8:27:49 AM org.elasticsearch.client.transport
INFO: [Bes] failed to get node info for [#transport#-1][inet[localhost/127.0.0.1:9200]], disconnecting...
org.elasticsearch.transport.ReceiveTimeoutTransportException: [][inet[localhost/127.0.0.1:9200]][cluster/nodes/info] request_id [0] timed out after [5002ms]
at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:342)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: No node available
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202)
at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:106)
at org.elasticsearch.client.support.AbstractClient.index(AbstractClient.java:84)
at org.elasticsearch.client.transport.TransportClient.index(TransportClient.java:310)
at org.elasticsearch.action.index.IndexRequestBuilder.doExecute(IndexRequestBuilder.java:315)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:57)
at Scratch.main(Scratch.java:30)
到目前为止,我发现最接近这个问题的是 here ,但线程拖尾没有解决。
最佳答案
TransportClient 默认端口是 9300。您必须在 Java 代码中使用它而不是 9200。这可能是连接失败的原因。
关于java - 为什么我无法通过 Java API 连接到 ElasticSearch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16670219/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!