摘要:開發環境這里講的是實戰對于如何安裝,配置環境變量,配置不做講解詳細請查看學習筆記四在上安裝和啟動增量同步數據到方式實現從零到一超級詳里面有做詳細講解,對于搭建集群目前還沒有寫相關文章,讀者可以自行在項目的文件中添加的依賴實現員工信息
開發環境:
elasticsearch:7.0.0
kibana:7.0.0
JDK: 1.8.0_201
maven: 3.6.1
詳細請查看:
elasticsearch學習筆記(四)——在windows上安裝和啟動Elasticsearch
https://segmentfault.com/a/11...
增量同步mysql 數據到elasticsearch canal adapter方式(binlog)實現(從零到一超級詳
https://segmentfault.com/a/11...
里面有做詳細講解,對于搭建ES集群目前還沒有寫相關文章,讀者可以自行google
2、實現員工信息的增刪改查 (1)構建clientorg.elasticsearch elasticsearch 7.0.0 org.elasticsearch.client elasticsearch-rest-high-level-client 7.0.0
我的ES機器IP是192.168.254.131,讀者可自行更換
RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200, "http") ) ); client.close();
對于多個ip的情況,直接使用如下格式即可:
RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("IP", PORT, "http"), new HttpHost("IP", PORT, "http") ... ) ); client.close();(2)創建員工信息
我個人比較喜歡這種格式的實現:
還有基于json字符串和map的,讀者可查看:
https://www.elastic.co/guide/...
格式如下
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field(name, value); builder.field(name, value); builder.field(name, value); builder.field(name, value); builder.field(name, value); ... } builder.endObject(); IndexRequest request = new IndexRequest(index).id(id).source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
示例:
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("name", "jack"); builder.field("age", 27); builder.field("position", "technique"); builder.field("country", "china"); builder.field("join_date", "2017-01-01"); builder.field("salary", 10000); } builder.endObject(); IndexRequest request = new IndexRequest("employee").id("1").source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);(3)根據ID獲取員工信息
詳細請查看:
https://www.elastic.co/guide/...
格式:
GetRequest request = new GetRequest(index, id); GetResponse response = client.get(request, RequestOptions.DEFAULT);
示例:
GetRequest request = new GetRequest("employee", "1"); GetResponse response = client.get(request, RequestOptions.DEFAULT);(4)修改員工信息
詳細請查看:
https://www.elastic.co/guide/...
格式:
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field(name, value); } builder.endObject(); UpdateRequest request = new UpdateRequest(index, id).doc(builder); UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
示例:
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("position", "technique"); } builder.endObject(); UpdateRequest request = new UpdateRequest("employee", "1").doc(builder); UpdateResponse response = client.update(request, RequestOptions.DEFAULT);(5)刪除員工信息
詳細請查看:
https://www.elastic.co/guide/...
格式:
DeleteRequest request = new DeleteRequest(index, id); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
示例:
DeleteRequest request = new DeleteRequest("employee", "1"); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);(6)測試員工信息的增刪改查
完整測試代碼:
/** * 創建員工信息 * @param client * @throws Exception */ private static void createEmployee(RestHighLevelClient client) throws Exception { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("name", "jack"); builder.field("age", 27); builder.field("position", "technique"); builder.field("country", "china"); builder.field("join_date", "2017-01-01"); builder.field("salary", 10000); } builder.endObject(); IndexRequest request = new IndexRequest("employee").id("1").source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse.getResult()); } /** * 獲取員工信息 * @param client */ private static void getEmployee(RestHighLevelClient client) throws IOException { GetRequest request = new GetRequest("employee", "1"); GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString()); } /** * 修改員工信息 * @param client */ private static void updateEmployee(RestHighLevelClient client) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("position", "technique"); } builder.endObject(); UpdateRequest request = new UpdateRequest("employee", "1").doc(builder); UpdateResponse response = client.update(request, RequestOptions.DEFAULT); System.out.println(response.getResult()); } /** * 刪除員工信息 * @param client */ private static void deleteEmployee(RestHighLevelClient client) throws IOException { DeleteRequest request = new DeleteRequest("employee", "1"); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println(response.getResult()); }
1、測試新增
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200, "http") ) ); createEmployee(client); client.close(); }
結果:
CREATED
2、測試獲取數據
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200, "http") ) ); getEmployee(client); client.close(); }
結果:
{"name":"jack","age":27,"position":"technique manager","country":"china","join_date":"2017-01-01","salary":10000}
3、測試更新數據
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200, "http") ) ); getEmployee(client); updateEmployee(client); getEmployee(client) client.close(); }
結果:
{"name":"jack","age":27,"position":"technique manager","country":"china","join_date":"2017-01-01","salary":10000} UPDATE {"name":"jack","age":27,"position":"technique","country":"china","join_date":"2017-01-01","salary":10000}
4、測試刪除數據
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200, "http") ) ); getEmployee(client); deleteEmployee(client); getEmployee(client); client.close(); }
結果:
{"name":"jack","age":27,"position":"technique","country":"china","join_date":"2017-01-01","salary":10000} DELETED null3、對員工信息進行復雜的搜索操作
需求:
(1)搜索職位中包含technique的員工
(2)同時要求age在30到40歲之間
(3)分頁查詢,查找第一頁
準備數據:
private static void prepareData(RestHighLevelClient client) throws IOException { client.index(new IndexRequest("employee").id("1").source(XContentFactory.jsonBuilder() .startObject() .field("name", "jack") .field("age", 27) .field("position", "technique software") .field("country", "china") .field("join_date", "2017-01-01") .field("salary", 10000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("2").source(XContentFactory.jsonBuilder() .startObject() .field("name", "marry") .field("age", 35) .field("position", "technique manager") .field("country", "china") .field("join_date", "2017-01-01") .field("salary", 12000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("3").source(XContentFactory.jsonBuilder() .startObject() .field("name", "tom") .field("age", 32) .field("position", "senior technique software") .field("country", "china") .field("join_date", "2016-01-01") .field("salary", 11000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("4").source(XContentFactory.jsonBuilder() .startObject() .field("name", "jen") .field("age", 25) .field("position", "junior finance") .field("country", "usa") .field("join_date", "2016-01-01") .field("salary", 7000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("5").source(XContentFactory.jsonBuilder() .startObject() .field("name", "mike") .field("age", 37) .field("position", "finance manager") .field("country", "usa") .field("join_date", "2015-01-01") .field("salary", 15000) .endObject() ), RequestOptions.DEFAULT); }
執行:
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200) ) ); prepareData(client); client.close(); }
然后構建查詢語句:
GET /employee/_search { "query": { "bool": { "must": [ { "match": { "position": "technique" } } ], "filter": { "range": { "age": { "gte": 30, "lte": 40 } } } } }, "from": 0, "size": 1 } { "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.5598161, "hits" : [ { "_index" : "employee", "_type" : "_doc", "_id" : "2", "_score" : 0.5598161, "_source" : { "name" : "marry", "age" : 35, "position" : "technique manager", "country" : "china", "join_date" : "2017-01-01", "salary" : 12000 } } ] } }
代碼:
private static void executeSearch(RestHighLevelClient client) throws IOException { SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchQuery("position", "technique")); builder.postFilter(QueryBuilders.rangeQuery("age").from(30).to(40)); builder.from(0); builder.size(1); SearchRequest request = new SearchRequest(); request.indices("employee"); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); } }
執行:
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200) ) ); executeSearch(client); client.close(); }
結果:
{"name":"marry","age":35,"position":"technique manager","country":"china","join_date":"2017-01-01","salary":12000}4、對員工信息進行聚合分析
需求:
(1)首先按照country國家來進行分組
(2)然后在每個country分組內,再按照入職年限進行分組
(3)最后計算每個分組內的平均薪資
GET /employee/_search { "size": 0, "aggs": { "group_by_country": { "terms": { "field": "country.keyword" }, "aggs": { "group_by_join_date": { "date_histogram": { "field": "join_date", "interval": "year" }, "aggs": { "avg_salary": { "avg": { "field": "salary" } } } } } } } }
{ "took" : 46, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 5, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "group_by_country" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "china", "doc_count" : 3, "group_by_join_date" : { "buckets" : [ { "key_as_string" : "2016-01-01T00:00:00.000Z", "key" : 1451606400000, "doc_count" : 1, "avg_salary" : { "value" : 11000.0 } }, { "key_as_string" : "2017-01-01T00:00:00.000Z", "key" : 1483228800000, "doc_count" : 2, "avg_salary" : { "value" : 11000.0 } } ] } }, { "key" : "usa", "doc_count" : 2, "group_by_join_date" : { "buckets" : [ { "key_as_string" : "2015-01-01T00:00:00.000Z", "key" : 1420070400000, "doc_count" : 1, "avg_salary" : { "value" : 15000.0 } }, { "key_as_string" : "2016-01-01T00:00:00.000Z", "key" : 1451606400000, "doc_count" : 1, "avg_salary" : { "value" : 7000.0 } } ] } } ] } } }
執行:
public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.254.131", 9200) ) ); SearchSourceBuilder builder = new SearchSourceBuilder(); builder.aggregation(AggregationBuilders.terms("group_by_country").field("country.keyword") .subAggregation(AggregationBuilders.dateHistogram("group_by_join_date").field("join_date") .dateHistogramInterval(DateHistogramInterval.YEAR) .subAggregation(AggregationBuilders.avg("avg_salary").field("salary")))); SearchRequest request = new SearchRequest(); request.indices("employee"); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); MapaggregationMap = response.getAggregations().asMap(); ParsedStringTerms group_by_country = (ParsedStringTerms) aggregationMap.get("group_by_country"); for (Terms.Bucket groupByCountryBucket : group_by_country.getBuckets()) { System.out.println(groupByCountryBucket.getKey() + ":" + groupByCountryBucket.getDocCount()); Histogram groupByJoinDate = (Histogram) groupByCountryBucket.getAggregations().asMap().get("group_by_join_date"); for (Histogram.Bucket groupByJoinDateBucket : groupByJoinDate.getBuckets()) { System.out.println(groupByJoinDateBucket.getKey() + ":" + groupByJoinDateBucket.getDocCount()); Avg avg = (Avg) groupByJoinDateBucket.getAggregations().asMap().get("avg_salary"); System.out.println(avg.getValue()); } } client.close(); }
結果:
china:3 2016-01-01T00:00Z:1 11000.0 2017-01-01T00:00Z:2 11000.0 usa:2 2015-01-01T00:00Z:1 15000.0 2016-01-01T00:00Z:1 7000.0附上完整代碼:
EmployeeCRUDApp.java
import org.apache.http.HttpHost; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; public class EmployeeCRUDApp { public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1", 9200, "http") ) ); createEmployee(client); getEmployee(client); updateEmployee(client); getEmployee(client); deleteEmployee(client); getEmployee(client); client.close(); } /** * 創建員工信息 * @param client * @throws Exception */ private static void createEmployee(RestHighLevelClient client) throws Exception { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("name", "jack"); builder.field("age", 27); builder.field("position", "technique manager"); builder.field("country", "china"); builder.field("join_date", "2017-01-01"); builder.field("salary", 10000); } builder.endObject(); IndexRequest request = new IndexRequest("employee").id("1").source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse.getResult()); } /** * 獲取員工信息 * @param client */ private static void getEmployee(RestHighLevelClient client) throws IOException { GetRequest request = new GetRequest("employee", "1"); GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString()); } /** * 修改員工信息 * @param client */ private static void updateEmployee(RestHighLevelClient client) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("position", "technique"); } builder.endObject(); UpdateRequest request = new UpdateRequest("employee", "1").doc(builder); UpdateResponse response = client.update(request, RequestOptions.DEFAULT); System.out.println(response.getResult()); } /** * 刪除員工信息 * @param client */ private static void deleteEmployee(RestHighLevelClient client) throws IOException { DeleteRequest request = new DeleteRequest("employee", "1"); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println(response.getResult()); } }
EmployeeSearchApp.java
import org.apache.http.HttpHost; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; /** * 員工搜索應用程序 */ public class EmployeeSearchApp { public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1", 9200) ) ); prepareData(client); executeSearch(client); client.close(); } /** * 執行搜索操作 * @param client */ private static void executeSearch(RestHighLevelClient client) throws IOException { SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchQuery("position", "technique")); builder.postFilter(QueryBuilders.rangeQuery("age").from(30).to(40)); builder.from(0); builder.size(1); SearchRequest request = new SearchRequest(); request.indices("employee"); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); } } /** * 準備數據 * @param client */ private static void prepareData(RestHighLevelClient client) throws IOException { client.index(new IndexRequest("employee").id("1").source(XContentFactory.jsonBuilder() .startObject() .field("name", "jack") .field("age", 27) .field("position", "technique software") .field("country", "china") .field("join_date", "2017-01-01") .field("salary", 10000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("2").source(XContentFactory.jsonBuilder() .startObject() .field("name", "marry") .field("age", 35) .field("position", "technique manager") .field("country", "china") .field("join_date", "2017-01-01") .field("salary", 12000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("3").source(XContentFactory.jsonBuilder() .startObject() .field("name", "tom") .field("age", 32) .field("position", "senior technique software") .field("country", "china") .field("join_date", "2016-01-01") .field("salary", 11000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("4").source(XContentFactory.jsonBuilder() .startObject() .field("name", "jen") .field("age", 25) .field("position", "junior finance") .field("country", "usa") .field("join_date", "2016-01-01") .field("salary", 7000) .endObject() ), RequestOptions.DEFAULT); client.index(new IndexRequest("employee").id("5").source(XContentFactory.jsonBuilder() .startObject() .field("name", "mike") .field("age", 37) .field("position", "finance manager") .field("country", "usa") .field("join_date", "2015-01-01") .field("salary", 15000) .endObject() ), RequestOptions.DEFAULT); } }
EmployeeAggrApp.java
public class EmployeeAggrApp { public static void main(String[] args) throws Exception { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1", 9200) ) ); SearchSourceBuilder builder = new SearchSourceBuilder(); builder.aggregation(AggregationBuilders.terms("group_by_country").field("country.keyword") .subAggregation(AggregationBuilders.dateHistogram("group_by_join_date").field("join_date") .dateHistogramInterval(DateHistogramInterval.YEAR) .subAggregation(AggregationBuilders.avg("avg_salary").field("salary")))); SearchRequest request = new SearchRequest(); request.indices("employee"); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); MapaggregationMap = response.getAggregations().asMap(); ParsedStringTerms group_by_country = (ParsedStringTerms) aggregationMap.get("group_by_country"); for (Terms.Bucket groupByCountryBucket : group_by_country.getBuckets()) { System.out.println(groupByCountryBucket.getKey() + ":" + groupByCountryBucket.getDocCount()); Histogram groupByJoinDate = (Histogram) groupByCountryBucket.getAggregations().asMap().get("group_by_join_date"); for (Histogram.Bucket groupByJoinDateBucket : groupByJoinDate.getBuckets()) { System.out.println(groupByJoinDateBucket.getKey() + ":" + groupByJoinDateBucket.getDocCount()); Avg avg = (Avg) groupByJoinDateBucket.getAggregations().asMap().get("avg_salary"); System.out.println(avg.getValue()); } } client.close(); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/74474.html
摘要:入門本節描述從獲取工件到在應用程序中使用它如何開始使用高級別客戶端。保證能夠與運行在相同主版本和大于或等于的次要版本上的任何節點通信。與具有相同的發布周期,將版本替換為想要的客戶端版本。 Java High Level REST Client 入門 本節描述從獲取工件到在應用程序中使用它如何開始使用高級別REST客戶端。 兼容性 Java High Level REST Client需...
摘要:用于的官方高級別客戶端,基于低級別客戶端,它公開特定的方法,并負責請求編組和響應反編組。入門初始化執行請求讀取響應日志記錄通用配置嗅探器在中被添加。依賴于核心項目,它接受與相同的請求參數,并返回相同的響應對象。 Elasticsearch Java REST Client Java REST Client有兩種類型: Java Low Level REST Client:用于Elast...
摘要:搜索請求用于與搜索文檔聚合相關的任何操作,還提供了在結果文檔上請求高亮的方法。將字段高光色添加到高亮構建器。稍后可以從中檢索高亮的文本片段。 Search API 搜索請求 SearchRequest用于與搜索文檔、聚合、suggestions相關的任何操作,還提供了在結果文檔上請求高亮的方法。 在最基本的表單中,我們可以向請求添加查詢: SearchRequest searchReq...
閱讀 1309·2021-11-15 11:37
閱讀 2564·2021-09-22 10:56
閱讀 3391·2021-09-06 15:11
閱讀 801·2021-08-31 09:45
閱讀 2897·2021-07-28 11:16
閱讀 1806·2019-08-30 15:44
閱讀 477·2019-08-30 13:22
閱讀 3344·2019-08-30 13:18