use of org.elasticsearch.client.Request in project sonarqube by SonarSource.
the class EsClient method indicesStats.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html
public IndicesStatsResponse indicesStats(String... indices) {
return execute(() -> {
Request request = new Request("GET", "/" + (indices.length > 0 ? (String.join(",", indices) + "/") : "") + "_stats");
request.addParameter("level", "shards");
Response response = restHighLevelClient.getLowLevelClient().performRequest(request);
return IndicesStatsResponse.toIndicesStatsResponse(gson.fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class));
}, () -> computeDetailsAsString(indices));
}
use of org.elasticsearch.client.Request in project beam by apache.
the class ElasticsearchIOTestCommon method testMaxParallelRequestsPerWindow.
void testMaxParallelRequestsPerWindow() throws Exception {
List<Document> data = ElasticsearchIOTestUtils.createDocuments(numDocs, ElasticsearchIOTestUtils.InjectionMode.DO_NOT_INJECT_INVALID_DOCS).stream().map(doc -> Document.create().withInputDoc(doc).withTimestamp(Instant.now())).collect(Collectors.toList());
Write write = ElasticsearchIO.write().withConnectionConfiguration(connectionConfiguration).withMaxParallelRequestsPerWindow(1);
PCollection<KV<Integer, Iterable<Document>>> batches = pipeline.apply(Create.of(data)).apply(StatefulBatching.fromSpec(write.getBulkIO()));
PCollection<Integer> keyValues = batches.apply(MapElements.into(integers()).via((SerializableFunction<KV<Integer, Iterable<Document>>, Integer>) KV::getKey));
// Number of unique keys produced should be number of maxParallelRequestsPerWindow * numWindows
// There is only 1 request (key) per window, and 1 (global) window ie. one key total where
// key value is 0
PAssert.that(keyValues).containsInAnyOrder(0);
PAssert.that(batches).satisfies(new AssertThatHasExpectedContents(0, data));
pipeline.run();
}
use of org.elasticsearch.client.Request in project beam by apache.
the class ElasticsearchIOTestCommon method testDefaultRetryPredicate.
/**
* 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()));
}
use of org.elasticsearch.client.Request in project beam by apache.
the class ElasticsearchIO method getBackendVersion.
static int getBackendVersion(ConnectionConfiguration connectionConfiguration) {
try (RestClient restClient = connectionConfiguration.createClient()) {
Request request = new Request("GET", "");
Response response = restClient.performRequest(request);
JsonNode jsonNode = parseResponse(response.getEntity());
int backendVersion = Integer.parseInt(jsonNode.path("version").path("number").asText().substring(0, 1));
checkArgument(VALID_CLUSTER_VERSIONS.contains(backendVersion), "The Elasticsearch version to connect to is %s.x. " + "This version of the ElasticsearchIO is only compatible with " + "Elasticsearch v7.x, v6.x, v5.x and v2.x", backendVersion);
return backendVersion;
} catch (IOException e) {
throw new IllegalArgumentException("Cannot get Elasticsearch version", e);
}
}
use of org.elasticsearch.client.Request in project beam by apache.
the class ElasticsearchIOTestUtils method deleteIndex.
static void deleteIndex(RestClient restClient, String index) throws IOException {
try {
closeIndex(restClient, index);
Request request = new Request("DELETE", String.format("/%s", index));
restClient.performRequest(request);
} catch (IOException e) {
// so when the first tests is run, the index does not exist yet
if (!e.getMessage().contains("index_not_found_exception")) {
throw e;
}
}
}
Aggregations