diff --git a/src/main/java/com/study/design/controller/EsController.java b/src/main/java/com/study/design/controller/EsController.java index 68b6164a90e5c416ad36480a41d6f6bb592cfd62..cb392a09ff8c27f531db18488c2be5fb7339a063 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 f44dcfd6147833232ab2a28bb91faa0266c0198e..5487ec807b0fccdf34a59eb462b347d3fd75dae9 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 bd622dd10d38dc4ba64215b682b69fc46f296370..9991ca3432b51f791a20f24dcc27c9664ff6a3c3 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 54c0e616547d7267b194b106c62ceeb7936666ce..548c795be5bae1d10ca7cf01dcd4e24a4daec507 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 e7902cc11466f60fc156aa4f893377b38244aa28..23f8cba4ce285590fb8b5207d6c7a4a28ff31a45 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);