ElasticSearch 使用Java Api访问集群
生活随笔
收集整理的這篇文章主要介紹了
ElasticSearch 使用Java Api访问集群
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ElasticSearch 使用Java Api訪問(wèn)集群
1、創(chuàng)建maven工程導(dǎo)入pom依賴
<dependencies><dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId><version>6.1.1</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.9.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>2、創(chuàng)建索引
package cn.cheng.es.api;import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; import java.util.HashMap; import java.util.Map;public class CreateIndex {private TransportClient client;private IndexResponse response;@Beforepublic void init() throws UnknownHostException {Settings settings = Settings.builder().put("cluster.name", "myes").build();client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));}@Test/*** 使用json串創(chuàng)建索引*/public void index1() throws UnknownHostException {String json = "{" +"\"user\":\"kimchy\"," +"\"postDate\":\"2013-01-30\"," +"\"message\":\"trying out Elasticsearch\"" +"}";response = client.prepareIndex("twitter", "doc", "100").setSource(json, XContentType.JSON).get();}@Test/*** 使用map對(duì)象創(chuàng)建索引*/public void index2() throws UnknownHostException {Map<String, Object> json = new HashMap<String, Object>();json.put("user", "kimchy");json.put("postDate", new Date());json.put("message", "trying out Elasticsearch");response = client.prepareIndex("twitter", "doc", "200").setSource(json).get();}@Test/*** 使用jsonBuilder創(chuàng)建索引*/public void index3() throws IOException {XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject();response = client.prepareIndex("twitter", "doc", "300").setSource(builder).get();}@Afterpublic void close() throws UnknownHostException {if (response != null) {String index = response.getIndex();String type = response.getType();String id = response.getId();long version = response.getVersion();RestStatus status = response.status();System.out.println("index " + index + " type" + type + "" + id + "" + version + "" + version);System.out.println(status);client.close();}}/*** 批量插入數(shù)據(jù)** @throws IOException*/@Testpublic void index4() throws IOException {BulkRequestBuilder bulkRequest = client.prepareBulk();bulkRequest.add(client.prepareIndex("twitter", "doc", "400").setSource(XContentFactory.jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject()));bulkRequest.add(client.prepareIndex("twitter", "doc", "500").setSource(XContentFactory.jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date()).field("message", "another post").endObject()));//執(zhí)行插入操作BulkResponse bulkResponse = bulkRequest.get();if (bulkResponse.hasFailures()) {System.out.println("-------------------我失敗了-----------------");}} }3、查詢索引
package cn.cheng.es.api;import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetItemResponse; import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; import java.util.HashMap; import java.util.Map;public class QueryIndex {private TransportClient client;private GetResponse response;@Beforepublic void init() throws UnknownHostException {Settings settings = Settings.builder().put("cluster.name", "myes").build();client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));}@Testpublic void get1() throws UnknownHostException {response = client.prepareGet("twitter", "doc", "1").get();}@Testpublic void get2() throws UnknownHostException {MultiGetResponse multiGetItemResponses = client.prepareMultiGet().add("twitter", "doc", "100").add("twitter", "doc", "200", "300", "400").get();for (MultiGetItemResponse itemResponse : multiGetItemResponses) {GetResponse response = itemResponse.getResponse();if (response.isExists()) {String json = response.getSourceAsString();System.out.println(json);}}}@Afterpublic void close() throws UnknownHostException {if (response != null) {String index = response.getIndex();String type = response.getType();String id = response.getId();long version = response.getVersion();System.out.println("index " + index + " type" + type + "" + id + "" + version + "" + version);client.close();}} }4、刪除索引
package cn.cheng.es.api;import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.BulkByScrollTask; import org.elasticsearch.index.reindex.DeleteByQueryAction; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.net.InetAddress; import java.net.UnknownHostException;public class DeleteIndex {private TransportClient client;private DeleteResponse response;@Beforepublic void init() throws UnknownHostException {Settings settings = Settings.builder().put("cluster.name", "myes").build();client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));}@Afterpublic void close() throws UnknownHostException {if(response!=null) {String index = response.getIndex();String type = response.getType();String id = response.getId();long version = response.getVersion();System.out.println("index " + index + " type" + type + "" + id + "" + version + "" + version);RestStatus status = response.status();System.out.println("status:" + status.getStatus());client.close();}}/*** 根據(jù)文檔進(jìn)行刪除* @throws UnknownHostException*/@Testpublic void delete1() throws UnknownHostException {response = client.prepareDelete("twitter", "doc", "100").get();}@Test/*** 根據(jù)根據(jù)查詢結(jié)果刪除數(shù)據(jù),并觸發(fā)相關(guān)事件*/public void delete2() throws UnknownHostException {DeleteByQueryAction.INSTANCE.newRequestBuilder(client).filter(QueryBuilders.matchQuery("gender", "male")).source("twitter").execute(new ActionListener<BulkByScrollResponse>() {public void onResponse(BulkByScrollResponse response) {long deleted = response.getDeleted();System.out.println("---------------"+deleted);}public void onFailure(Exception e) {System.out.println("------------錯(cuò)誤了");}});} }總結(jié)
以上是生活随笔為你收集整理的ElasticSearch 使用Java Api访问集群的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: zookeeper应用之分布式锁
- 下一篇: 数据仓库建立