lambda - lambda函数中的Pyspark udf函数错误
问题描述
我在下面写了一个 udf 函数,它给我一个错误。请帮忙。
下面是我的数据集;
df1 = sqlContext.range(0, 1000)\
.withColumn('normal1',func.abs(10*func.round(randn(seed=1),2)))\
.withColumn('normal2',func.abs(100*func.round(randn(seed=2),2)))\
.withColumn('normal3',func.abs(func.round(randn(seed=3),2)))
df1 = df1.withColumn('Y',when(df1.normal1*df1.normal2*df1.normal3>750, 1)\
.otherwise(0))
udf函数如下:
from pyspark.sql import types as T
balancingRatio=0.8
calculateWeights = udf(lambda d:(1 * balancingRatio) if d==0 else (1 * (1.0 - balancingRatio)),T.IntegerType())
weightedDataset = df1.withColumn('classWeightCol', calculateWeights('Y'))
weightedDataset.show()
这需要一些时间并给我一个错误;
Py4JJavaError: An error occurred while calling o670.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0
in stage 25.0 failed 1 times, most recent failure: Lost task 0.0 in stage
25.0 (TID 427, localhost, executor driver): org.apache.spark.SparkException:
Python worker failed to connect back.
可能是什么问题?谢谢你。
我发现互联网上的一个简单示例也不起作用
maturity_udf = udf(lambda age: "adult" if age >=18 else "child",
T.StringType())
df = sqlContext.createDataFrame([{'name': 'Alice', 'age': 1}])
df.withColumn("maturity", maturity_udf(df.age)).show()
不是:我得到了 python 3.7.1 和 spark 2.4
解决方案
您需要通过将OBJC_DISABLE_INITIALIZE_FORK_SAFETY
变量设置为来禁用分叉安全,YES
这为我解决了这个问题。
import os
os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES'
推荐阅读
- docker - 来自 Docker 的 NEO4j 因果集群不工作
- excel - 计算在直到循环上设置的变量范围的模式
- javascript - 为什么 Heatmap 在错误的位置显示地图?
- python - 使用 Python 到嵌套 JSON 的 DataFrame?
- python - 空格分隔的 csv,列名和值中有空格
- vue.js - 如何为递归组件设置 vuex 存储实例
- jquery - 如何在@Html.partial 中传递变量
- php - 无法连接到 MySQL 数据库 - 它在本地工作但不能在线
- android - Layout Inflator 在 android 4.1.1 AlretDialog 上崩溃
- python - 如何在不丢失信息的情况下保存 float64 图像数据,同时能够将其可视化