1. 添加Maven依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
</dependency>
2. 建立客户端连接:
通过TransportClient接口,我们可以不启动节点就可以和ES集群进行通信,它需要指定ES集群中其中一台或者多台机器IP地址和端口(默认9300)
/**
* @Author rk
* @Date 2018/12/10 12:15
* @Description:
* 学习elasticsearch的基本java api
* 在5.x之前都是直接是TransportClient.builder
* 在5.x之后都是直接是new PreBuiltTransportClient(Settings.EMPTY)
* Transport(jdbc中的connection)
*
**/
public class ElasticSearchTest {
private static final String HOST="hadoop01,hadoop02,hadoop03";
private static final int PORT=9300;
private TransportClient client;
@Before
public void setUp(){
//connection
Settings setting = Settings.builder()
.put("cluster.name","rk-ES")
.build();
client = new PreBuiltTransportClient(setting);
//连接到集群
TransportAddress[] transportAddrs = new TransportAddress[3];
String[] hosts = HOST.split(",");
for(int i = 0; i < hosts.length; i++){
transportAddrs[i] = new TransportAddress(new InetSocketAddress(hosts[i],PORT));
}
client.addTransportAddresses(transportAddrs);
//测试查看查看settings里面的配置,默认cluster.name = elasticsearch,
// 所以在创建settings时,需要添加 .put("cluster.name","rk-ES")
/*Settings sets = client.settings();
Set<String> keys = sets.keySet();
for (String key : keys){
String value = sets.get(key);
System.out.println("key: " + key + "\t --->value: " + value);
}*/
}
@Test
public void test(){
}
@After
public void cleanUp(){
client.close();
}
}
如果没有设置cluster.name,且cluster.name不是elasticsearch,则会报错:NoNodeAvailableException[None of the configured nodes are available:
1) 如果需要使用其他名称的集群(默认是elasticsearch),需要在创建Settings时,添加:Settings.builder().put("cluster.name","rk-ES").build();
2) 通过TransportClient这个接口,自动嗅探整个集群的状态,ES会自动把集群中其它机器的IP地址添加到客户端中。(有待测试)
Settings settings = Settings.builder()
.put("cluster.name","rk-ES")
.put("client.transport.snif", true)
.build();
TransportClient client = new PreBuiltTransportClient(setting)
.addTransportAddress(
new TransportAddress(
new InetSocketAddress("hadoop01", 9300)
)
);
3. 增加索引:
增加索引的数据格式有4种,json、map、bean、xContentBuilder
// 设置要查询的索引库
String INDEX = "product";
String TYPE = "bigdata";
-
Json:
@Test
public void testAddJson(){
String source = "{\"name\": \"hbase\",\"author\": \"Apache\",\"version\": \"1.1.5\"}";
//添加一个索引
IndexResponse response = client.prepareIndex(INDEX, TYPE, "1")
.setSource(source,XContentType.JSON)
.get();
System.out.println("version: " + response.getVersion());
}
-
Map:
@Test
public void testAddMap(){
Map<String, Object> source = new HashMap<>();
source.put("name","hbase");
source.put("author","Apache");
source.put("version", "1.1.5");
//添加一个索引
IndexResponse response = client.prepareIndex(INDEX, TYPE, "2")
.setSource(source)
.get();
System.out.println("version: " + response.getVersion());
}
-
Object:
@Test
public void testAddParams(){
//添加一个索引
IndexResponse response = client.prepareIndex(INDEX, TYPE, "3")
.setSource("name", "flume", "author", "cloudera", "version", "1.8.0")
.get();
System.out.println("version: " + response.getVersion());
}
- Bean:
@Test
public void testAddEntity() throws JsonProcessingException {
BigdataProduct bp = new BigdataProduct("kafka", "linkedIn", "0.10.1.0");
ObjectMapper om = new ObjectMapper();
byte[] bytes = om.writeValueAsBytes(bp);
//添加一个索引
IndexResponse response = client.prepareIndex(INDEX, TYPE, "4")
.setSource(bytes, XContentType.JSON)
.get();
System.out.println("version: " + response.getVersion());
}
- XContentBuilder:
@Test
public void testAddXContent() throws IOException {
//Create a new {@link XContentBuilder} using the given {@link XContent} content.
XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
.startObject()
.field("name", "sqoop")
.field("author","apache")
.field("version", "1.4.7")
.endObject();
//添加一个索引
IndexResponse response = client.prepareIndex(INDEX, TYPE, "5")
.setSource(xContentBuilder)
.get();
System.out.println("version: " + response.getVersion());
}
4. 查询:
@Test
public void testGet(){
GetResponse response = client.prepareGet(INDEX, TYPE, "1").get();
Map<String, Object> sources = response.getSource();
System.out.println("version: " + response.getVersion());
for (Map.Entry<String, Object> source : sources.entrySet()){
System.out.println(source.getKey() + " = " + source.getValue());
}
}
5. 更新:
@Test
public void testUpdate(){
UpdateResponse response = client.prepareUpdate(INDEX, TYPE, "1")
.setDoc("{\"version\": \"2.1.0\"}",XContentType.JSON)
.get();
testGet();
}
6.删除:
@Test
public void testDelete(){
DeleteResponse response = client.prepareDelete(INDEX, TYPE, "5")
.get();
System.out.println("version: " + response.getVersion());
}
7.批量操作bulk:
@Test
public void testBulk(){
BulkResponse response = client.prepareBulk()
.add(client.prepareIndex(INDEX, TYPE, "5").setSource("{\"name\":\"redis\", \"author\": \"redis\", \"version\": \"5.0.0\"}", XContentType.JSON))
.add(client.prepareIndex(INDEX, TYPE, "6").setSource("{\"name\":\"scala\", \"author\": \"jvm\", \"version\": \"2.12.4\"}", XContentType.JSON))
.add(client.prepareUpdate(INDEX, TYPE, "2").setDoc("{\"url\": \"http://flume.apache.org\"}", XContentType.JSON))
.get();
BulkItemResponse[] items = response.getItems();
for (BulkItemResponse bir : items){
System.out.println("id: " + bir.getId() + " ---> version: " + bir.getVersion());
}
}