org.apache.http.nio.entity.NStringEntity Java Examples

The following examples show how to use org.apache.http.nio.entity.NStringEntity. 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: TestPasswordAuthentication.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void test()
        throws IOException
{
    String json = new ObjectMapper().writeValueAsString(ImmutableMap.<String, Object>builder()
            .put("value", 42L)
            .build());

    client.getLowLevelClient()
            .performRequest(
                    "POST",
                    "/test/_doc?refresh",
                    ImmutableMap.of(),
                    new NStringEntity(json, ContentType.APPLICATION_JSON),
                    new BasicHeader("Authorization", format("Basic %s", Base64.encodeAsString(format("%s:%s", USER, PASSWORD).getBytes(StandardCharsets.UTF_8)))));

    assertThat(assertions.query("SELECT * FROM test"))
            .matches("VALUES BIGINT '42'");
}
 
Example #2
Source File: ElasticsearchCollectorTest.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000L)
public void testAll() throws Exception {
    HttpHost host = new HttpHost(HOST, HTTP_PORT, "http");
    RestClient client = RestClient.builder(new HttpHost[]{ host }).build();
    HttpEntity entity = new NStringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON);
    Request request = new Request("POST", "/test/_doc/");
    request.setEntity(entity);
    client.performRequest(request);

    MockDispatcher dispatcher = new MockDispatcher();
    ElasticsearchCollector collector = new ElasticsearchCollector();
    collector.setDispatcher(dispatcher);
    Hashtable<String, Object> configuration = new Hashtable<>();
    configuration.put("addresses", "http://localhost:" + HTTP_PORT);
    configuration.put("index", "test");
    collector.activate(configuration);

    collector.run();

    Assert.assertEquals(1, dispatcher.postedEvents.size());
    Assert.assertEquals("elasticsearch", dispatcher.postedEvents.get(0).getProperty("type"));
    Assert.assertEquals("decanter/collect/elasticsearch", dispatcher.postedEvents.get(0).getTopic());
}
 
Example #3
Source File: WeikePath.java    From jframe with Apache License 2.0 6 votes vote down vote up
private void bulkIndexMember(List<?> memList) throws Exception {
    StringBuilder buf = new StringBuilder(1024);
    for (Object mem : memList) {
        buf.append("{\"index\": {}}");
        buf.append("\n");
        buf.append(Gson.toJson(mem));
        buf.append("\n");
    }

    long startTime = System.currentTimeMillis();
    RestClient client = Plugin.client;

    HttpEntity entity = new NStringEntity(buf.toString(), ContentType.APPLICATION_JSON);

    Response indexResponse = client.performRequest("POST", "/weike/member/_bulk",
            Collections.<String, String>emptyMap(), entity);

    if (LOG.isDebugEnabled()) {
        LOG.debug("indexMember {}ms", System.currentTimeMillis() - startTime);
        LOG.debug("indexResponse {}", indexResponse.toString());
    }
}
 
Example #4
Source File: WeikePath.java    From jframe with Apache License 2.0 6 votes vote down vote up
private void indexMember(String sellerId, Object mem) throws IOException {
    if (sellerId == null)
        sellerId = "";

    long startTime = System.currentTimeMillis();

    RestClient client = Plugin.client;
    String json = Gson.toJson(mem);

    HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);
    String path = "/weike/member";
    if (!"".equals(sellerId)) {
        path += "?routing=" + sellerId;
    }
    Response indexResponse = client.performRequest("POST", path, Collections.<String, String>emptyMap(), entity);

    if (LOG.isDebugEnabled()) {
        LOG.debug("indexMember {}ms", System.currentTimeMillis() - startTime);
        LOG.debug("indexResponse {}", indexResponse.toString());
    }
}
 
Example #5
Source File: TestQuery.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testSearch() throws Exception {
    // JsonObject json = new JsonObject();
    // json.addProperty("from", "0");
    // json.addProperty("size", "10");
    // json.addProperty("explain", true);
    // JsonObject query = new JsonObject();
    // query.add
    // json.add("query", query);
    String json = "{\"explain\":false,\"from\":0,\"size\":1,\"query\":{\"range\":{\"tradeAmount\":{\"gte\":10,\"lte\":2000}}}}";

    long startTime = System.currentTimeMillis();
    HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);
    Response response = client.performRequest("GET", "/weike/member/_search", Collections.singletonMap("pretty", "true"), entity);
    LOG.info("search-{} {}ms", EntityUtils.toString(response.getEntity()), System.currentTimeMillis() - startTime);
}
 
