ElasticSearch 使用Java Api访问集群
1、创建maven工程导入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、创建索引
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;
@Before
public 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串创建索引
*/
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对象创建索引
*/
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创建索引
*/
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();
}
@After
public 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();
}
}
/**
* 批量插入数据
*
* @throws IOException
*/
@Test
public 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()
)
);
//执行插入操作
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;
@Before
public 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
public void get1() throws UnknownHostException {
response = client.prepareGet("twitter", "doc", "1").get();
}
@Test
public 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);
}
}
}
@After
public 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;
@Before
public void init() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "myes").build();
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));
}
@After
public 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();
}
}
/**
* 根据文档进行删除
* @throws UnknownHostException
*/
@Test
public void delete1() throws UnknownHostException {
response = client.prepareDelete("twitter", "doc", "100").get();
}
@Test
/**
* 根据根据查询结果删除数据,并触发相关事件
*/
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("------------错误了");
}
});
}
}