【elasticsearch】springboot结合滚动查询(scroll)机制,对elasticsearch进行大数据操作,高性能查询ES中某个索引中的大量数据

概要

在实际业务中,很多时候需要从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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来学吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值