From aefbb74270839d22376ba59f405813d2b443fbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 30 Jul 2023 18:59:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:es=E6=BB=9A=E5=8A=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../study/design/controller/EsController.java | 2 - .../design/esquery/EsQueryProcessor.java | 46 +++++++++++++++---- .../com/study/design/esquery/EsSqlQuery.java | 17 +++++++ .../com/study/design/esquery/EsSqlResult.java | 18 ++++++++ .../com/study/design/service/EsService.java | 6 +-- 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/study/design/controller/EsController.java b/src/main/java/com/study/design/controller/EsController.java index 68b6164..cb392a0 100644 --- a/src/main/java/com/study/design/controller/EsController.java +++ b/src/main/java/com/study/design/controller/EsController.java @@ -13,8 +13,6 @@ public class EsController { @PostMapping("es") public Boolean query(@RequestParam String query, Long fetchSize) { - return esService.query(query, fetchSize); } - } diff --git a/src/main/java/com/study/design/esquery/EsQueryProcessor.java b/src/main/java/com/study/design/esquery/EsQueryProcessor.java index f44dcfd..5487ec8 100644 --- a/src/main/java/com/study/design/esquery/EsQueryProcessor.java +++ b/src/main/java/com/study/design/esquery/EsQueryProcessor.java @@ -13,16 +13,32 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; + +/** + * 查询处理器 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/30 18:11 + */ @Component public class EsQueryProcessor { - //1. 我们要用stream 返回 为了节省内存 + + /** + * 用stream 返回 为了节省内存 + * + * @param query + * @param fetchSize + * @return + */ public Stream> scrollEsStream(String query, Long fetchSize) { return StreamSupport.stream(Spliterators .spliteratorUnknownSize(new ScrollIterator(query, fetchSize), 0), false); } - - //2. 我们要 迭代器 + /** + * 要用迭代器 + */ private class ScrollIterator implements Iterator> { private String scrollId; private List columns; @@ -36,18 +52,23 @@ public class EsQueryProcessor { new EsSqlQuery(query, fetchSize), EsSqlResult.class);//第一次访问的结果出来了 this.scrollId = esSqlResult.getCursor(); this.columns = esSqlResult.getColumns() - .stream().map(x->x.get("name")) + .stream().map(x -> x.get("name")) .collect(Collectors.toList()); this.iterator = convert(columns, esSqlResult).iterator(); } - // hasNext 根据 是否 scrollId 为null进行后续的 第二次,第三次,,,的访问,直到 scrollId 为null + /** + * hasNext 根据 是否 scrollId 为null进行后续的 第二次,第三次,,,的访问,直到 scrollId 为null + * + * @return + */ @Override public boolean hasNext() { return iterator.hasNext() || scrollNext(); } + private boolean scrollNext() { - if(iterator == null || this.scrollId == null) { + if (iterator == null || this.scrollId == null) { return false; } EsSqlResult esSqlResult = restTemplate.postForObject("http://localhost:9200/_sql?format=json", @@ -64,13 +85,18 @@ public class EsQueryProcessor { } - - //3. 返回结果传统一点 List + /** + * 返回结果传统一点 List + * + * @param columns + * @param esSqlResult + * @return + */ private List> convert(List columns, EsSqlResult esSqlResult) { List> results = new ArrayList<>(); - for(List row : esSqlResult.getRows()) { + for (List row : esSqlResult.getRows()) { Map map = new HashMap<>(); - for(int i = 0; i < columns.size(); i++) { + for (int i = 0; i < columns.size(); i++) { map.put(columns.get(i), row.get(i)); } results.add(map); diff --git a/src/main/java/com/study/design/esquery/EsSqlQuery.java b/src/main/java/com/study/design/esquery/EsSqlQuery.java index bd622dd..9991ca3 100644 --- a/src/main/java/com/study/design/esquery/EsSqlQuery.java +++ b/src/main/java/com/study/design/esquery/EsSqlQuery.java @@ -2,10 +2,27 @@ package com.study.design.esquery; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +/** + * 查询参数 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/30 18:10 + */ @JsonIgnoreProperties public class EsSqlQuery { + /** + * 查询的sql + */ private String query; + /** + * 一次获取的数量 + */ private Long fetchSize; + /** + * 游标 + */ private String cursor; public EsSqlQuery(String cursor) { diff --git a/src/main/java/com/study/design/esquery/EsSqlResult.java b/src/main/java/com/study/design/esquery/EsSqlResult.java index 54c0e61..548c795 100644 --- a/src/main/java/com/study/design/esquery/EsSqlResult.java +++ b/src/main/java/com/study/design/esquery/EsSqlResult.java @@ -3,9 +3,27 @@ package com.study.design.esquery; import java.util.List; import java.util.Map; + +/** + * 查询的结果集 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/30 18:11 + */ public class EsSqlResult { + + /** + * 列名 + */ private List> columns; + /** + * 行数据信息 + */ private List> rows; + /** + * 游标 + */ private String cursor; public List> getColumns() { diff --git a/src/main/java/com/study/design/service/EsService.java b/src/main/java/com/study/design/service/EsService.java index e7902cc..23f8cba 100644 --- a/src/main/java/com/study/design/service/EsService.java +++ b/src/main/java/com/study/design/service/EsService.java @@ -1,13 +1,9 @@ package com.study.design.service; import com.study.design.esquery.EsQueryProcessor; -import com.study.design.order.pojo.Order; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.stream.Stream; @@ -16,7 +12,7 @@ public class EsService { @Autowired private EsQueryProcessor esQueryProcessor; - + public Boolean query(String query, Long fetchSize) { Stream> mapStream = esQueryProcessor .scrollEsStream(query, fetchSize); -- GitLab