文章目录
概要
在实际业务中,很多时候需要从ES中操作大量数据(如百万千万级别等),今天咱们通过springboot结合elasticsearch-rest-client实现对大数据的操作,其中用到了滚动查询(scroll)机制逐批获取数据以提高性能,本次会结合一个批量导出ES中用户操作日志的业务进行展开
所需组件(注意版本)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.12.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.12.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
技术名词解释
- 滚动查询(scroll):滚动查询逻辑,我们通过滚动查询(scroll)机制逐批获取数据,如每次获取 1000 条数据,直到最多返回 100000条数据等
- 性能:滚动查询可以在大数据量时有效避免内存溢出问题,但要确保对 Elasticsearch 集群进行适当的资源控制,避免因滚动查询过多消耗内存或资源。
完整代码
` 提示:
- Controller层代码
/**
* 导出用户操作日志
* @throws Exception
*/
@PostMapping("exportoperationlog")
public ResponseEntity<byte[]> ExportOperationlog(@RequestBody UserOperateLogEntity entity) throws Exception {
Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ResultUserOperateLogModel resultUserOperateLogModel = service.GetAllOperationLog(entity);
List<UserOperateLogModel> list = resultUserOperateLogModel.rows;
Sheet sheet = workbook.createSheet("Sheet1");
// 创建头部行
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("用户名");
headerRow