当前位置: 首页 > 面试题库 >

为什么我不能通过Java API连接到ElasticSearch?

尚阳炎
2023-03-14
问题内容

我无法通过Java API连接到原始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尝试(每个页面):

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)

与最接近的事我发现,到目前为止,这个问题是在这里,但线程落后了,但未得到解决。


问题答案:

TransportClient的默认端口为9300。您必须在Java代码中使用它而不是9200。这可能是连接失败的原因。



 类似资料:
  • 我无法通过Java API连接到vanilla ElasticSearch集群。 复制: 我得到以下堆栈跟踪: 到目前为止,我发现的最接近这个问题的东西是这里,但线程拖尾没有解决。

  • 我可以使用SQLDeveloper连接到远程数据库。 我试图从命令行使用sqlcl连接到同一个数据库,但我收到一个错误。 下面是我正在运行的命令: 我也尝试过: 以下是我收到的错误: 同样在SQLDeveloper中,我只是在“自定义jdbc url”下输入以下内容,它连接没有任何问题,所以我希望我可以使用相同的URL通过命令行连接,但到目前为止,它不起作用:

  • 我了解在lambda中捕获此(修改对象属性)的正确方法如下: 但我对我所看到的以下特点感到好奇: 我感到困惑(并希望得到回答)的奇怪之处在于,为什么以下方法有效: 以及为什么我无法通过引用明确捕获此内容:

  • 我正在尝试使用python和MySQL开发药房管理系统。但我在连接数据库时遇到了问题。 下面是一些代码:

  • 我正在尝试用我用给定的主机名设置的用户名和密码进行连接。我无法连接。我已经检查了安全组在端口的配置是否正确,允许从“我的IP”传入,它在那里填充了我的IP。 我还会做错什么? 当我尝试在终端中使用以下命令进入DB时: 我得到:上的MySQL服务器

  • 当我单击“确定”时,我收到一条错误消息:“另一个应用程序正在侦听端口8888。请检查您的端口配置”。 我还成功地用8888和Java任务控制(JMC)评测了这台机器。然而,JMC要求我提供用户名和密码,然后才允许连接。