Amazon Elasticsearch Service 入门实践
Amazon Elasticsearch Service 使用户能够在云中轻松地设置、操作和扩展 Elasticsearch 集群。
第一步:创建
这里面有一个IAM ARN,需要去IAM服务中寻找,如下图所示。
访问策略如下:
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"AWS": "arn:aws:iam::767504256855:user/yucheng"},"Action": "es:*","Resource": "arn:aws:es:us-east-1:767504256855:domain/yucheng/*"}]
}
第二步:保存终端节点和密钥
创建成功之后有一个终端节点的链接,需要保存下来。
除了这个终端节点,还有一个区域也需要记录。
在IAM服务中下载密钥。
第三步 创建JAVA的maven工程与编码
maven配置
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build><dependencies><dependency><groupId>com.amazonaws</groupId><artifactId>aws-java-sdk-core</artifactId><version>1.11.327</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.1.1</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.1.1</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.1.1</version></dependency></dependencies>
编码测试。
AWSRequestSigningApacheInterceptor
这个类是官方提供的,需直接拷贝。
/** Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.** Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with* the License. A copy of the License is located at** http://aws.amazon.com/apache2.0** or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions* and limitations under the License.*/import com.amazonaws.DefaultRequest;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.Signer;
import com.amazonaws.http.HttpMethodName;
import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;import static org.apache.http.protocol.HttpCoreContext.HTTP_TARGET_HOST;/*** An {@link HttpRequestInterceptor} that signs requests using any AWS {@link Signer}* and {@link AWSCredentialsProvider}.*/
public class AWSRequestSigningApacheInterceptor implements HttpRequestInterceptor {/*** The service that we're connecting to. Technically not necessary.* Could be used by a future Signer, though.*/private final String service;/*** The particular signer implementation.*/private final Signer signer;/*** The source of AWS credentials for signing.*/private final AWSCredentialsProvider awsCredentialsProvider;/**** @param service service that we're connecting to* @param signer particular signer implementation* @param awsCredentialsProvider source of AWS credentials for signing*/public AWSRequestSigningApacheInterceptor(final String service,final Signer signer,final AWSCredentialsProvider awsCredentialsProvider) {this.service = service;this.signer = signer;this.awsCredentialsProvider = awsCredentialsProvider;}/*** {@inheritDoc}*/@Overridepublic void process(final HttpRequest request, final HttpContext context)throws HttpException, IOException {URIBuilder uriBuilder;try {uriBuilder = new URIBuilder(request.getRequestLine().getUri());} catch (URISyntaxException e) {throw new IOException("Invalid URI" , e);}// Copy Apache HttpRequest to AWS DefaultRequestDefaultRequest<?> signableRequest = new DefaultRequest<>(service);HttpHost host = (HttpHost) context.getAttribute(HTTP_TARGET_HOST);if (host != null) {signableRequest.setEndpoint(URI.create(host.toURI()));}final HttpMethodName httpMethod =HttpMethodName.fromValue(request.getRequestLine().getMethod());signableRequest.setHttpMethod(httpMethod);try {signableRequest.setResourcePath(uriBuilder.build().getRawPath());} catch (URISyntaxException e) {throw new IOException("Invalid URI" , e);}if (request instanceof HttpEntityEnclosingRequest) {HttpEntityEnclosingRequest httpEntityEnclosingRequest =(HttpEntityEnclosingRequest) request;if (httpEntityEnclosingRequest.getEntity() != null) {signableRequest.setContent(httpEntityEnclosingRequest.getEntity().getContent());}}signableRequest.setParameters(nvpToMapParams(uriBuilder.getQueryParams()));signableRequest.setHeaders(headerArrayToMap(request.getAllHeaders()));// Sign itsigner.sign(signableRequest, awsCredentialsProvider.getCredentials());// Now copy everything backrequest.setHeaders(mapToHeaderArray(signableRequest.getHeaders()));if (request instanceof HttpEntityEnclosingRequest) {HttpEntityEnclosingRequest httpEntityEnclosingRequest =(HttpEntityEnclosingRequest) request;if (httpEntityEnclosingRequest.getEntity() != null) {BasicHttpEntity basicHttpEntity = new BasicHttpEntity();basicHttpEntity.setContent(signableRequest.getContent());httpEntityEnclosingRequest.setEntity(basicHttpEntity);}}}/**** @param params list of HTTP query params as NameValuePairs* @return a multimap of HTTP query params*/private static Map<String, List<String>> nvpToMapParams(final List<NameValuePair> params) {Map<String, List<String>> parameterMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);for (NameValuePair nvp : params) {List<String> argsList =parameterMap.computeIfAbsent(nvp.getName(), k -> new ArrayList<>());argsList.add(nvp.getValue());}return parameterMap;}/*** @param headers modeled Header objects* @return a Map of header entries*/private static Map<String, String> headerArrayToMap(final Header[] headers) {Map<String, String> headersMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);for (Header header : headers) {if (!skipHeader(header)) {headersMap.put(header.getName(), header.getValue());}}return headersMap;}/*** @param header header line to check* @return true if the given header should be excluded when signing*/private static boolean skipHeader(final Header header) {return ("content-length".equalsIgnoreCase(header.getName())&& "0".equals(header.getValue())) // Strip Content-Length: 0|| "host".equalsIgnoreCase(header.getName()); // Host comes from endpoint}/*** @param mapHeaders Map of header entries* @return modeled Header objects*/private static Header[] mapToHeaderArray(final Map<String, String> mapHeaders) {Header[] headers = new Header[mapHeaders.size()];int i = 0;for (Map.Entry<String, String> headerEntry : mapHeaders.entrySet()) {headers[i++] = new BasicHeader(headerEntry.getKey(), headerEntry.getValue());}return headers;}
}
ESClient
这个类是自己实现的,在构造RestHighLevelClient之时,与普通Elasticsearch不同的是需要添加一些认证的操作。
import com.amazonaws.auth.AWS4Signer;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class ESClient {private static String AWS_ACCESS_KEY_ID = "XXX";//替换成自己的private static String AWS_SECRET_ACCESS_KEY = "XXXXX";//替换成自己的public static RestHighLevelClient getClient() {AWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);AWS4Signer signer = new AWS4Signer();AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);signer.setServiceName("es");signer.setRegionName("us-east-1");HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor("es", signer, credentialsProvider);return new RestHighLevelClient(RestClient.builder(HttpHost.create("https://search-yucheng-wnevxf2cs6ijij6lloqgbf537u.us-east-1.es.amazonaws.com")).setHttpClientConfigCallback(hacb -> hacb.addInterceptorLast(interceptor)));}public static void main(String[] args) {RestHighLevelClient client = ESClient.getClient();Map<String, Object> map = new HashMap<>();map.put("name", "yucheng");IndexRequest indexRequest = new IndexRequest("yucheng").source(map);try {IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);System.out.println(indexResponse);} catch (IOException e) {e.printStackTrace();}}
}
结果
Amazon Elasticsearch Service 入门实践相关推荐
- ElasticSearch最佳入门实践(一)什么是ElasticSearch
ElasticSearch ElasticSearch是一种分布式.高可用.高性能.可伸缩的搜索和分析系统. 什么是搜索 百度:比如我们想搜索任何信息的时候,就会上百度搜索一下.例如找一部自己喜欢的电 ...
- AWS - ElasticSearch服务入门
Amazon Elasticsearch Service (Amazon ES) 是一种托管服务,可以让您轻松在AWS云中部署.操作和扩展 Elasticsearch 集群.Elasticsearch ...
- android hook底层代码_Java-Hook技术-入门实践(反射、动态代理)-Hook拦截通知(当前App/Context)...
老样子,上一篇MonkeyLei:Java-Hook技术-入门实践+反射.动态代理.热修复再看看 我们Hook学习了一下,一个是Java本地Main的实践练习. 一个是Android的监听事件的Hoo ...
- aidl使用_Android-Service学习鸭-入门实践-远程服务(bindService方式-AIDL)
延续上一篇MonkeyLei:Android-Service学习鸭-入门实践-本地服务(bindService方式) ,我接着实践下远程服务的使用(有个疑问先搁这,远程服务到底有哪些使用场景?). 不 ...
- 【转】推荐系统入门实践:世纪佳缘会员推荐(完整版)
推荐系统入门实践:世纪佳缘会员推荐(完整版) 版本 作者 联系 日期 1.0 周巍然 weiran.chow@gmail.com 20120723 2.0 严 程 supersteven198701@ ...
- 基于qiankun搭建ng-alain15微前端项目入门实践
基础环境 实践日期:2023-02-22 ng versionAngular CLI: 15.1.6 Node: 18.14.2 Package Manager: npm 9.5.0 OS: win3 ...
- kubernetes 入门实践-搭建集群
ㅤㅤㅤ ㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ(一个人的真正伟大之处就在于他能够认识到自己的渺小 -- 保罗) ㅤㅤㅤ ㅤㅤㅤ ㅤㅤㅤㅤㅤㅤㅤㅤㅤ 上一篇:kubernetes 入门实践-核心概念 下 ...
- ElasticSearch第一讲:ElasticSearch从入门到精通
ElasticSearch第一讲:ElasticSearch从入门到精通 业内目前来说事实上的一个标准,就是分布式搜索引擎一般大家都用elasticsearch.本文是ElasticSearch第一讲 ...
- C# Windows Service入门
C# Windows Service入门 https://www.cnblogs.com/FreeLoopPowter/p/15598294.html Microsoft Windows 服务(过去称 ...
最新文章
- 浅析网站如何快速提升收录量?
- Git笔记(一)——[commit, checkout]
- wxpython动态实时绘图_wxPython: 简单的绘图例子
- Spark _18 _Shuffle文件寻址
- 月入过万?就靠SEO了
- Go error 处理实践
- python画中秋的月亮_水彩中秋月亮教程
- 分享一个NHibernate的博客链接
- 微服务的4个设计原则和19个解决方案
- FailSafe双机方案
- php 导入excal,php导入excel php使用phpexcel导入excel文件
- not是什么意思在c语言,为什么在C样式语言中逻辑NOT运算符是“!”而不是“ ~~”?...
- 前端所有安全问题总结
- 什么是webservice
- Workbench网格分块分区划分 扫面、节点控制网格数的应用
- 月份 星期 英文描述
- Unity Shader通过菲涅尔反射和散射实现玻璃效果
- Activity 会签
- Springboot 整合百度地图 API
- 通过AD域验证登录Linux系统(Linux安装sssd加入Windows AD域)
热门文章
- http和https的区别和联系
- python opencv 官方文档里LaTeX公式不能正常显示怎么办?
- tensorflow GPU笔记
- Markdown 如何实现空行、空格?
- tensorflow:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
- mybatis批量更新报错XXXXX-Inline
- java数字转大写 其他报异常_【踩坑系列】使用long类型处理金额,科学计数法导致金额转大写异常...
- Linux运维:如何根据端口号查看哪个应用占用?
- mysql 报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executin
- java中getchars方法_Java getChars() 方法