apache-spark - Pyspark 从空数据框中选择抛出异常
问题描述
这个问题与这个问题类似,但没有答案,我有一个数据框,如果存在,我会从中选择数据
schema = StructType([
StructField("file_name", StringType(), True),
StructField("result", ArrayType(StructType()), True),
])
df = rdd.toDF(schema=schema)
print((df.count(), len(df.columns))) # 0,2
df.cache()
df = df.withColumn('result', F.explode(df['result']))
get_doc_id = F.udf(lambda line: ntpath.basename(line).replace('_all.txt', ''), StringType())
df = df.filter(df.result.isNotNull()).select(F.lit(job_id).alias('job_id'),
get_doc_id(df['file_name']).alias('doc_id'),
df['result._2'].alias('line_content'),
df['result._4'].alias('line1'),
df['result._3'].alias('line2'))
当数据框为空时,上面会引发错误
pyspark.sql.utils.AnalysisException: 'No such struct field _2 in ;
它不应该只在result
列有数据时执行吗?以及如何克服这一点?
解决方案
Spark 懒惰地执行代码。因此它不会检查您的过滤条件中是否有数据。您的代码在分析阶段失败,因为您的数据中没有名为 result._2 的列。您在结果列的架构中传递空的 StructType 。您应该将其更新为如下内容:
schema = StructType([
StructField("file_name", StringType(), True),
StructField("result", ArrayType(StructType([StructField("line_content",StringType(),True), StructField("line1",StringType(),True), StructField("line2",StringType(),True)])), True)
])
df = spark.createDataFrame(sc.emptyRDD(),schema=schema)
df = df.withColumn('result', F.explode(df['result']))
get_doc_id = F.udf(lambda line: ntpath.basename(line).replace('_all.txt', ''), StringType())
df = df.filter(df.result.isNotNull()).select(F.lit('job_id').alias('job_id'),
get_doc_id(df['file_name']).alias('doc_id'),
df['result.line_content'].alias('line_content'),
df['result.line1'].alias('line1'),
df['result.line2'].alias('line2'))
推荐阅读
- go - 无法使用 Golang http.PostForm 向 Web 表单发出 POST 请求
- swiftui - ObservedObject 没有接收到变量的变化
- bash - sshpass 在 Bash 中不起作用,但在命令行外部起作用
- apache - 将网站从 http 迁移到 https,现在它已损坏
- wsh - 从 WScript.exe 启动的 Internet Explorer 不会出现在顶部
- python - 使用 sqlalchemy 读取带有小写列的 pandas 表
- c# - Dropbox API,如何查看每个团队成员的文件夹权限?
- google-chrome - 在 Chrome 中使用 GStreamer 进行 MJPEG 流式传输
- elasticsearch - 通过嵌套属性计算 ElasticSearch 中的搜索结果
- mysql - 错误 #1046:列的数据太长,但源列和目标列的类型均为 char(1)