Example #6
Source File: ElasticsearchBulkDocumentWriterIntegrationTest.java    From metron with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setup() throws Exception {
    client = ElasticsearchClientFactory.create(globals());
    retrieveDao = new ElasticsearchRetrieveLatestDao(client);
    writer = new ElasticsearchBulkDocumentWriter<>(client)
            .withRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

    // add bro template
    JSONObject broTemplate = JSONUtils.INSTANCE.load(new File(broTemplatePath), JSONObject.class);
    String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true);
    HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON);
    Response response = client
            .getLowLevelClient()
            .performRequest("PUT", "/_template/bro_template", Collections.emptyMap(), broEntity);
    assertThat(response.getStatusLine().getStatusCode(), CoreMatchers.equalTo(200));
}
 
Example #7
Source File: MetricStoreImpl.java    From griffin with Apache License 2.0 6 votes vote down vote up
private HttpEntity getHttpEntityForSearch(String metricName, int from, int
    size, long tmst)
    throws JsonProcessingException {
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> queryParam = new HashMap<>();
    Map<String, Object> termQuery = Collections.singletonMap("name.keyword",
        metricName);
    queryParam.put("filter", Collections.singletonMap("term", termQuery));
    Map<String, Object> sortParam = Collections
        .singletonMap("tmst", Collections.singletonMap("order",
            "desc"));
    map.put("query", Collections.singletonMap("bool", queryParam));
    map.put("sort", sortParam);
    map.put("from", from);
    map.put("size", size);
    return new NStringEntity(JsonUtil.toJson(map),
        ContentType.APPLICATION_JSON);
}
 
Example #8
Source File: ElasticSearchClientServiceImpl.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Response runQuery(String endpoint, String query, String index, String type) {
    StringBuilder sb = new StringBuilder()
        .append("/")
        .append(index);
    if (type != null && !type.equals("")) {
        sb.append("/")
        .append(type);
    }

    sb.append(String.format("/%s", endpoint));

    HttpEntity queryEntity = new NStringEntity(query, ContentType.APPLICATION_JSON);

    try {
        return client.performRequest("POST", sb.toString(), Collections.emptyMap(), queryEntity);
    } catch (Exception e) {
        throw new ElasticsearchError(e);
    }
}
 
Example #9
Source File: ElasticSearchClient.java    From skywalking with Apache License 2.0 6 votes vote down vote up
public boolean createTemplate(String indexName, Map<String, Object> settings,
                              Map<String, Object> mapping) throws IOException {
    indexName = formatIndexName(indexName);

    String[] patterns = new String[] {indexName + "-*"};

    Map<String, Object> aliases = new HashMap<>();
    aliases.put(indexName, new JsonObject());

    Map<String, Object> template = new HashMap<>();
    template.put("index_patterns", patterns);
    template.put("aliases", aliases);
    template.put("settings", settings);
    template.put("mappings", mapping);

    HttpEntity entity = new NStringEntity(new Gson().toJson(template), ContentType.APPLICATION_JSON);

    Response response = client.getLowLevelClient()
                              .performRequest(
                                  HttpPut.METHOD_NAME, "/_template/" + indexName, Collections.emptyMap(), entity);
    return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
}
 
