org.elasticsearch.client.Response Java Examples

The following examples show how to use org.elasticsearch.client.Response. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
private Response doPerformRequest(String method, String path) throws IOException, ExecutionException {
    if (async) {
        final CompletableFuture<Response> resultFuture = new CompletableFuture<>();
        lowLevelClient.performRequestAsync(method, path, new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                resultFuture.complete(response);
            }

            @Override
            public void onFailure(Exception exception) {
                resultFuture.completeExceptionally(exception);
            }
        });
        try {
            return resultFuture.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return lowLevelClient.performRequest(method, path);
}
 
Example #2
Source File: DocumentService.java    From elastic-rabbitmq with MIT License 6 votes vote down vote up
public ESDeleteResponse Delete(String index, String type, Long sourceId, Map<String, String> params) {
    params = addTenantId2Param(params);
    try {
        Response response = client.performRequest(
                "DELETE",
                index + "/" + type + "/" + sourceId,
                params);

        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode > 299) {
            logger.warn("Problem while indexing a document: {}" + response.getStatusLine().getReasonPhrase());
            throw new ElasticAPIException("Could not index a document, status code is " + statusCode);
        }

        ESDeleteResponse deleteResponse = gson.fromJson(IOUtils.toString(response.getEntity().getContent()),
                ESDeleteResponse.class);
        return deleteResponse;
    } catch (IOException e) {
        logger.error("Failed to delete document with type [" + type + "] id ["
                + sourceId + "]: ",e);
    }
    return null;
}
 
Example #3
Source File: DocumentService.java    From elastic-rabbitmq with MIT License 6 votes vote down vote up
public ESMultiGetResponse multiGet(String index, String type, HashMap<String, String> params, String requestBody) {
    Map<String, String> param = addTenantId2Param(params);

    try {
        HttpEntity requestEntity = new StringEntity(requestBody);
        Response response = client.performRequest(
                "POST",
                index + "/" + type + "/_mget",
                param,
                requestEntity);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            logger.warn("Not found" + response.toString());
            return null;
        }

        logger.info("Got response :{}" + response.getEntity().toString());
        String resStr = IOUtils.toString(response.getEntity().getContent());
        return gson.fromJson(resStr, ESMultiGetResponse.class);
    } catch (IOException e) {
        logger.error("Failed to get document with type [" + type + "] ",e);
    }

    return null;
}
 
Example #4
Source File: ElasticSearchManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch data and scroll id.
 *
 * @param endPoint the end point
 * @param _data the data
 * @param keyField the key field
 * @param payLoad the pay load
 * @return the string
 */
private static String fetchDataAndScrollId(String endPoint, Map<String, Map<String, String>> _data, String keyField,
        String payLoad) {
    try {
        ObjectMapper objMapper = new ObjectMapper();
        Response response = invokeAPI("GET", endPoint, payLoad);
        String responseJson = EntityUtils.toString(response.getEntity());
        JsonNode _info = objMapper.readTree(responseJson).at("/hits/hits");
        String scrollId = objMapper.readTree(responseJson).at("/_scroll_id").textValue();
        Iterator<JsonNode> it = _info.elements();
        while (it.hasNext()) {
            String doc = it.next().fields().next().getValue().toString();
            Map<String, String> docMap = new ObjectMapper().readValue(doc,
                    new TypeReference<Map<String, String>>() {
                    });
            _data.put(docMap.get(keyField), docMap);
            docMap.remove(keyField);
        }
        return scrollId;
    } catch (ParseException | IOException e) {
        LOGGER.error("Error fetchDataAndScrollId ", e);
    }
    return "";

}
 
