Search in sources :

Example 76 with Response

use of org.elasticsearch.client.Response in project hazelcast by hazelcast.

the class ElasticCatClientTest method shouldRetryOnShards.

@Test
public void shouldRetryOnShards() throws IOException {
    ElasticCatClient catClient = new ElasticCatClient(restClient, 5);
    Response nodesResponse = response("es2node_nodes.json");
    Response shardsResponse = response("es2node_shards.json");
    when(restClient.performRequest(any())).thenThrow(new IOException("Could not connect")).thenReturn(nodesResponse, shardsResponse);
    List<Shard> shards = catClient.shards("my-index");
    assertThat(shards).extracting(Shard::getHttpAddress).containsOnly("127.0.0.1:9200", "127.0.0.1:9201");
}
Also used : Response(org.elasticsearch.client.Response) IOException(java.io.IOException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 77 with Response

use of org.elasticsearch.client.Response in project hazelcast by hazelcast.

the class ElasticCatClientTest method shards.

@Test
public void shards() throws IOException {
    ElasticCatClient catClient = new ElasticCatClient(restClient, 5);
    Response nodesResponse = response("es2node_nodes.json");
    Response shardsResponse = response("es2node_shards.json");
    when(restClient.performRequest(any())).thenReturn(nodesResponse, shardsResponse);
    List<Shard> shards = catClient.shards("my-index");
    assertThat(shards).extracting(Shard::getHttpAddress).containsOnly("127.0.0.1:9200", "127.0.0.1:9201");
}
Also used : Response(org.elasticsearch.client.Response) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 78 with Response

use of org.elasticsearch.client.Response in project hazelcast by hazelcast.

the class ElasticCatClient method shards.

/**
 * Returns list of shards for given indexes
 *
 * Only STARTED shards are returned.
 *
 * @param indices indexes to return shards for (wildcard format accepted)
 */
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
public List<Shard> shards(String... indices) {
    Map<String, String> idToAddress = nodes().stream().collect(toMap(Node::getId, Node::getHttpAddress));
    try {
        Request r = new Request("GET", "/_cat/shards/" + String.join(",", indices));
        r.addParameter("format", "json");
        r.addParameter("h", "id,index,shard,prirep,docs,state,ip,node");
        Response res = withRetry(() -> client.performRequest(r), retries);
        try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
            JsonArray array = Json.parse(reader).asArray();
            List<Shard> shards = new ArrayList<>(array.size());
            for (JsonValue value : array) {
                Optional<Shard> shard = convertToShard(value, idToAddress);
                shard.ifPresent(shards::add);
            }
            LOG.log(FINE, "Shards " + shards);
            return shards;
        }
    } catch (IOException e) {
        throw new JetException("Could not get ES shards", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Request(org.elasticsearch.client.Request) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException) Response(org.elasticsearch.client.Response) JsonArray(com.hazelcast.internal.json.JsonArray) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 79 with Response

use of org.elasticsearch.client.Response in project hazelcast by hazelcast.

the class ElasticCatClient method master.

/**
 * Returns current master of the ES cluster
 */
public Master master() {
    try {
        Request r = new Request("GET", "/_cat/master");
        r.addParameter("format", "json");
        Response res = withRetry(() -> client.performRequest(r), retries);
        try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
            JsonArray array = Json.parse(reader).asArray();
            JsonObject object = array.get(0).asObject();
            return new Master(object.get("host").asString(), object.get("id").asString(), object.get("ip").asString(), object.get("node").asString());
        }
    } catch (IOException e) {
        throw new JetException("Could not get ES cluster master", e);
    }
}
Also used : Response(org.elasticsearch.client.Response) JsonArray(com.hazelcast.internal.json.JsonArray) InputStreamReader(java.io.InputStreamReader) Request(org.elasticsearch.client.Request) JsonObject(com.hazelcast.internal.json.JsonObject) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException)

Example 80 with Response

use of org.elasticsearch.client.Response in project yyl_example by Relucent.

the class ElasticsearchTest method catApi.

// 查看API信息
private static void catApi(RestClient client) throws Exception {
    Request request = new Request("GET", "_cat");
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) Request(org.elasticsearch.client.Request)

Aggregations

Response (org.elasticsearch.client.Response)75 IOException (java.io.IOException)24 Request (org.elasticsearch.client.Request)19 BasicHeader (org.apache.http.message.BasicHeader)14 NStringEntity (org.apache.http.nio.entity.NStringEntity)14 HttpEntity (org.apache.http.HttpEntity)13 ResponseException (org.elasticsearch.client.ResponseException)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 HashMap (java.util.HashMap)9 RestClient (org.elasticsearch.client.RestClient)8 Map (java.util.Map)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7 ArrayList (java.util.ArrayList)6 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)6 JSONArray (org.json.simple.JSONArray)6 JSONObject (org.json.simple.JSONObject)6 JSONParser (org.json.simple.parser.JSONParser)6 ParseException (org.json.simple.parser.ParseException)6 InputStream (java.io.InputStream)5 StringEntity (org.apache.http.entity.StringEntity)5