private Map<String, Object> map(final Response response) throws IOException { try (InputStream is = response.getEntity().getContent()) { final ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") final Map<String, Object> map = mapper.readValue(is, Map.class); return map; } }
private Map<String, Object> parseResponse(Response response) throws IOException { final int code = response.getStatusLine().getStatusCode(); if (code >= 200 & code < 300) { InputStream inputStream = response.getEntity().getContent(); byte[] result = IOUtils.toByteArray(inputStream); inputStream.close(); return mapper.readValue(new String(result, charset), Map.class); } else { String errorMessage = String.format("ElasticSearch reported an error while trying to run the query: %s", response.getStatusLine().getReasonPhrase()); throw new IOException(errorMessage); } }
@Override public Collection<Values> toValues(Response response) { LookupResponse lookupResponse; try { lookupResponse = objectMapper.readValue(response.getEntity().getContent(), LookupResponse.class); } catch (UnsupportedOperationException | IOException e) { throw new IllegalArgumentException("Response " + response + " is invalid", e); } return Collections.singleton(new Values( lookupResponse.getIndex(), lookupResponse.getType(), lookupResponse.getId(), lookupResponse.getSource())); }
/** * Store current state to ElasticSearch. * * @param tuples list of tuples for storing to ES. * Each tuple should have relevant fields (source, index, type, id) for EsState's tupleMapper to extract ES document. */ public void updateState(List<TridentTuple> tuples) { try { String bulkRequest = buildRequest(tuples); Response response = client.performRequest("post", "_bulk", new HashMap<>(), new StringEntity(bulkRequest.toString())); BulkIndexResponse bulkResponse = objectMapper.readValue(response.getEntity().getContent(), BulkIndexResponse.class); if (bulkResponse.hasErrors()) { LOG.warn("failed processing bulk index requests: " + bulkResponse.getFirstError() + ": " + bulkResponse.getFirstResult()); throw new FailedException(); } } catch (IOException e) { LOG.warn("failed processing bulk index requests: " + e.toString()); throw new FailedException(e); } } }
@Override // TODO change interface to return Settings. public Map getSetting(String indexName) { Assert.notNull(indexName, "No index defined for getSettings"); ObjectMapper objMapper = new ObjectMapper(); Map settings = null; RestClient restClient = client.getLowLevelClient(); try { Response response = restClient.performRequest("GET", "/" + indexName + "/_settings"); settings = convertSettingResponse(EntityUtils.toString(response.getEntity()), indexName); } catch (Exception e) { throw new ElasticsearchException("Error while getting settings for indexName : " + indexName, e); } return settings; }
@Override public Map getMapping(String indexName, String type) { Assert.notNull(indexName, "No index defined for getMapping()"); Assert.notNull(type, "No type defined for getMapping()"); Map mappings = null; RestClient restClient = client.getLowLevelClient(); try { Response response = restClient.performRequest("GET", "/" + indexName + "/_mapping/" + type); mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()), type); } catch (Exception e) { throw new ElasticsearchException( "Error while getting mapping for indexName : " + indexName + " type : " + type + " ", e); } return mappings; }
JsonNode root = objectMapper.readTree(EntityUtils.toString(errorResponse.getEntity())); String errorCode = root.get("error").get("type").asText(); if ("index_already_exists_exception".equals(errorCode)) {
@Override public List<AliasMetaData> queryForAlias(String indexName) { List<AliasMetaData> aliases = null; RestClient restClient = client.getLowLevelClient(); Response response; String aliasResponse; try { response = restClient.performRequest("GET", "/" + indexName + "/_alias/*"); aliasResponse = EntityUtils.toString(response.getEntity()); } catch (Exception e) { throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName, e); } return convertAliasResponse(aliasResponse); }
private void deleteAllIndices() throws IOException { Response beforeResponse = restClient.performRequest(HttpMethod.GET, "/_cat/indices"); Reader streamReader = new InputStreamReader(beforeResponse.getEntity().getContent()); BufferedReader bufferedReader = new BufferedReader(streamReader); String line; while ((line = bufferedReader.readLine()) != null) { String[] fields = line.split("\\s"); String endpoint = String.format("/%s", fields[2]); restClient.performRequest(HttpMethod.DELETE, endpoint); } }
/** * {@inheritDoc} * Tuple should have relevant fields (source, index, type) for storeMapper to extract ES document.<br/> * If there exists non-empty percolate response, EsPercolateBolt will emit tuple with original source * and Percolate.Match for each Percolate.Match in PercolateResponse. */ @Override public void process(Tuple tuple) { try { String source = tupleMapper.getSource(tuple); String index = tupleMapper.getIndex(tuple); String type = tupleMapper.getType(tuple); Map<String, String> indexParams = new HashMap<>(); indexParams.put(type, null); String percolateDoc = "{\"doc\": " + source + "}"; Response response = client.performRequest("get", getEndpoint(index, type, "_percolate"), new HashMap<>(), new StringEntity(percolateDoc)); PercolateResponse percolateResponse = objectMapper.readValue(response.getEntity().getContent(), PercolateResponse.class); if (!percolateResponse.getMatches().isEmpty()) { for (PercolateResponse.Match match : percolateResponse.getMatches()) { collector.emit(new Values(source, match)); } } collector.ack(tuple); } catch (Exception e) { collector.reportError(e); collector.fail(tuple); } }
@Test public void elasticsearchVersion() throws IOException { try (ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:5.6.12")) { container.start(); Response response = getClient(container).performRequest(new Request("GET", "/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); String responseAsString = EntityUtils.toString(response.getEntity()); assertThat(responseAsString, containsString("5.6.12")); } }
@Test public void elasticsearchDefaultTest() throws IOException { try (ElasticsearchContainer container = new ElasticsearchContainer() .withEnv("foo", "bar") // dummy env for compiler checking correct generics usage ) { container.start(); Response response = getClient(container).performRequest(new Request("GET", "/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(EntityUtils.toString(response.getEntity()), containsString(ELASTICSEARCH_DEFAULT_VERSION)); // The default image is running with the features under Elastic License response = getClient(container).performRequest(new Request("GET", "/_xpack/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); // For now we test that we have the monitoring feature available assertThat(EntityUtils.toString(response.getEntity()), containsString("monitoring")); } }
Response response = restClient.performRequest(HttpMethod.GET, resourcePath); String responseBody = IOUtils.toString(response.getEntity().getContent()); logger.info("responseBody: {}", responseBody);
Response response = restClient.performRequest(HttpMethod.GET, resourcePath); String responseBody = IOUtils.toString(response.getEntity().getContent()); TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
/** * Gets ALL Elasticsearch indices, or null if status code returned is not OK 200. */ public String[] getIndices() throws IOException { Response response = lowLevelClient.performRequest("GET", "/_cat/indices"); if(response.getStatusLine().getStatusCode() == 200) { String responseStr = IOUtils.toString(response.getEntity().getContent()); List<String> indices = new ArrayList<>(); for(String line : Splitter.on("\n").split(responseStr)) { Iterable<String> splits = Splitter.on(" ").split(line.replaceAll("\\s+", " ").trim()); if(Iterables.size(splits) > 3) { String index = Iterables.get(splits, 2, ""); if(!StringUtils.isEmpty(index)) { indices.add(index.trim()); } } } String[] ret = new String[indices.size()]; ret=indices.toArray(ret); return ret; } return null; }
/** * https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-put-mapping.html * @param index * @param mappingType https://www.elastic.co/guide/en/elasticsearch/reference/5.6/mapping.html#mapping-type * @param mapping * @throws IOException */ public void putMapping(String index, String mappingType, String mapping) throws IOException { HttpEntity entity = new StringEntity(mapping); Response response = lowLevelClient.performRequest("PUT" , "/" + index + "/_mapping/" + mappingType , Collections.emptyMap() , entity ); if(response.getStatusLine().getStatusCode() != 200) { String responseStr = IOUtils.toString(response.getEntity().getContent()); throw new IllegalStateException("Got a " + response.getStatusLine().getStatusCode() + " due to " + responseStr); } }
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { Response response = this.client .performRequest(new Request("GET", "/_cluster/health/")); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() != HttpStatus.SC_OK) { builder.down(); builder.withDetail("statusCode", statusLine.getStatusCode()); builder.withDetail("reasonPhrase", statusLine.getReasonPhrase()); return; } try (InputStream inputStream = response.getEntity().getContent()) { doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8)); } }