摘要:开发环境这里讲的是实战对于如何安装,配置环境变量,配置不做讲解详细请查看学习笔记四在上安装和启动增量同步数据到方式实现从零到一超级详里面有做详细讲解,对于搭建集群目前还没有写相关文章,读者可以自行在项目的文件中添加的依赖实现员工信息
开发环境:
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(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 1308·2021-11-15 11:37
阅读 2564·2021-09-22 10:56
阅读 3390·2021-09-06 15:11
阅读 801·2021-08-31 09:45
阅读 2897·2021-07-28 11:16
阅读 1805·2019-08-30 15:44
阅读 476·2019-08-30 13:22
阅读 3343·2019-08-30 13:18