Example #5
Source File: ElasticsearchTable.java    From Quicksql with MIT License 6 votes vote down vote up
private ElasticsearchJson.Result httpRequest(ObjectNode query) throws IOException {
  Objects.requireNonNull(query, "query");
  String uri = String.format(Locale.ROOT, "/%s/%s/_search", indexName, typeName);

  Hook.QUERY_PLAN.run(query);
  final String json = mapper.writeValueAsString(query);

  LOGGER.debug("Elasticsearch Query: {}", json);

  HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  Response response = restClient.performRequest("POST", uri, Collections.emptyMap(), entity);
  if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    final String error = EntityUtils.toString(response.getEntity());
    final String message = String.format(Locale.ROOT,
        "Error while querying Elastic (on %s/%s) status: %s\nQuery:\n%s\nError:\n%s\n",
        response.getHost(), response.getRequestLine(), response.getStatusLine(), query, error);
    throw new RuntimeException(message);
  }

  try (InputStream is = response.getEntity().getContent()) {
    return mapper.readValue(is, ElasticsearchJson.Result.class);
  }
}
 
Example #6
Source File: MetricStoreImpl.java    From griffin with Apache License 2.0 6 votes vote down vote up
private ResponseEntity<?> getResponseEntityFromResponse(Response response)
    throws IOException {
    String body = EntityUtils.toString(response.getEntity());
    HttpStatus status = HttpStatus.valueOf(response.getStatusLine()
        .getStatusCode());
    return new ResponseEntity<>(body, responseHeaders, status);
}
 
Example #7
Source File: ElasticsearchDatastoreRuntime.java    From components with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<ValidationResult> doHealthChecks(RuntimeContainer container) {
    ValidationResult validationResult;
    try (RestClient client = ElasticsearchConnection.createClient(properties)) {
        Response response = client.performRequest("GET", "/_cluster/health", new HashMap<String, String>(),
                new BasicHeader("", ""));
        ElasticsearchResponse esResponse = new ElasticsearchResponse(response);
        if (esResponse.isOk()) {
            JsonNode entity = esResponse.getEntity();
            String status = entity.path("status").asText();
            if (status != "red") {
                validationResult = ValidationResult.OK;
            } else {
                validationResult = new ValidationResult(TalendRuntimeException.createUnexpectedException(
                        String.format("Cluster %s status is red", entity.path("cluster_name").asText())));
            }
        } else {
            validationResult = new ValidationResult(
                    TalendRuntimeException.createUnexpectedException(esResponse.getStatusLine().toString()));
        }
    } catch (IOException e) {
        validationResult = new ValidationResult(TalendRuntimeException.createUnexpectedException(e.getMessage()));
    }
    return Arrays.asList(validationResult);
}
 
Example #8
Source File: IndexService.java    From elastic-rabbitmq with MIT License 6 votes vote down vote up
public Boolean indexExist(String indexName) {
    try {
        Response response = client.performRequest("HEAD", indexName);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            return true;
        } else if (statusCode == 404) {
            return false;
        }
        logger.error("Error checking index existence: {}" + response.getStatusLine().getReasonPhrase());

    } catch (IOException e) {
        logger.error("Failed to verify the index existence ", e);
    }

    return true;
}
 
Example #9
Source File: LoadData.java    From elastic-rabbitmq with MIT License 6 votes vote down vote up
public ESMultiGetResponse multiGet(HashMap<String, String> params, String requestBody) {
    try {
        HttpEntity requestEntity = new StringEntity(requestBody);
        Response response = client.performRequest(
                "POST",
                ESConstants.STORE_INDEX + "/" + ESConstants.SKU_TYPE + "/_mget",
                params,
                requestEntity);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            logger.warn("Not found" + response.toString());
            return null;
        }
        String resStr = IOUtils.toString(response.getEntity().getContent());
        return gson.fromJson(resStr, ESMultiGetResponse.class);
    } catch (IOException e) {
        logger.error("Failed to get document with type  " + e);
    }

    return null;
}
 
Example #10
Source File: ESManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch data and scroll id.
 *
 * @param endPoint
 *            the end point
 * @param _data
 *            the data
 * @param keyField
 *            the key field
 * @param payLoad
 *            the pay load
 * @return the string
 */
