.setText(payload) .setContentType(requestContentType);
.setText(payload) .setContentType(requestContentType);
@Test public void test() throws Exception { try (CloseableHttpClient client = HttpClientBuilder.create().build()) { HttpPut put = new HttpPut( String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); put.setEntity(EntityBuilder.create().setText(VALUE) .setContentType(ContentType.create("text-plain", StandardCharsets.UTF_8)).build()); try (CloseableHttpResponse resp = client.execute(put)) { assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); } HttpGet get = new HttpGet( String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); try (CloseableHttpResponse resp = client.execute(get)) { assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); assertEquals("value", Bytes.toString(ByteStreams.toByteArray(resp.getEntity().getContent()))); } get = new HttpGet(String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), "whatever", FAMILY, QUALIFIER)); try (CloseableHttpResponse resp = client.execute(get)) { assertEquals(HttpStatus.SC_NOT_FOUND, resp.getStatusLine().getStatusCode()); } } } }
@Override public void performLowLevelRequest(String method, String endpoint, String jsonEntity) throws IOException { HttpEntity entity = EntityBuilder.create().setText(jsonEntity).setContentType(ContentType.APPLICATION_JSON).build(); lowLevelClient.performRequest(method, endpoint, Collections.emptyMap(), entity); }
/** * Reindex data from one index/type to another index * @param sourceIndex source index name * @param sourceType source type name * @param targetIndex target index name * @return The number of documents that have been reindexed * @throws IOException In case of error */ public int reindex(String sourceIndex, String sourceType, String targetIndex) throws IOException { logger.debug("reindex [{}]/[{}] -> [{}]/[doc]", sourceIndex, sourceType, targetIndex); String reindexQuery = "{ \"source\": {\n" + " \"index\": \"" + sourceIndex + "\",\n" + " \"type\": \"" + sourceType + "\"\n" + " },\n" + " \"dest\": {\n" + " \"index\": \"" + targetIndex + "\",\n" + " \"type\": \"doc\"\n" + " }\n" + "}\n"; logger.trace("{}", reindexQuery); HttpEntity entity = EntityBuilder.create().setText(reindexQuery).setContentType(ContentType.APPLICATION_JSON).build(); Response restResponse = lowLevelClient.performRequest("POST", "/_reindex", Collections.emptyMap(), entity); Map<String, Object> response = asMap(restResponse); logger.debug("reindex response: {}", response); return (response == null ? -1 : (int) response.get("total")); }
/** * Fully removes a type from an index (removes data) * @param index index name * @param type type * @throws IOException In case of error */ public void deleteByQuery(String index, String type) throws IOException { logger.debug("deleteByQuery [{}]/[{}]", index, type); String deleteByQuery = "{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " }\n" + "}"; HttpEntity entity = EntityBuilder.create().setText(deleteByQuery).setContentType(ContentType.APPLICATION_JSON).build(); Response restResponse = lowLevelClient.performRequest("POST", "/" + index + "/" + type + "/_delete_by_query", Collections.emptyMap(), entity); Map<String, Object> response = asMap(restResponse); logger.debug("reindex response: {}", response); }
public HttpEntity createEntity(String json) { if (isNullOrEmpty(json)) { return null; } return EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build(); } }
public HttpBuilder setText(final String text) { entityBuilder.setText(text); hasOtherBody = true; return this; }
//JSON object. JSONObject jsonObject = new JSONObject(); jsonObject.put("username", register_username); jsonObject.put("password", register_password); //Entity builder to send value. EntityBuilder entityBuilder = EntityBuilder.create(); entityBuilder.setText(jsonObject.toString()); httpPost.setEntity(entityBuilder.build()); //Getting response HttpResponse response = httpClient.execute(httpPost); String responseBody = EntityUtils.toString(response.getEntity()); JSONObject responseObject = new JSONObject(responseBody);
//JSON object. JSONObject jsonObject = new JSONObject(); jsonObject.putOpt("username", register_username); jsonObject.putOpt("password", register_password); //Entity builder to send value. EntityBuilder entityBuilder = EntityBuilder.create(); entityBuilder.setText(jsonObject.toString()); httpPost.setEntity(entityBuilder.build()); //Getting response HttpResponse response = httpClient.execute(httpPost); String responseBody = EntityUtils.toString(response.getEntity()); JSONObject responseObject = new JSONObject(responseBody);
@Override public Response call() throws Exception { return esClientProvider.getClient().performRequest( POST_ACTION, getDeleteByQueryPath(typeDescriptor), Collections.<String, String>emptyMap(), EntityBuilder.create().setText(MAPPER.writeValueAsString(queryMap)).build(), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())); } }, typeDescriptor.getIndex(), "DELETE BY QUERY");
@Override public Response call() throws Exception { return esClientProvider.getClient().performRequest( PUT_ACTION, getIndexPath(indexName), Collections.<String, String>emptyMap(), EntityBuilder.create().setText(MAPPER.writeValueAsString(indexSettings)).build(), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())); }
@Override public Response call() throws Exception { return esClientProvider.getClient().performRequest( PUT_ACTION, getMappingPath(typeDescriptor), Collections.<String, String>emptyMap(), EntityBuilder.create().setText(MAPPER.writeValueAsString(mapping)).build(), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())); } }, typeDescriptor.getIndex(), "PUT MAPPING");
@Override public Response call() throws Exception { return esClientProvider.getClient().performRequest( GET_ACTION, getSearchPath(typeDescriptor), Collections.<String, String>emptyMap(), EntityBuilder.create().setText(MAPPER.writeValueAsString(queryMap)).build(), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())); }
@Override public Response call() throws Exception { return esClientProvider.getClient().performRequest( GET_ACTION, getSearchPath(typeDescriptor), Collections.<String, String>emptyMap(), EntityBuilder.create().setText(MAPPER.writeValueAsString(queryMap)).build(), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())); }
@Override public void performLowLevelRequest(String method, String endpoint, String jsonEntity) throws IOException { HttpEntity entity = EntityBuilder.create().setText(jsonEntity).setContentType(ContentType.APPLICATION_JSON).build(); lowLevelClient.performRequest(method, endpoint, Collections.emptyMap(), entity); }
protected HttpEntity getJsonEntity(Object o) { return EntityBuilder.create() .setText(GsonUtil.get().toJson(o)) .setContentType(ContentType.APPLICATION_JSON) .build(); }
public HttpEntity createEntity(String json) { if (isNullOrEmpty(json)) { return null; } return EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build(); } }
HttpEntity toHttpEntity() { EntityBuilder builder = EntityBuilder.create() .setContentType(contentType) .setText(body); if (compressed) { builder.gzipCompress(); } return builder.build(); } }
@Override public HttpEntity getEntity() { return EntityBuilder.create().setText(getXmlBody().asXML()) .setContentType(ContentType.APPLICATION_XML).build(); } }