目录
项目需求
要求
普通策略
升级策略:使用迭代器模式
迭代器模式组成
代码实现
查询实体
返回实体
实现类
代码测试
mock的ES返回结果json数据
第一次返回结果
第二次返回结果
第三次返回结果
postMan请求, 控制台打印结果
数据从Mysql 迁移到 Es, Es查询数据默认fetch Size最大为10000条,如果查询超过1万条,需要通过scroll形式进行查询
/**
* 第一次查询用query和fetchSize, 后续用返回结果中的游标cursor
*/
@JsonIgnoreProperties
public class EsSqlQuery {
/**
* 调用ES的查询的sql
*/
private String query;
/**
* 取出的条数
*/
private Long fetchSize;
/**
* ES返回的游标
*/
private String cursor;
public EsSqlQuery(String cursor) {
this.cursor = cursor;
}
public EsSqlQuery(String query, Long fetchSize) {
this.query = query;
this.fetchSize = fetchSize;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public Long getFetchSize() {
return fetchSize;
}
public void setFetchSize(Long fetchSize) {
this.fetchSize = fetchSize;
}
public String getCursor() {
return cursor;
}
public void setCursor(String cursor) {
this.cursor = cursor;
}
}
public class EsSqlResult {
private List
@Component
public class EsQueryProcessor {
// 1.用stream返回,节省内存
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;
/**
* 迭代元素
*/
Iterator> iterator;
/**
* 模拟访问次数
*/
private int i = 1;
// 2.1构造函数进行第一次查询, 初始化后续需要使用的columns和iterator,scrollId
public ScrollIterator(String query, Long fetchSize) {
// 模拟根据query和fetchSize从ES第一次获取数据
String jsonName = "es1.json";
EsSqlResult result = this.getEsSqlResult(jsonName);
columns = CollStreamUtil.toList(result.getColumns(), u -> u.get("name"));
this.scrollId = result.getCursor();
this.iterator = convert(columns, result).iterator();
}
// 2.2根据 scrollId 是否为null进行后续访问,直到scrollId为null
@Override
public boolean hasNext() {
return iterator.hasNext() || scrollNext();
}
private boolean scrollNext() {
if (iterator == null || this.scrollId == null) {
return false;
}
i++;
// 模拟根据query和fetchSize从ES第i次获取数据
String jsonName = "es" + i + ".json";
EsSqlResult result = this.getEsSqlResult(jsonName);
this.scrollId = result.getCursor();
this.iterator = convert(columns, result).iterator();
return iterator.hasNext();
}
@Override
public Map next() {
return iterator.next();
}
// 模拟从ES获取查询结果
private EsSqlResult getEsSqlResult(String jsonName) {
if (StrUtil.isBlank(jsonName)) {
return null;
}
EsSqlResult result = null;
List fileList = FileUtil.loopFiles(ResourceUtil.getResource("json").getFile());
for (File file : fileList) {
if (jsonName.equals(file.getName())) {
String json = FileUtil.readString(file, Charset.forName("UTF-8"));
result = JSONObject.parseObject(json, EsSqlResult.class);
}
}
return result;
}
}
// 3.返回结果传统一点 List
private List> convert(List columns, EsSqlResult result) {
List> results = new ArrayList<>();
for (List
@RestController
public class EsController {
@Autowired
private EsService esService;
@GetMapping("/es")
public Boolean suggestRequirement() {
return esService.query();
}
}
public class EsService {
@Autowired
private EsQueryProcessor processor;
public Boolean query() {
Stream> mapStream = processor.scrollEsStream(null, null);
mapStream.forEach(x -> System.out.println(x));
return true;
}
}
{
"cursor": "safajfkajskjkasg",
"columns": [
{
"name": "username",
"type": "String"
},
{
"name": "age",
"type": "String"
}
],
"rows": [
[
"zhao",
"11"
],
[
"qian",
"22"
]
]
}
{
"cursor": "safajfkajskjkasg",
"rows": [
[
"sun",
"33"
],
[
"li",
"44"
]
]
}
{
"rows": [
[
"zhou",
"55"
],
[
"wu",
"66"
]
]
}