private static String fetchDataAndScrollId(String endPoint, List<Map<String, String>> _data, String payLoad) {
    try {
        ObjectMapper objMapper = new ObjectMapper();
        Response response = invokeAPI("GET", endPoint, payLoad);
        String responseJson = EntityUtils.toString(response.getEntity());
        JsonNode _info = objMapper.readTree(responseJson).at("/hits/hits");
        String scrollId = objMapper.readTree(responseJson).at("/_scroll_id").textValue();
        Iterator<JsonNode> it = _info.elements();
        String doc;
        Map<String, String> docMap;
        while (it.hasNext()) {
            doc = it.next().fields().next().getValue().toString();
            docMap = objMapper.readValue(doc, new TypeReference<Map<String, String>>() {
            });
            docMap.remove("discoverydate");
            docMap.remove("_loaddate");
            docMap.remove("id");
            _data.add(docMap);
        }
        return scrollId;
    } catch (ParseException | IOException e) {
        LOGGER.error("Error in fetchDataAndScrollId" ,e );
    }
    return "";
}
 
Example #11
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobScopeExcludeAndIncludeAttributesTerms() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_SCOPE_EXCLUDE_AND_INCLUDE_ATTRIBUTES_TERMS);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 4);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a2,0");
        docsExpected.add("b2,0");
        docsExpected.add("c2,0");
        docsExpected.add("d2,0");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #12
Source File: ESManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch data and scroll id.
 *
 * @param endPoint
 *            the end point
 * @param _data
 *            the data
 * @param keyField
 *            the key field
 * @param payLoad
 *            the pay load
 * @return the string
 */
private static String fetchDataAndScrollId(String endPoint, Map<String, Map<String, String>> _data, String keyField,
        String payLoad) {
    try {
        ObjectMapper objMapper = new ObjectMapper();
        Response response = invokeAPI("GET", endPoint, payLoad);
        String responseJson = EntityUtils.toString(response.getEntity());
        JsonNode _info = objMapper.readTree(responseJson).at("/hits/hits");
        String scrollId = objMapper.readTree(responseJson).at("/_scroll_id").textValue();
        Iterator<JsonNode> it = _info.elements();
        String doc;
        Map<String, String> docMap;
        while (it.hasNext()) {
            doc = it.next().fields().next().getValue().toString();
            docMap = objMapper.readValue(doc, new TypeReference<Map<String, String>>() {
            });
            _data.put(docMap.get(keyField), docMap);
            docMap.remove(keyField);
        }
        return scrollId;
    } catch (ParseException | IOException e) {
        LOGGER.error("Error in fetchDataAndScrollId" ,e );
    }
    return "";
}
 
Example #13
Source File: ElasticSearchRepository.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public static void bulkUpload(List<String> errors, String bulkRequest) {
    try {
        System.out.println("********"+bulkRequest);
    	Response resp = invokeAPI("POST", "/_bulk?refresh=true", bulkRequest);
        
        String responseStr = EntityUtils.toString(resp.getEntity());
        if (responseStr.contains("\"errors\":true")) {
        	LOGGER.error(responseStr);
            errors.add(responseStr);
        }
    } catch (Exception e) {
    	e.printStackTrace();
    	LOGGER.error("Bulk upload failed",e);
        errors.add(e.getMessage());
    }
}
 
Example #14
Source File: ElasticsearchClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
private static void onAfterExecute(@Advice.Argument(5) ResponseListener responseListener,
                                   @Advice.Local("span") @Nullable Span span,
                                   @Advice.Local("wrapped") boolean wrapped,
                                   @Advice.Local("helper") @Nullable ElasticsearchRestClientInstrumentationHelper<HttpEntity, Response, ResponseListener> helper,
                                   @Advice.Thrown @Nullable Throwable t) {
    if (span != null) {
        // Deactivate in this thread. Span will be ended and reported by the listener
        span.deactivate();

        if (!wrapped) {
            // Listener is not wrapped- we need to end the span so to avoid leak and report error if occurred during method invocation
            helper.finishClientSpan(null, span, t);
        }
    }
}
 
