如何使用弹性 java 客户端与 aws elasticsearch 服务交谈? [英] How to talk to aws elasticsearch service using elastic java client?

查看:36
本文介绍了如何使用弹性 java 客户端与 aws elasticsearch 服务交谈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 AWS elasticsearch 服务(不是 EC2)设置了一个 elasticsearch 服务器.它给了我一个端点 https://xxx-xxxxxxxx.us-west-2.es.amazonaws.com/ 如果我点击这个端点(注意没有指定端口)我可以得到预期的

I have set up a elasticsearch server using AWS elasticsearch service (Not EC2). It gave me an endpoint https://xxx-xxxxxxxx.us-west-2.es.amazonaws.com/ and if I click this endpoint(Note that there is no port specified) I can get the expected

{
  status: 200,
  name: "Mastermind",
  cluster_name: "xxxx",
  version: {
    number: "1.5.2",
    build_hash: "yyyyyy",
    build_timestamp: "2015-04-27T09:21:06Z",
    build_snapshot: false,
    lucene_version: "4.10.4"
  },
  tagline: "You Know, for Search"
}

问题是如何在没有端口号的情况下通过 elasticsearch java 客户端获取此信息?我得到的示例代码是

The question is how do I get this through the elasticsearch java client without a port number? The sample code I get is

Client client = TransportClient.builder().build()
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300));

如果我使用此代码并将host1"替换为我的端点,我将得到NoNodeAvailableException"

If I use this code and just replace "host1" with my endpoint, I'll get "NoNodeAvailableException"

ps:我使用的java客户端版本是2.0.0.

ps: The java client version I'm using is 2.0.0.

编辑我最终决定使用 Jest,这是一个 3rd 方 REST 客户端.但是 Brooks 在下面的回答也非常有帮助 - AWS 确实将端口 80 用于 http,将 443 用于 https.我猜对我来说是防火墙.

Edit I finally decided to go with Jest, a 3rd party REST client. But what Brooks answered below is also very helpful - AWS do use port 80 for http and 443 for https. The blocker for me was the firewall I guess.

编辑 2AWS ES 服务文档明确指出:
该服务支持端口 80 上的 HTTP,但不支持 TCP 传输.

Edit2 The AWS ES service documentation explicitly says:
The service supports HTTP on port 80, but does not support TCP transport.

推荐答案

信不信由你,AWS 不会使用 9200 和 9300 启动 Elasticsearch.它是通过普通的旧端口 80 启动的.

Believe it or not, AWS doesn't launch Elasticsearch using 9200 and 9300. It's launched via plain old port 80.

所以,为了演示,试试这个...

So, to demonstrate, try this...

curl -XPOST "http://xxx-xxxxxxxx.us-west-2.es.amazonaws.com:80/myIndex/myType" -d '["name":"Edmond"}'

curl -XPOST "https://xxx-xxxxxxxx.us-west-2.es.amazonaws.com:443/myIndex/myType" -d '["name":"Edmond"}'

它应该回应:{"_index":"myIndex","_type":"myType","_id":"SOME_ID_#","_version":1,"created":true}

It should respond with: {"_index":"myIndex","_type":"myType","_id":"SOME_ID_#","_version":1,"created":true}

签入 Kibana,您会看到它就在那里.

Check in Kibana and you'll see it's there.

那么,在你的代码中,它应该是:

So, then in your code, it should be:

Client client = TransportClient.builder().build()
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("xxx-xxxxxxxx.us-west-2.es.amazonaws.com"), 80));

不幸的是,我不知道如何使用传输客户端通过 SSL/HTTPS 进行加密传输.您可以尝试使用常规 REST 调用代替 JERSEY.

Unfortunately, I don't off-hand know how to transmit encrypted via SSL/HTTPS using the transport client. You could try using regular REST calls instead using JERSEY.

最后,确保您的 Elasticsearch 访问策略配置正确.类似的东西:

Finally, make sure your Elasticsearch access policy is configured properly. Something along the lines of:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "es:*",
      "Resource": "arn:aws:es:us-east-1:yyyyyyy:domain/myDomain/*"
    },
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "es:*",
      "Resource": "arn:aws:es:us-east-1:yyyyyyyyy:domain/myDomain"
    }
  ]
}

注意:上述访问策略是完全开放的,不建议用于远程接近生产的任何内容.只是让你知道....

NOTE: The above access policy is completely wide open and is not recommended for anything remotely close to production. Just so you know....

这篇关于如何使用弹性 java 客户端与 aws elasticsearch 服务交谈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