Example #10
Source File: ElasticSearchClientServiceImpl.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public DeleteOperationResponse deleteById(String index, String type, List<String> ids) {
    try {
        StringBuilder sb = new StringBuilder();
        for (int idx = 0; idx < ids.size(); idx++) {
            String header = buildBulkHeader("delete", index, type, ids.get(idx));
            sb.append(header).append("\n");
        }
        HttpEntity entity = new NStringEntity(sb.toString(), ContentType.APPLICATION_JSON);
        StopWatch watch = new StopWatch();
        watch.start();
        Response response = client.performRequest("POST", "/_bulk", Collections.emptyMap(), entity);
        watch.stop();

        if (getLogger().isDebugEnabled()) {
            getLogger().debug(String.format("Response for bulk delete: %s",
                    IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)));
        }

        DeleteOperationResponse dor = new DeleteOperationResponse(watch.getDuration(TimeUnit.MILLISECONDS));

        return dor;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #11
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public boolean advance() throws IOException {
  if (batchIterator.hasNext()) {
    current = batchIterator.next();
    return true;
  } else {
    String requestBody =
        String.format(
            "{\"scroll\" : \"%s\",\"scroll_id\" : \"%s\"}",
            source.spec.getScrollKeepalive(), scrollId);
    HttpEntity scrollEntity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
    Request request = new Request("GET", "/_search/scroll");
    request.addParameters(Collections.emptyMap());
    request.setEntity(scrollEntity);
    Response response = restClient.performRequest(request);
    JsonNode searchResult = parseResponse(response.getEntity());
    updateScrollId(searchResult);
    return readNextBatchAndReturnFirstDocument(searchResult);
  }
}
 
Example #12
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
  // remove the scroll
  String requestBody = String.format("{\"scroll_id\" : [\"%s\"]}", scrollId);
  HttpEntity entity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
  try {
    Request request = new Request("DELETE", "/_search/scroll");
    request.addParameters(Collections.emptyMap());
    request.setEntity(entity);
    restClient.performRequest(request);
  } finally {
    if (restClient != null) {
      restClient.close();
    }
  }
}
 
Example #13
Source File: LowLevelRestController.java    From ProjectStudy with MIT License 6 votes vote down vote up
/**
   * 使用脚本更新Name
   *
   * @param id
* @param bookDto
   * @return org.springframework.http.ResponseEntity<java.lang.String>
   * @throws IOException
   * @author wliduo[[email protected]]
   * @date 2019/8/9 11:37
   */
  @PutMapping("/book/{id}")
  public ResponseEntity<String> update2(@PathVariable("id") String id, @RequestBody BookDto bookDto) throws IOException {
      // 构造HTTP请求
      Request request = new Request("POST", new StringBuilder("/book/book/")
              .append(id).append("/_update").toString());
      // 设置其他一些参数比如美化Json
      request.addParameter("pretty", "true");
      JSONObject jsonObject = new JSONObject();
      // 创建脚本语言,如果是字符变量,必须加单引号
      StringBuilder op1 = new StringBuilder("ctx._source.name=").append("'" + bookDto.getName() + "'");
      jsonObject.put("script", op1);
      request.setEntity(new NStringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON));
      // 执行HTTP请求
      Response response = restClient.performRequest(request);
      // 获取返回的内容
      String responseBody = EntityUtils.toString(response.getEntity());
      return new ResponseEntity<>(responseBody, HttpStatus.OK);
  }
 
Example #14
Source File: LowLevelRestController.java    From ProjectStudy with MIT License 6 votes vote down vote up
/**
 * 根据Id更新Book,ES的POST和PUT可以看下面这个文章
 *
 * https://blog.csdn.net/z457181562/article/details/93470152
 * @param bookDto
 * @return org.springframework.http.ResponseEntity<java.lang.String>
 * @throws IOException
 * @author wliduo[[email protected]]
 * @date 2019/8/9 10:04
 */
@PutMapping("/book")
public ResponseBean update(@RequestBody BookDto bookDto) throws IOException {
    // 构造HTTP请求
    /*Request request = new Request("POST", new StringBuilder("/book/book/")
            .append(bookDto.getId()).append("/_update").toString());*/
    Request request = new Request("PUT", new StringBuilder("/book/book/")
            .append(bookDto.getId()).toString());
    // 设置其他一些参数比如美化Json
    request.addParameter("pretty", "true");
    /*// 将数据丢进去,这里一定要外包一层'doc',否则内部不能识别
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("doc", new JSONObject(bookDto));*/
    // 设置请求体并指定ContentType,如果不指定会乱码
    request.setEntity(new NStringEntity(JSONObject.toJSONString(bookDto), ContentType.APPLICATION_JSON));
    // 执行HTTP请求
    Response response = restClient.performRequest(request);
    // 获取返回的内容
    String responseBody = EntityUtils.toString(response.getEntity());
    return new ResponseBean(HttpStatus.OK.value(), "更新成功", JSON.parseObject(responseBody));
}
 
