计数最小草图

返回具有给定 esp、置信度和种子的列的 count-min 草图。 结果是字节数组,可以在使用前反序列化为字节 CountMinSketch 数组。 Count-min sketch 是一个概率数据结构,用于通过子线性空间进行基数估算。

Syntax

from pyspark.sql import functions as sf

sf.count_min_sketch(col, eps, confidence, seed=None)

参数

参数 类型 Description
col pyspark.sql.Column 或 str 要计算的目标列。
eps pyspark.sql.Column 或浮点数 相对错误,必须为正。
confidence pyspark.sql.Column 或浮点数 置信度,必须为正值且小于 1.0。
seed pyspark.sql.Column 或 int,可选 随机种子。

退货

pyspark.sql.Column:列的 count-min 草图

例子

示例 1:使用列作为参数

from pyspark.sql import functions as sf
spark.range(100).select(
    sf.hex(sf.count_min_sketch(sf.col("id"), sf.lit(3.0), sf.lit(0.1), sf.lit(1)))
).show(truncate=False)
+------------------------------------------------------------------------+
|hex(count_min_sketch(id, 3.0, 0.1, 1))                                  |
+------------------------------------------------------------------------+
|0000000100000000000000640000000100000001000000005D8D6AB90000000000000064|
+------------------------------------------------------------------------+

示例 2:使用数字作为参数

from pyspark.sql import functions as sf
spark.range(100).select(
    sf.hex(sf.count_min_sketch("id", 1.0, 0.3, 2))
).show(truncate=False)
+----------------------------------------------------------------------------------------+
|hex(count_min_sketch(id, 1.0, 0.3, 2))                                                  |
+----------------------------------------------------------------------------------------+
|0000000100000000000000640000000100000002000000005D96391C00000000000000320000000000000032|
+----------------------------------------------------------------------------------------+

示例 3:使用长种子

from pyspark.sql import functions as sf
spark.range(100).select(
    sf.hex(sf.count_min_sketch("id", sf.lit(1.5), 0.2, 1111111111111111111))
).show(truncate=False)
+----------------------------------------------------------------------------------------+
|hex(count_min_sketch(id, 1.5, 0.2, 1111111111111111111))                                |
+----------------------------------------------------------------------------------------+
|00000001000000000000006400000001000000020000000044078BA100000000000000320000000000000032|
+----------------------------------------------------------------------------------------+