Example #15
Source File: ElasticsearchClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeExecute(@Advice.Argument(0) String method,
                                    @Advice.Argument(1) String endpoint,
                                    @Advice.Argument(3) @Nullable HttpEntity entity,
                                    @Advice.Argument(value = 5, readOnly = false) ResponseListener responseListener,
                                    @Advice.Local("span") Span span,
                                    @Advice.Local("wrapped") boolean wrapped,
                                    @Advice.Local("helper") ElasticsearchRestClientInstrumentationHelper<HttpEntity, Response, ResponseListener> helper) {

    helper = esClientInstrHelperManager.getForClassLoaderOfClass(Response.class);
    if (helper != null) {
        span = helper.createClientSpan(method, endpoint, entity);
        if (span != null) {
            responseListener = helper.<ResponseListener>wrapResponseListener(responseListener, span);
            wrapped = true;
        }
    }
}
 
Example #16
Source File: ElasticsearchClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
private static void onAfterExecute(@Advice.Argument(1) ResponseListener responseListener,
                                   @Advice.Local("span") @Nullable Span span,
                                   @Advice.Local("wrapped") boolean wrapped,
                                   @Advice.Local("helper") @Nullable ElasticsearchRestClientInstrumentationHelper<HttpEntity, Response, ResponseListener> helper,
                                   @Advice.Thrown @Nullable Throwable t) {
    if (span != null) {
        // Deactivate in this thread. Span will be ended and reported by the listener
        span.deactivate();

        if (!wrapped) {
            // Listener is not wrapped- we need to end the span so to avoid leak and report error if occurred during method invocation
            helper.finishClientSpan(null, span, t);
        }
    }
}
 
Example #17
Source File: ElasticSearchClientServiceImpl.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> parseResponse(Response response) {
    final int code = response.getStatusLine().getStatusCode();

    try {
        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);
        }
    } catch (Exception ex) {
        throw new ElasticsearchError(ex);
    }
}
 
Example #18
Source File: IndexService.java    From elastic-rest-spring-wrapper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Checks if the cluster with the provided cluster name contains an index with the provided index name
 *
 * @param clusterName The name of the cluster to connect to
 * @param indexName   The name of the index to check for existence
 * @return True if the index exists in the cluster with the provided name
 */
public Boolean indexExist(String clusterName, String indexName) {
    try {
        Response response = clusterManagementService.getRestClientForCluster(clusterName).performRequest(
                "HEAD",
                indexName
        );

        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == 200) {
            return true;
        } else if (statusCode == 404) {
            return false;
        } else {
            logger.warn("Problem while checking index existence: {}", response.getStatusLine().getReasonPhrase());
            throw new QueryExecutionException("Could not check index existence, status code is " + statusCode);
        }
    } catch (IOException e) {
        logger.warn("Problem while verifying if index exists.", e);
        throw new IndexApiException("Error when checking for existing index.");
    }
}
 
Example #19
Source File: ZentityPluginIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testPluginIsLoaded() throws Exception {
    Response response = client.performRequest(new Request("GET", "_nodes/plugins"));
    JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
    Iterator<Map.Entry<String, JsonNode>> nodes = json.get("nodes").fields();
    while (nodes.hasNext()) {
        Map.Entry<String, JsonNode> entry = nodes.next();
        JsonNode node = entry.getValue();
        boolean pluginFound = false;
        for (JsonNode plugin : node.get("plugins")) {
            String pluginName = plugin.get("name").textValue();
            if (pluginName.equals("zentity")) {
                pluginFound = true;
                break;
            }
        }
        assertTrue(pluginFound);
    }
}
 