Example #15
Source File: LowLevelRestController.java    From ProjectStudy with MIT License 6 votes vote down vote up
/**
 * 添加ES对象, Book的ID就是ES中存储的Document的ID,ES的POST和PUT可以看下面这个文章
 * https://blog.csdn.net/z457181562/article/details/93470152
 *
 * @param bookDto
 * @return org.springframework.http.ResponseEntity<java.lang.String>
 * @throws IOException
 * @author wliduo[[email protected]]
 * @date 2019/8/8 17:46
 */
@PostMapping("/book")
public ResponseBean add(@RequestBody BookDto bookDto) throws IOException {
    // Endpoint直接指定为Index/Type的形式
    /*Request request = new Request("POST", new StringBuilder("/book/book/").toString());*/
    // 防重复新增数据
    bookDto.setId(System.currentTimeMillis());
    Request request = new Request("PUT", new StringBuilder("/book/book/")
            .append(bookDto.getId()).append("/_create").toString());
    // 设置其他一些参数比如美化Json
    request.addParameter("pretty", "true");
    // 设置请求体并指定ContentType,如果不指定会乱码
    request.setEntity(new NStringEntity(JSONObject.toJSONString(bookDto), ContentType.APPLICATION_JSON));
    // 发送HTTP请求
    Response response = restClient.performRequest(request);
    // 获取响应体
    String responseBody = EntityUtils.toString(response.getEntity());
    return new ResponseBean(HttpStatus.OK.value(), "添加成功", JSON.parseObject(responseBody));
}
 
Example #16
Source File: LowLevelRestController.java    From ProjectStudy with MIT License 6 votes vote down vote up
/**
   * 使用脚本更新Name
   *
   * @param id
* @param bookDto
   * @return org.springframework.http.ResponseEntity<java.lang.String>
   * @throws IOException
   * @author wliduo[[email protected]]
   * @date 2019/8/9 11:37
   */
  @PutMapping("/book/{id}")
  public ResponseEntity<String> update2(@PathVariable("id") String id, @RequestBody BookDto bookDto) throws IOException {
      // 构造HTTP请求
      Request request = new Request("POST", new StringBuilder("/book/book/")
              .append(id).append("/_update").toString());
      // 设置其他一些参数比如美化Json
      request.addParameter("pretty", "true");
      JSONObject jsonObject = new JSONObject();
      // 创建脚本语言,如果是字符变量,必须加单引号
      StringBuilder op1 = new StringBuilder("ctx._source.name=").append("'" + bookDto.getName() + "'");
      jsonObject.put("script", op1);
      request.setEntity(new NStringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON));
      // 执行HTTP请求
      Response response = restClient.performRequest(request);
      // 获取返回的内容
      String responseBody = EntityUtils.toString(response.getEntity());
      return new ResponseEntity<>(responseBody, HttpStatus.OK);
  }
 
Example #17
Source File: LowLevelRestController.java    From ProjectStudy with MIT License 6 votes vote down vote up
/**
 * 根据Id更新Book,ES的POST和PUT可以看下面这个文章
 *
 * https://blog.csdn.net/z457181562/article/details/93470152
 * @param bookDto
 * @return org.springframework.http.ResponseEntity<java.lang.String>
 * @throws IOException
 * @author wliduo[[email protected]]
 * @date 2019/8/9 10:04
 */
@PutMapping("/book")
public ResponseBean update(@RequestBody BookDto bookDto) throws IOException {
    // 构造HTTP请求
    /*Request request = new Request("POST", new StringBuilder("/book/book/")
            .append(bookDto.getId()).append("/_update").toString());*/
    Request request = new Request("PUT", new StringBuilder("/book/book/")
            .append(bookDto.getId()).toString());
    // 设置其他一些参数比如美化Json
    request.addParameter("pretty", "true");
    /*// 将数据丢进去,这里一定要外包一层'doc',否则内部不能识别
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("doc", new JSONObject(bookDto));*/
    // 设置请求体并指定ContentType,如果不指定会乱码
    request.setEntity(new NStringEntity(JSONObject.toJSONString(bookDto), ContentType.APPLICATION_JSON));
    // 执行HTTP请求
    Response response = restClient.performRequest(request);
    // 获取返回的内容
    String responseBody = EntityUtils.toString(response.getEntity());
    return new ResponseBean(HttpStatus.OK.value(), "更新成功", JSON.parseObject(responseBody));
}
 
