java 操作ES


package cn.crxy.elasticsearch_11;


import static org.junit.Assert.*;


import java.util.HashMap;


import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.junit.Before;
import org.junit.Test;


import com.fasterxml.jackson.databind.ObjectMapper;


import cn.crxy.elasticsearch_08.Person;


public class EsTest {

TransportClient transportClient;
@Before
public void test0() throws Exception {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch")//指定集群名称
.put("client.transport.sniff", true)//开启自动嗅探功能,可以把集群内部的所有节点都添加到客户端连接中
.build();
transportClient = new TransportClient(settings);
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
transportClient.addTransportAddresses(transportAddress);
}


/**
* 创建es客户端连接对象
* @throws Exception
*/
@Test
public void test1() throws Exception {
TransportClient transportClient = new TransportClient();
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
transportClient.addTransportAddress(transportAddress);

ImmutableList<DiscoveryNode> connectedNodes = transportClient.connectedNodes();
for (DiscoveryNode discoveryNode : connectedNodes) {
System.out.println(discoveryNode.getHostAddress());
}
}
String index = "crxy";
String type = "emp";

/**
* 如果集群的名称被修改了,需要显式指定集群的名称
* @throws Exception
*/
@Test
public void test2() throws Exception {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
TransportClient transportClient = new TransportClient(settings);
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
transportClient.addTransportAddress(transportAddress);

//表示查询指定索引库中指定类类型下的指定id的数据
GetResponse getResponse = transportClient.prepareGet(index, type, "1").get();
}

/**
* 工作中建议使用这种
* @throws Exception
*/
@Test
public void test3() throws Exception {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch")//指定集群名称
.put("client.transport.sniff", true)//开启自动嗅探功能,可以把集群内部的所有节点都添加到客户端连接中
.build();
TransportClient transportClient = new TransportClient(settings);
//指定集群内的节点
TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.170", 9300);
//TransportAddress transportAddress1 = new InetSocketTransportAddress("192.168.1.171", 9300);
transportClient.addTransportAddresses(transportAddress);

ImmutableList<DiscoveryNode> connectedNodes = transportClient.connectedNodes();
for (DiscoveryNode discoveryNode : connectedNodes) {
System.out.println(discoveryNode.getHostAddress());
}
}

/**
* index -1 json字符串
* @throws Exception
*/
@Test
public void test4() throws Exception {
String jsonStr = "{\"name\":\"hehe\",\"age\":18}";
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "2")//指定索引库,类型,id
.setSource(jsonStr)//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}

/**
* index -2  hash类型
* @throws Exception
*/
@Test
public void test5() throws Exception {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("name", "zs");
hashMap.put("age", 20);

IndexResponse indexResponse = transportClient.prepareIndex(index, type, "3")//指定索引库,类型,id
.setSource(hashMap)//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}


/**
* 工作中最常用
* index -3  bean对象
* @throws Exception
*/
@Test
public void test6() throws Exception {
Person person = new Person();
person.setName("haha");
person.setAge(12);

ObjectMapper objectMapper = new ObjectMapper();
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4")//指定索引库,类型,id
.setSource(objectMapper.writeValueAsString(person))//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}

/**
* index -4  es工具类
* @throws Exception
*/
@Test
public void test7() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()//{
.field("name", "ww")//"name":"ww"
.field("age", 30)//"age":30
.endObject();//}

ObjectMapper objectMapper = new ObjectMapper();
IndexResponse indexResponse = transportClient.prepareIndex(index, type, "4")//指定索引库,类型,id
.setSource(builder)//指定数据的详细信息
.get();//指定具体操作
System.out.println(indexResponse.getVersion());
}


/**
* 查询
* @throws Exception
*/
@Test
public void test8() throws Exception {
GetResponse getResponse = transportClient.prepareGet(index, type, "1").get();
System.out.println(getResponse.getSourceAsString());
}


/**
* 更新
* @throws Exception
*/
@Test
public void test9() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("age", 11).endObject();
UpdateResponse updateResponse = transportClient.prepareUpdate(index, type, "100").setDoc(builder).get();
System.out.println(updateResponse.getVersion());
}

/**
* 删除
* @throws Exception
*/
@Test
public void test10() throws Exception {
DeleteResponse deleteResponse = transportClient.prepareDelete(index, type, "1").get();
}

/**
* 求count值
* @throws Exception
*/
@Test
public void test11() throws Exception {
CountResponse countResponse = transportClient.prepareCount(index).get();
System.out.println(countResponse.getCount());

}


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值