【发布时间】:2017-05-14 10:05:25
【问题描述】:
在另一个数据帧上执行 UDF 时如何引用 pyspark 数据帧?
这是一个虚拟示例。我正在创建两个数据框scores 和lastnames,每个数据框内都有一个在两个数据框中相同的列。在scores 上应用的UDF 中,我想过滤lastnames 并返回在lastname 中找到的字符串。
from pyspark import SparkContext
from pyspark import SparkConf
from pyspark.sql import SQLContext
from pyspark.sql.types import *
sc = SparkContext("local")
sqlCtx = SQLContext(sc)
# Generate Random Data
import itertools
import random
student_ids = ['student1', 'student2', 'student3']
subjects = ['Math', 'Biology', 'Chemistry', 'Physics']
random.seed(1)
data = []
for (student_id, subject) in itertools.product(student_ids, subjects):
data.append((student_id, subject, random.randint(0, 100)))
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
schema = StructType([
StructField("student_id", StringType(), nullable=False),
StructField("subject", StringType(), nullable=False),
StructField("score", IntegerType(), nullable=False)
])
# Create DataFrame
rdd = sc.parallelize(data)
scores = sqlCtx.createDataFrame(rdd, schema)
# create another dataframe
last_name = ["Granger", "Weasley", "Potter"]
data2 = []
for i in range(len(student_ids)):
data2.append((student_ids[i], last_name[i]))
schema = StructType([
StructField("student_id", StringType(), nullable=False),
StructField("last_name", StringType(), nullable=False)
])
rdd = sc.parallelize(data2)
lastnames = sqlCtx.createDataFrame(rdd, schema)
scores.show()
lastnames.show()
from pyspark.sql.functions import udf
def getLastName(sid):
tmp_df = lastnames.filter(lastnames.student_id == sid)
return tmp_df.last_name
getLastName_udf = udf(getLastName, StringType())
scores.withColumn("last_name", getLastName_udf("student_id")).show(10)
以下是trace的最后一部分:
Py4JError: An error occurred while calling o114.__getnewargs__. Trace:
py4j.Py4JException: Method __getnewargs__([]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:335)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:344)
at py4j.Gateway.invoke(Gateway.java:252)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:209)
at java.lang.Thread.run(Thread.java:745)
【问题讨论】:
-
您不能在 UDF 中访问
df,因为它将在 executor 中处理,而dfref 只能从驱动程序访问。您可以为lastnames使用广播变量。如果需要任何帮助,请告诉我。 -
但请考虑将
lastnames与scores一起加入,而不是从UDF 中加入。 -
嗨@mrsrinivas,感谢您的回复。首先,我无法加入,因为即使这个虚拟示例可以使用加入来解决,但在我的实际实现中,我需要在 UDF 中进行更多处理。其次,是的!在这种情况下如何使用广播变量?
标签: apache-spark dataframe pyspark user-defined-functions broadcast