Example #20
Source File: MarcElasticsearchClientTest.java    From metadata-qa-marc with GNU General Public License v3.0 6 votes vote down vote up
public void testIndexTweet() throws IOException {
  MarcElasticsearchClient client = new MarcElasticsearchClient();
  Response response = client.indexTweet(2, "kimchy", "trying out Elasticsearch");
  assertEquals("HTTP/1.1 201 Created", response.getStatusLine().toString());
  assertEquals(3, response.getHeaders().length);

  assertEquals("Location", response.getHeaders()[0].getName());
  assertEquals("/twitter/tweet/2", response.getHeaders()[0].getValue());
  assertEquals("content-type", response.getHeaders()[1].getName());
  assertEquals("application/json; charset=UTF-8", response.getHeaders()[1].getValue());
  assertEquals("content-length", response.getHeaders()[2].getName());
  assertTrue(140 < Integer.parseInt(response.getHeaders()[2].getValue()));

  String json = EntityUtils.toString(response.getEntity());
  // {"_index":"twitter","_type":"tweet","_id":"2","_version":161,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}
  assertTrue(json.startsWith("{\"_index\":\"twitter\",\"_type\":\"tweet\",\"_id\":\"2\",\"_version\":"));
  assertTrue(json.endsWith(",\"result\":\"created\",\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"created\":true}"));

  // assertEquals(2, client.getNumberOfTweets());
}
 
Example #21
Source File: ElasticsearchContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void elasticsearchDefaultTest() throws IOException {
    // Create the elasticsearch container.
    try (ElasticsearchContainer container = new ElasticsearchContainer()
        .withEnv("foo", "bar") // dummy env for compiler checking correct generics usage
    ) {
        // Start the container. This step might take some time...
        container.start();

        // Do whatever you want with the rest client ...
        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"));
    }
}
 
Example #22
Source File: DocumentService.java    From elastic-rabbitmq with MIT License 6 votes vote down vote up
public ESQueryResponse query(String index, String type, Map<String, String> params, String body) {
    params = addTenantId2Param(params);

    HttpEntity requestBody = new StringEntity(body, Charsets.UTF_8);
    try {
        Response response = client.performRequest(
                "GET",
                index + "/" + type + "/_search",
                params,
                requestBody);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode > 299) {
            logger.warn("Problem while indexing a document: {}" + response.getStatusLine().getReasonPhrase());
            throw new ElasticAPIException("Could not index a document, status code is " + statusCode);
        }

        ESQueryResponse esQueryResponse = gson.fromJson(IOUtils.toString(response.getEntity().getContent()),
                ESQueryResponse.class);

        return esQueryResponse;
    } catch (IOException e) {
        logger.error("Failed to update document with type [" + type + "] body [" + body + "]: ",e);
    }
    return null;
}
 
Example #23
Source File: ElasticSearchManager.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Type exists.
 *
 * @param indexName the index name
 * @param type the type
 * @return true, if successful
 */
private static boolean typeExists(String indexName, String type) {

    try {
        Response response = invokeAPI("HEAD", indexName + "/_mapping/" + type, null);
        if (response != null) {
            return response.getStatusLine().getStatusCode() == 200 ? true : false;
        }
    } catch (IOException e) {
        LOGGER.error("Error typeExists ", e);
    }

    return false;
}
 
Example #24
Source File: RestElasticClient.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double getVersion() {
    if (version != null) {
        return version;
    }

    final Pattern pattern = Pattern.compile("(\\d+\\.\\d+)\\.\\d+");
    try {
        final Response response = performRequest("GET", "/", null, true);
        try (final InputStream inputStream = response.getEntity().getContent()) {
            Map<String,Object> info = mapper.readValue(inputStream, new TypeReference<Map<String, Object>>() {});
            @SuppressWarnings("unchecked")
            Map<String,Object> ver = (Map<String,Object>) info.getOrDefault("version", Collections.EMPTY_MAP);
            final Matcher m = pattern.matcher((String) ver.get("number"));
            if (!m.find()) {
                version = DEFAULT_VERSION;
            } else {
                version = Double.valueOf(m.group(1));
            }
        }
    } catch (Exception e) {
        LOGGER.warning("Error getting server version: " + e);
        version = DEFAULT_VERSION;
    }

    return version;
}
 
