Search in sources :

Example 86 with Response

use of org.graylog.shaded.elasticsearch7.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)

Example 87 with Response

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

the class ElasticsearchTest method deleteDocument.

// 删除文档
private static void deleteDocument(RestClient client) throws Exception {
    Request request = new Request("DELETE", INDEX + "/" + TYPE + "/1");
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) Request(org.elasticsearch.client.Request)

Example 88 with Response

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

the class ElasticsearchTest method getDocument.

// 获取文档
private static void getDocument(RestClient client) throws Exception {
    Request request = new Request("GET", INDEX + "/" + TYPE + "/1");
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) Request(org.elasticsearch.client.Request)

Example 89 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project YCSB by brianfrankcooper.

the class ElasticsearchRestClient method init.

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread.
 */
@Override
public void init() throws DBException {
    final Properties props = getProperties();
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    final int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    final int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    final Boolean newIndex = Boolean.parseBoolean(props.getProperty("es.new_index", "false"));
    final String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
    final List<HttpHost> esHttpHosts = new ArrayList<>(nodeList.length);
    for (String h : nodeList) {
        String[] nodes = h.split(":");
        esHttpHosts.add(new HttpHost(nodes[0], Integer.valueOf(nodes[1]), "http"));
    }
    restClient = RestClient.builder(esHttpHosts.toArray(new HttpHost[esHttpHosts.size()])).build();
    final Response existsResponse = performRequest(restClient, "HEAD", "/" + indexKey);
    final boolean exists = existsResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
    if (exists && newIndex) {
        final Response deleteResponse = performRequest(restClient, "DELETE", "/" + indexKey);
        final int statusCode = deleteResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new DBException("delete [" + indexKey + "] failed with status [" + statusCode + "]");
        }
    }
    if (!exists || newIndex) {
        try (XContentBuilder builder = jsonBuilder()) {
            builder.startObject();
            builder.startObject("settings");
            builder.field("index.number_of_shards", numberOfShards);
            builder.field("index.number_of_replicas", numberOfReplicas);
            builder.endObject();
            builder.endObject();
            final Map<String, String> params = emptyMap();
            final StringEntity entity = new StringEntity(builder.string());
            final Response createResponse = performRequest(restClient, "PUT", "/" + indexKey, params, entity);
            final int statusCode = createResponse.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                throw new DBException("create [" + indexKey + "] failed with status [" + statusCode + "]");
            }
        } catch (final IOException e) {
            throw new DBException(e);
        }
    }
    final Map<String, String> params = Collections.singletonMap("wait_for_status", "green");
    final Response healthResponse = performRequest(restClient, "GET", "/_cluster/health/" + indexKey, params);
    final int healthStatusCode = healthResponse.getStatusLine().getStatusCode();
    if (healthStatusCode != HttpStatus.SC_OK) {
        throw new DBException("cluster health [" + indexKey + "] failed with status [" + healthStatusCode + "]");
    }
}
Also used : DBException(site.ycsb.DBException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) StringEntity(org.apache.http.entity.StringEntity) HttpHost(org.apache.http.HttpHost) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 90 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project YCSB by brianfrankcooper.

the class ElasticsearchRestClient method insert.

@Override
public Status insert(final String table, final String key, final Map<String, ByteIterator> values) {
    try {
        final Map<String, String> data = StringByteIterator.getStringMap(values);
        data.put(KEY, key);
        final Response response = restClient.performRequest("POST", "/" + indexKey + "/" + table + "/", Collections.<String, String>emptyMap(), new NStringEntity(new ObjectMapper().writeValueAsString(data), ContentType.APPLICATION_JSON));
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
            return Status.ERROR;
        }
        if (!isRefreshNeeded) {
            synchronized (this) {
                isRefreshNeeded = true;
            }
        }
        return Status.OK;
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException) DBException(site.ycsb.DBException)

Aggregations

Response (org.elasticsearch.client.Response)111 Request (org.elasticsearch.client.Request)38 IOException (java.io.IOException)33 HttpEntity (org.apache.http.HttpEntity)24 NStringEntity (org.apache.http.nio.entity.NStringEntity)21 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Map (java.util.Map)14 BasicHeader (org.apache.http.message.BasicHeader)14 ResponseException (org.elasticsearch.client.ResponseException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 RestClient (org.elasticsearch.client.RestClient)11 ArrayList (java.util.ArrayList)10 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)10 StringEntity (org.apache.http.entity.StringEntity)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 InputStream (java.io.InputStream)8 JSONObject (org.json.simple.JSONObject)8 MultiSearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7