一、在進行java操作elasticsearch之前,請確認好集群的名稱及對應的ES節點ip和端口
1、查看ES的集群名稱
#進入elasticsearch.yml配置文件
/opt/elasticsearch-6.4.3/config vim elasticsearch.yml
2、查詢ip
二、根據文檔id查詢數據
/** * */ package com.cyb.test; import java.net.InetAddress; import java.net.UnknownHostException; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; /** * @Title: EsDemo.java * @Package:com.cyb.test * @Description: * @author:陳遠波 * @date:2019年1月20日 * @version:V1.0 */ public class EsDemo { //從es中查詢數據 @Test public void test1() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //實現數據查詢(指定_id查詢) 參數分別是 索引名,類型名 id GetResponse response = client.prepareGet("lib3","user","1").execute().actionGet(); //得到查詢出的數據 System.out.println(response.getSourceAsString());//打印出json數據 client.close();//關閉客戶端 } }
在kibana中查詢id為1的結果為:
java控制台輸出的結果為:
三、插入數據
1、在插入數據之前,需要使用kibana在es中建立索引和定義好字段等信息
PUT /index1 { "settings": { "number_of_shards": 5, "number_of_replicas": 0 }, "mappings": { "blog":{ "properties":{ "id":{ "type":"long" }, "title":{ "type":"text", "analyzer":"ik_max_word" }, "content":{ "type":"text", "analyzer":"ik_max_word" }, "postdate":{ "type":"date" }, "url":{ "type":"text" } } } } }
2、java實現添加
//插入數據 @Test public void test2() throws IOException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //將數據轉換成文檔的格式(后期可以使用java對象,將數據轉換成json對象就可以了) XContentBuilder doContentBuilder=XContentFactory.jsonBuilder() .startObject() .field("id", "1") //字段名 : 值 .field("title", "java設計模式之裝飾模式") .field("content", "在不必改變原類文件和使用繼承的情況下,動態地擴展一個對象的功能") .field("postdate", "2018-05-20") .field("url", "https://www.cnblogs.com/chenyuanbo/") .endObject(); //添加文檔 index1:索引名 blog:類型 10:id
//.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) 代表插入成功后立即刷新,因為ES中插入數據默認分片要1秒鍾后再刷新
IndexResponse response = client.prepareIndex("index1","blog","10") .setSource(doContentBuilder).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); System.out.println(response.status()); //打印出CREATED 表示添加成功 }
三、java實現刪除
//刪除文檔 @Test public void test3() throws UnknownHostException { Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); DeleteResponse response = client.prepareDelete("index1","blog","10").get(); System.out.println(response.status()); //控制台打印出OK代表刪除成功 }
四、java實現修改數據
1、對指定字段進行修改
//修改數據(指定字段進行修改) @Test public void test4() throws IOException, InterruptedException, ExecutionException { Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); UpdateRequest request = new UpdateRequest(); request.index("index1") //索引名 .type("blog") //類型 .id("10")//id .doc( XContentFactory.jsonBuilder() .startObject() .field("title", "單例設計模式")//要修改的字段 及字段值 .endObject() ); UpdateResponse response= client.update(request).get(); System.out.println(response.status()); //控制台出現OK 代表更新成功 }
2、使用upsert修改
upsert修改用法:修改文章存在,執行修改,不存在則執行插入
//upsert 修改用法:修改文章存在,執行修改,不存在則執行插入 @Test public void test5() throws IOException, InterruptedException, ExecutionException { Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); IndexRequest request1 = new IndexRequest("index1","blog","8").source( XContentFactory.jsonBuilder() .startObject() .field("id", "2") //字段名 : 值 .field("title", "工廠模式") .field("content", "靜態工廠,實例工廠") .field("postdate", "2018-05-20") .field("url", "https://www.cnblogs.com/chenyuanbo/") .endObject() ); UpdateRequest request2 = new UpdateRequest("index1","blog","8").doc( XContentFactory.jsonBuilder().startObject() .field("title", "設計模式") .endObject() ).upsert(request1); UpdateResponse response = client.update(request2).get(); System.out.println(response.status()); }
以上是java對elasticsearch的基本操作,下一篇博客本人將書寫bulk的批量操作。對ES感興趣的朋友可以加個關注,另轉發請說明出處,本人的博客地址為:https://www.cnblogs.com/chenyuanbo/
技術在於交流!