Example #18
Source File: LowLevelRestController.java    From ProjectStudy with MIT License 6 votes vote down vote up
/**
 * 添加ES对象, Book的ID就是ES中存储的Document的ID,ES的POST和PUT可以看下面这个文章
 * https://blog.csdn.net/z457181562/article/details/93470152
 *
 * @param bookDto
 * @return org.springframework.http.ResponseEntity<java.lang.String>
 * @throws IOException
 * @author wliduo[[email protected]]
 * @date 2019/8/8 17:46
 */
@PostMapping("/book")
public ResponseBean add(@RequestBody BookDto bookDto) throws IOException {
    // Endpoint直接指定为Index/Type的形式
    /*Request request = new Request("POST", new StringBuilder("/book/book/").toString());*/
    // 防重复新增数据
    bookDto.setId(System.currentTimeMillis());
    Request request = new Request("PUT", new StringBuilder("/book/book/")
            .append(bookDto.getId()).append("/_create").toString());
    // 设置其他一些参数比如美化Json
    request.addParameter("pretty", "true");
    // 设置请求体并指定ContentType,如果不指定会乱码
    request.setEntity(new NStringEntity(JSONObject.toJSONString(bookDto), ContentType.APPLICATION_JSON));
    // 发送HTTP请求
    Response response = restClient.performRequest(request);
    // 获取响应体
    String responseBody = EntityUtils.toString(response.getEntity());
    return new ResponseBean(HttpStatus.OK.value(), "添加成功", JSON.parseObject(responseBody));
}
 
Example #19
Source File: EventElasticSearchIOTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void tearDownClass() throws Exception {
    URIBuilder builder = new URIBuilder().setScheme("http")
            .setHost("127.0.0.1").setPort(9200)
            .setPath("/events/graphite_event/_query");

    HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() {
        @Override
        public String getMethod() {
            return "DELETE";
        }
    };
    delete.setURI(builder.build());

    String deletePayload = "{\"query\":{\"match_all\":{}}}";
    HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON);
    delete.setEntity(entity);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(delete);
    if(response.getStatusLine().getStatusCode() != 200)
    {
        System.out.println("Couldn't delete index after running tests.");
    }
    else {
        System.out.println("Successfully deleted index after running tests.");
    }
}
 
Example #20
Source File: ElasticSearchClientServiceImpl.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public IndexOperationResponse bulk(List<IndexOperationRequest> operations) {
    try {
        StringBuilder payload = new StringBuilder();
        for (int index = 0; index < operations.size(); index++) {
            IndexOperationRequest or = operations.get(index);
            buildRequest(or, payload);
        }

        if (getLogger().isDebugEnabled()) {
            getLogger().debug(payload.toString());
        }
        HttpEntity entity = new NStringEntity(payload.toString(), ContentType.APPLICATION_JSON);
        StopWatch watch = new StopWatch();
        watch.start();
        Response response = client.performRequest("POST", "/_bulk", Collections.emptyMap(), entity);
        watch.stop();

        String rawResponse = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);

        if (getLogger().isDebugEnabled()) {
            getLogger().debug(String.format("Response was: %s", rawResponse));
        }

        IndexOperationResponse retVal = IndexOperationResponse.fromJsonResponse(rawResponse);

        return retVal;
    } catch (Exception ex) {
        throw new ElasticsearchError(ex);
    }
}
 