Example #25
Source File: TargetTypesServiceImpl.java    From pacbot with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> getAttributeValues(AttributeValuesRequest attributeValuesRequest) {
	try {
		Response response = invokeAPI("GET", attributeValuesRequest.getIndex(), attributeValuesRequest.getPayload());
		if(response != null) {
			String attributeValues = EntityUtils.toString(response.getEntity());
			return mapper.readValue(attributeValues, new TypeReference<Map<String, Object>>(){});
		}
	} catch (Exception exception) {
		log.error(UNEXPECTED_ERROR_OCCURRED, exception);
	}
	return Maps.newHashMap();
}
 
Example #26
Source File: ElasticSearchClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean deleteTemplate(String indexName) throws IOException {
    indexName = formatIndexName(indexName);

    Response response = client.getLowLevelClient()
                              .performRequest(HttpDelete.METHOD_NAME, "/_template/" + indexName);
    return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
}
 
Example #27
Source File: EsRestClient.java    From jkes with Apache License 2.0 5 votes vote down vote up
public Response performRequest(String method, String endpoint) {
    try {
        return this.restClient.performRequest(method, endpoint);
    } catch (IOException e) {
        throw new RequestException(e);
    }
}
 
Example #28
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public boolean start() throws IOException {
  restClient = source.spec.getConnectionConfiguration().createClient();

  String query = source.spec.getQuery() != null ? source.spec.getQuery().get() : null;
  if (query == null) {
    query = "{\"query\": { \"match_all\": {} }}";
  }
  if ((source.backendVersion >= 5) && source.numSlices != null && source.numSlices > 1) {
    // if there is more than one slice, add the slice to the user query
    String sliceQuery =
        String.format("\"slice\": {\"id\": %s,\"max\": %s}", source.sliceId, source.numSlices);
    query = query.replaceFirst("\\{", "{" + sliceQuery + ",");
  }
  String endPoint =
      String.format(
          "/%s/%s/_search",
          source.spec.getConnectionConfiguration().getIndex(),
          source.spec.getConnectionConfiguration().getType());
  Map<String, String> params = new HashMap<>();
  params.put("scroll", source.spec.getScrollKeepalive());
  if (source.backendVersion == 2) {
    params.put("size", String.valueOf(source.spec.getBatchSize()));
    if (source.shardPreference != null) {
      params.put("preference", "_shards:" + source.shardPreference);
    }
  }
  HttpEntity queryEntity = new NStringEntity(query, ContentType.APPLICATION_JSON);
  Request request = new Request("GET", endPoint);
  request.addParameters(params);
  request.setEntity(queryEntity);
  Response response = restClient.performRequest(request);
  JsonNode searchResult = parseResponse(response.getEntity());
  updateScrollId(searchResult);
  return readNextBatchAndReturnFirstDocument(searchResult);
}
 
Example #29
Source File: ElasticsearchRestClientTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void test1(final RestClient client, final HttpEntity entity) throws IOException {
  final Request request = new Request("PUT", "/twitter/tweet/1");
  request.setEntity(entity);

  final Response indexResponse = client.performRequest(request);
  assertNotNull(indexResponse);
}
 
Example #30
Source File: ESManager.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Bulk upload.
 *
 * @param errors the errors
 * @param bulkRequest the bulk request
 */
private static void bulkUpload(List<String> errors, StringBuilder bulkRequest) {
    try {
        Response resp = invokeAPI("POST", "/_bulk", bulkRequest.toString());
        String responseStr = EntityUtils.toString(resp.getEntity());
        if (responseStr.contains("\"errors\":true")) {
            List<String> errRecords = Util.retrieveErrorRecords(responseStr);
            LOGGER.error("Upload failed for {}",errRecords);
            errors.addAll(errRecords);
        }
    } catch (Exception e) {
        LOGGER.error("Bulk upload failed",e);
        errors.add(e.getMessage());
    }
}