繁体   English   中英

如何将第二个数据帧的列传递到PySpark 1.6.1中的UDF

[英]How to pass the column of a second dataframe into a UDF in PySpark 1.6.1

这就是我想要做的。 我想在两个不同的数据帧中对两列的每个条目进行比较。 数据框如下所示:

>>> subject_df.show()
+------+-------------+
|USERID|     FULLNAME|
+------+-------------+
| 12345|  steve james|
| 12346| steven smith|
| 43212|bill dunnigan|
+------+-------------+

>>> target_df.show()
+------+-------------+
|USERID|     FULLNAME|
+------+-------------+
|111123|  steve tyler|
|422226|  linda smith|
|123333|bill dunnigan|
| 56453|  steve smith|
+------+-------------+

这是我尝试使用的逻辑:

# CREATE FUNCTION    
def string_match(subject, targets):
    for target in targets:
        <logic>
    return logic_result

# CREATE UDF
string_match_udf = udf(string_match, IntegerType())

# APPLY UDF
subject_df.select(subject_df.FULLNAME, string_match_udf(subject_df.FULLNAME, target_df.FULLNAME).alias("score"))

这是我在pyspark shell中运行代码时遇到的错误:

py4j.protocol.Py4JJavaError: An error occurred while calling o45.select.
: java.lang.RuntimeException: Invalid PythonUDF PythonUDF#string_match(FULLNAME#2,FULLNAME#5), requires attributes from more than one child.

我认为我的问题的根源是试图将第二列传递给函数。 我应该使用RDD吗? 请记住,实际的subject_df和target_df都超过100,000行。 我愿意接受任何建议。

看起来您对用户定义的函数如何工作有错误的想法:

  • 函数当时只从一行接收值
  • 您无法使用来自无关DataFame的数据。

做你想做的事的唯一方法是采取笛卡尔积。

subject_df.join(target_df).select(
 f(subject_df.FULLNAME, target_df.FULLNAME)
)

其中f是一个比较当时两个元素的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM