apache-spark - Pandas UDF 不比 Spark UDF 快吗?

标签 apache-spark pyspark

<分区>

我从 Pyspark 网站获取了以下 UDF,因为我试图了解是否存在性能改进。我做了很大范围的数字,但两者花费的时间几乎相同,我做错了什么?

谢谢!

import pandas as pd
from pyspark.sql.functions import col, udf
from pyspark.sql.types import LongType
import time

start = time.time()
# Declare the function and create the UDF
def multiply_func(a, b):
    return a * b

multiply = udf(multiply_func, returnType=LongType())

# The function for a pandas_udf should be able to execute with local Pandas data
x = pd.Series(list(range(1, 1000000)))
print(multiply_func(x, x))
# 0    1
# 1    4
# 2    9
# dtype: int64
end = time.time()
print(end-start)

这是 Pandas UDF

import pandas as pd
from pyspark.sql.functions import col, pandas_udf
from pyspark.sql.types import LongType
import time

start = time.time()
# Declare the function and create the UDF
def multiply_func(a, b):
    return a * b

multiply = pandas_udf(multiply_func, returnType=LongType())

# The function for a pandas_udf should be able to execute with local Pandas data
x = pd.Series(list(range(1, 1000000)))
print(multiply_func(x, x))
# 0    1
# 1    4
# 2    9
# dtype: int64

最佳答案

除非您的数据足够大以至于不能仅由一个节点处理,否则不应考虑 spark。

Pandas 在单个节点上执行所有操作,而 spark 将数据分发到多个节点进行处理。

因此,如果您在一小组数据上比较性能,pandas 可以胜过 spark。

关于apache-spark - Pandas UDF 不比 Spark UDF 快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61760247/

相关文章:

scala - 从 scala 中的嵌套 json 文件创建 spark 数据框

python - PySpark:组合两个 VectorAssembler 的输出

PySpark - 当值为 "t"和 "f"时如何使用模式读取 BooleanType

python - 带逗号的 PySpark 值不包含逗号? (尝试转换为 ArrayType(StringType()))

machine-learning - 我可以使用带有稀疏向量的数据帧进行交叉验证调整吗?

apache-spark - 连接具有相同分区的两个 DataFrame 后,Spark DataFrame 具有哪些属性?

apache-spark - Spark 物理计划和逻辑计划

maven - 如何将 proto3 与 Hadoop/Spark 一起使用?

pyspark - 我们如何使用 jdbc 执行联接查询,而不是使用 pyspark 获取多个表

apache-spark - Spark SQL : INSERT INTO statement syntax