gpt4 book ai didi

apache-spark - 如何实现spark sql分页查询

转载 作者:行者123 更新时间:2023-12-03 07:19:30 49 4
gpt4 key购买 nike

有人如何在 Spark sql 查询中进行分页吗?

我需要使用spark sql,但不知道如何进行分页。

尝试过:

select * from person limit 10, 10

最佳答案

已经6年了,不知道当时是否可以。

我会在答案上添加一个顺序ID,并偏移量和偏移量+限制之间搜索寄存器

在纯 Spark SQL 查询中,对于偏移量 10 和限制 10,它会是这样的

WITH count_person AS (
SELECT *, monotonically_increasing_id() AS count FROM person)
SELECT * FROM count_person WHERE count > 10 AND count < 20

在 PySpark 上,情况非常相似

import pyspark.sql.functions as F

offset = 10
limit = 10
df = df.withColumn('_id', F.monotonically_increasing_id())
df = df.where(F.col('_id').between(offset, offset + limit))

即使对于大量数据,它也足够灵活和快速。

关于apache-spark - 如何实现spark sql分页查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227949/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com