Amazon Elasticsearch Service 入门实践
生活随笔
收集整理的這篇文章主要介紹了
Amazon Elasticsearch Service 入门实践
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Amazon Elasticsearch Service 使用戶能夠在云中輕松地設(shè)置、操作和擴展 Elasticsearch 集群。
第一步:創(chuàng)建
?
?
?
這里面有一個IAM ARN,需要去IAM服務(wù)中尋找,如下圖所示。
訪問策略如下:
{"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/*"}] }?第二步:保存終端節(jié)點和密鑰
創(chuàng)建成功之后有一個終端節(jié)點的鏈接,需要保存下來。
除了這個終端節(jié)點,還有一個區(qū)域也需要記錄。
在IAM服務(wù)中下載密鑰。
?第三步 創(chuàng)建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
這個類是自己實現(xiàn)的,在構(gòu)造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();}} }結(jié)果
總結(jié)
以上是生活随笔為你收集整理的Amazon Elasticsearch Service 入门实践的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在AWS S3的连接过程中发生java.
- 下一篇: 2020年美团春招 技术综合试卷第一题