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()));
}
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()));
}
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()));
}
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 + "]");
}
}
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;
}
}
Aggregations