Example #21
Source File: HttpEventsQueryHandlerIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void tearDownClass() throws Exception {
    URIBuilder builder = new URIBuilder().setScheme("http")
            .setHost("127.0.0.1").setPort(9200)
            .setPath("/events/graphite_event/_query");

    HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() {
        @Override
        public String getMethod() {
            return "DELETE";
        }
    };
    delete.setURI(builder.build());

    String deletePayload = "{\"query\":{\"match_all\":{}}}";
    HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON);
    delete.setEntity(entity);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(delete);
    if(response.getStatusLine().getStatusCode() != 200)
    {
        System.out.println("Couldn't delete index after running tests.");
    }
    else {
        System.out.println("Successfully deleted index after running tests.");
    }
}
 
Example #22
Source File: HttpMetricNamesHandlerIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void tearDownClass() throws Exception {
    URIBuilder builder = new URIBuilder().setScheme("http")
            .setHost("127.0.0.1").setPort(9200)
            .setPath("/metric_metadata/metrics/_query");

    HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() {
        @Override
        public String getMethod() {
            return "DELETE";
        }
    };
    delete.setURI(builder.build());

    String deletePayload = "{\"query\":{\"match_all\":{}}}";
    HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON);
    delete.setEntity(entity);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(delete);
    if(response.getStatusLine().getStatusCode() != 200)
    {
        System.out.println("Couldn't delete index after running tests.");
    }
    else {
        System.out.println("Successfully deleted index after running tests.");
    }
}
 
Example #23
Source File: ElasticIOIntegrationTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private void deleteAllDocuments(String typeToEmpty) throws URISyntaxException, IOException {
    URIBuilder builder = new URIBuilder().setScheme("http")
            .setHost("127.0.0.1").setPort(9200)
            .setPath(typeToEmpty);

    HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() {
        @Override
        public String getMethod() {
            return "DELETE";
        }
    };
    delete.setURI(builder.build());

    String deletePayload = "{\"query\":{\"match_all\":{}}}";
    HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON);
    delete.setEntity(entity);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(delete);
    if(response.getStatusLine().getStatusCode() != 200)
    {
        System.out.println(String.format("Couldn't delete index [%s] after running tests.", typeToEmpty));
    }
    else {
        System.out.println(String.format("Successfully deleted [%s] index after running tests.", typeToEmpty));
    }
}
 
Example #24
Source File: ElasticsearchIOTestCommon.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Test that the default predicate correctly parses chosen error code. */
void testDefaultRetryPredicate(RestClient restClient) throws IOException {

  HttpEntity entity1 = new NStringEntity(BAD_REQUEST, ContentType.APPLICATION_JSON);
  Request request = new Request("POST", "/_bulk");
  request.addParameters(Collections.emptyMap());
  request.setEntity(entity1);
  Response response1 = restClient.performRequest(request);
  assertTrue(CUSTOM_RETRY_PREDICATE.test(response1.getEntity()));

  HttpEntity entity2 = new NStringEntity(OK_REQUEST, ContentType.APPLICATION_JSON);
  request.setEntity(entity2);
  Response response2 = restClient.performRequest(request);
  assertFalse(DEFAULT_RETRY_PREDICATE.test(response2.getEntity()));
}
 
Example #25
Source File: ElasticSearchFilter.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public EsFilterResult query(String dataSetId, String fieldName, String elasticSearchQuery, String token,
                            int preferredPageSize) throws IOException {
  String endpoint = dataSetId + (fieldName != null && !fieldName.isEmpty() ? "/" + fieldName : "") + "/_search";
  JsonNode queryNode = elaborateQuery(elasticSearchQuery, token, preferredPageSize);
  Map<String, String> params = Collections.singletonMap("pretty", "true");

  HttpEntity entity = new NStringEntity(queryNode.toString(), ContentType.APPLICATION_JSON);
  Response response = restClient.performRequest(METHOD_GET, endpoint, params, entity);

  JsonNode responseNode = mapper.readTree(response.getEntity().getContent());
  return new EsFilterResult(queryNode, responseNode);
}
 
Example #26
Source File: LocalWebServerHttpRequestHandler.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
private static HttpEntity createTextEntity(String text) throws UnsupportedEncodingException
{
	NStringEntity entity = new NStringEntity(MessageFormat.format("<html><body><h1>{0}</h1></body></html>", text), //$NON-NLS-1$
			IOUtil.UTF_8);
	entity.setContentType(HTML_TEXT_TYPE + HTTP.CHARSET_PARAM + IOUtil.UTF_8);
	return entity;
}
 
