我在 Azure 上的 Jupyter 中使用 PySpark。我正在尝试在数据帧上使用 UDF 进行测试,但是 UDF 没有执行。
我的数据框是由以下人员创建的:
users = sqlContext.sql("SELECT DISTINCT userid FROM FoodDiaryData")
我已确认此数据框填充了 100 行。在下一个单元格中,我尝试执行一个简单的 udf。
def iterateMeals(user):
print user
users.foreach(iterateMeals)
这不会产生任何输出。我希望数据框中的每个条目都已被打印。但是,如果我只是尝试 iterateMeals('test')
它将触发并打印“test”。我还尝试使用 pyspark.sql.functions
from pyspark.sql.functions import udf
def iterateMeals(user):
print user
f_iterateMeals = udf(iterateMeals,LongType())
users.foreach(f_iterateMeals)
当我尝试此操作时,我收到以下错误:
Py4JError: An error occurred while calling o461.__getnewargs__. Trace:
py4j.Py4JException: Method __getnewargs__([]) does not exist
有人可以解释一下我哪里出了问题吗?我需要在 .foreach
内执行 udfs此应用程序的数据帧。
最佳答案
您不会看到输出,因为
print
在工作节点上执行并转到相应的输出。请参阅Why does foreach not bring anything to the driver program?以获得完整的解释。foreach
在RDD
上运行,而不是在DataFrame
上运行。UDF
在此上下文中无效。
关于python - PySpark:UDF 未在数据帧上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193789/