· 7 years ago · Mar 29, 2018, 09:32 AM
1public void getIndexedRecords(String index,String type,String id,String documentJSON){
2
3 try {
4 String endpoint = host +"/" +index+"/_search" ;
5 // Builds the request. We need an AWS service, URI, HTTP method, and request
6 // body (in this case, JSON).
7
8 Request<?> request = new DefaultRequest<Void>(service);
9 request.setEndpoint(new URI(endpoint));
10 request.setHttpMethod(HttpMethodName.GET);
11 request.setContent(new ByteArrayInputStream(documentJSON.getBytes()));
12
13 // Retrieves our credentials from the computer. For more information on where
14 // this class looks for credentials, see
15 // http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html.
16
17 String accessKey = CommonUtils.getCommonProperty("accessKey", "");
18 String secretKey = CommonUtils.getCommonProperty("secretKey", "");;
19 AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
20
21 // Signs the request using our region, service, and credentials. AWS4Signer
22 // modifies the original request rather than returning a new request.
23
24 AWS4Signer signer = new AWS4Signer();
25 signer.setRegionName(region);
26 signer.setServiceName(service);
27 signer.sign(request, creds);
28 request.addHeader("Content-Type", "application/json");
29
30 // Creates and configures the HTTP client, creates the error and response
31 // handlers, and finally executes the request.
32
33 ClientConfiguration config = new ClientConfiguration();
34 config.setProtocol(Protocol.HTTPS);
35 AmazonHttpClient client = new AmazonHttpClient(config);
36 System.out.println(client);
37 ExecutionContext context = new ExecutionContext(true);
38 ESAWSErrorHandler errorHandler = new ESAWSErrorHandler();
39 ESAWSResponseHandler<Void> responseHandler = new ESAWSResponseHandler<Void>();
40 client.requestExecutionBuilder().executionContext(context).errorResponseHandler(errorHandler).request(request)
41 .execute(responseHandler);
42 } catch (Exception e) {
43 e.printStackTrace();
44 }
45
46
47 }