Example #27
Source File: ElasticsearchUpdateIntegrationTest.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * Install the index template to ensure that "guid" is of type "keyword". The
 * {@link org.apache.metron.elasticsearch.dao.ElasticsearchRetrieveLatestDao} cannot find
 * documents if the type is not "keyword".
 *
 * See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-term-query.html
 */
private static void installIndexTemplate() throws IOException {
  HttpEntity broEntity = new NStringEntity(indexTemplate, ContentType.APPLICATION_JSON);
  ElasticsearchClient client = ElasticsearchClientFactory.create(globalConfig);
  Response response = client
          .getLowLevelClient()
          .performRequest("PUT", "/_template/test_template", Collections.emptyMap(), broEntity);
  assertThat(response.getStatusLine().getStatusCode(), CoreMatchers.equalTo(200));
}
 
Example #28
Source File: ElasticSearchClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public int delete(String indexName, String timeBucketColumnName, long endTimeBucket) throws IOException {
    indexName = formatIndexName(indexName);
    Map<String, String> params = Collections.singletonMap("conflicts", "proceed");
    String jsonString = "{" + "  \"query\": {" + "    \"range\": {" + "      \"" + timeBucketColumnName + "\": {" + "        \"lte\": " + endTimeBucket + "      }" + "    }" + "  }" + "}";
    HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
    Response response = client.getLowLevelClient()
                              .performRequest(
                                  HttpPost.METHOD_NAME, "/" + indexName + "/_delete_by_query", params, entity);
    log.debug("delete indexName: {}, jsonString : {}", indexName, jsonString);
    return response.getStatusLine().getStatusCode();
}
 
Example #29
Source File: ElasticsearchHTTP.java    From hangout with MIT License 5 votes vote down vote up
protected void emit(final Map event) {
    String _index = (String) this.indexRender.render(event);
    String _indexType = (String) indexTypeRender.render(event);
    String requestBody;
    Response response = null;

    addActionList(event, _index, _indexType);
    if (this.actionList.size() / 2 >= this.bulkActions) {
        log.info(String.format("bulk %d docs" , this.actionList.size() / 2));
        try {

            requestBody = actionList.stream().map(JSONValue::toJSONString).collect(Collectors.joining("\n")) + "\n";

            response = restClient.performRequest(
                    "POST",
                    BULKPATH,
                    Collections.<String, String>emptyMap(),
                    new NStringEntity(
                            requestBody,
                            ContentType.APPLICATION_JSON
                    )
            );

        log.info("bulk returned");

        } catch (IOException e) {
            log.error("Bulk index es Error:", e);
            if (response != null)
                log.error("Response Code is " + response.getStatusLine().toString());
        } finally {
            actionList.clear();
        }
    }
}
 
Example #30
Source File: ElasticsearchPlugin.java    From hawkular-alerts with Apache License 2.0 5 votes vote down vote up
protected void writeAlert(Action a) throws Exception {
    String url = a.getProperties().get(PROP_URL);
    String index = a.getProperties().get(PROP_INDEX);
    String type = a.getProperties().get(PROP_TYPE);
    String[] urls = url.split(",");
    HttpHost[] hosts = new HttpHost[urls.length];
    for (int i=0; i<urls.length; i++) {
        hosts[i] = HttpHost.create(urls[0]);
    }
    RestClient client = RestClient.builder(hosts)
            .setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.useSystemProperties();
                CredentialsProvider credentialsProvider = checkBasicCredentials(a);
                if (credentialsProvider != null) {
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
                return httpClientBuilder;
            }).build();

    HttpEntity document = new NStringEntity(transform(a), ContentType.APPLICATION_JSON);
    String endpoint = "/" + index + "/" + type;
    Header[] headers = checkHeaders(a);
    Response response = headers == null ? client.performRequest("POST", endpoint, Collections.EMPTY_MAP, document) :
            client.performRequest("POST", endpoint, Collections.EMPTY_MAP, document, headers);
    log.debugf(response.toString());
    client.close();
}