Sparksql使用where子句获取示例行



是否可以使用where子句获得查询的n行样本?

我尝试使用下面的表样函数,但我最终只在第一个分区' 20121-09-14 '中获得记录。P

select * from (select * from table where s in ('2021-09-14', '2021-09-15')) tablesample(100行)

你可以使用单调递增的ID -这里或Rand来生成一个额外的列,可以用来订购你的数据集来生成必要的采样字段

这两个函数可以结合使用,也可以单独使用

进一步,您可以使用LIMIT子句对所需的N记录进行采样

注:order是一个开销很大的操作

数据准备

input_str = """
1   2/12/2019   114 2
2   3/5/2019    116 1
3   3/3/2019    120 6
4   3/4/2019    321 10
6   6/5/2019    116 1
7   6/3/2019    116 1
8   10/1/2019   120 3
9   10/1/2019   120 3
10  10/1/2020   120 3
11  10/1/2020   120 3
12  10/1/2020   120 3
13  10/1/2022   120 3
14  10/1/2021   120 3
15  10/6/2019   120 3
""".split()
input_values = list(map(lambda x: x.strip() if x.strip() != 'null' else None, input_str))
cols = list(map(lambda x: x.strip() if x.strip() != 'null' else None, "shipment_id  ship_date   customer_id quantity".split()))

n = len(input_values)
input_list = [tuple(input_values[i:i+4]) for i in range(0,n,4)]
sparkDF = sql.createDataFrame(input_list, cols)
sparkDF = sparkDF.withColumn('ship_date',F.to_date(F.col('ship_date'),'d/M/yyyy'))
sparkDF.show()
+-----------+----------+-----------+--------+
|shipment_id| ship_date|customer_id|quantity|
+-----------+----------+-----------+--------+
|          1|2019-12-02|        114|       2|
|          2|2019-05-03|        116|       1|
|          3|2019-03-03|        120|       6|
|          4|2019-04-03|        321|      10|
|          6|2019-05-06|        116|       1|
|          7|2019-03-06|        116|       1|
|          8|2019-01-10|        120|       3|
|          9|2019-01-10|        120|       3|
|         10|2020-01-10|        120|       3|
|         11|2020-01-10|        120|       3|
|         12|2020-01-10|        120|       3|
|         13|2022-01-10|        120|       3|
|         14|2021-01-10|        120|       3|
|         15|2019-06-10|        120|       3|
+-----------+----------+-----------+--------+

排序-单调递增ID &兰德

sparkDF.createOrReplaceTempView("shipment_table")
sql.sql("""
SELECT
*
FROM (
SELECT 
*
,monotonically_increasing_id() as increasing_id
,RAND(10) as random_order
FROM shipment_table
WHERE ship_date BETWEEN '2019-01-01' AND '2019-12-31'
ORDER BY monotonically_increasing_id() DESC ,RAND(10) DESC
LIMIT 5
)
""").show()
+-----------+----------+-----------+--------+-------------+-------------------+
|shipment_id| ship_date|customer_id|quantity|increasing_id|       random_order|
+-----------+----------+-----------+--------+-------------+-------------------+
|         15|2019-06-10|        120|       3|   8589934593|0.11682250456449328|
|          9|2019-01-10|        120|       3|   8589934592|0.03422639313807285|
|          8|2019-01-10|        120|       3|            6| 0.8078688178371882|
|          7|2019-03-06|        116|       1|            5|0.36664222617947817|
|          6|2019-05-06|        116|       1|            4|    0.2093704977577|
+-----------+----------+-----------+--------+-------------+-------------------+

如果您正在使用Dataset,则有内置功能,如文档中所述:

sample(withReplacement: Boolean, fraction: Double): Dataset[T]
Returns a new Dataset by sampling a fraction of rows, using a random seed.
withReplacement: Sample with replacement or not.
fraction: Fraction of rows to generate, range [0.0, 1.0].
Since
1.6.0
Note
This is NOT guaranteed to provide exactly the fraction of the total count of the given Dataset.

要使用此功能,您需要根据您正在寻找的任何标准过滤数据集,然后对结果进行采样。如果您需要一个精确的行数,而不是一个分数,您可以使用limit(n)跟随sample的调用,其中n是要返回的行数。

最新更新