gpt4 book ai didi

apache-spark - Spark : Union can only be performed on tables with the compatible column types. 结构<名称,ID> != 结构

转载 作者:行者123 更新时间:2023-12-04 05:23:57 24 4
gpt4 key购买 nike

错误 :
联合只能在具有兼容列类型的表上执行。
结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符串,skyward_points:字符串)在第二个表的第一列;;

这里结构字段的顺序是不同的,但其余的都是一样的。

数据框 1 架构

root
|-- emcg_uuid: string (nullable = true)
|-- name: string (nullable = true)
|-- phone_no: string (nullable = true)
|-- dob: string (nullable = true)
|-- country: string (nullable = true)
|-- travel_type: string (nullable = true)
|-- gdpr_restricted_flg: string (nullable = false)
|-- gdpr_reason_code: string (nullable = false)
|-- document: map (nullable = true)
| |-- key: string
| |-- value: string (valueContainsNull = true)
|-- skyward: struct (nullable = false)
| |-- tier: string (nullable = false)
| |-- skyward_number: string (nullable = false)
| |-- skyward_points: string (nullable = false)

dataframe2 schema
root
|-- emcg_uuid: string (nullable = true)
|-- name: string (nullable = true)
|-- phone_no: string (nullable = true)
|-- dob: string (nullable = true)
|-- country: string (nullable = true)
|-- travel_type: string (nullable = true)
|-- gdpr_restricted_flg: string (nullable = true)
|-- gdpr_reason_code: string (nullable = true)
|-- document: map (nullable = true)
| |-- key: string
| |-- value: string (valueContainsNull = true)
|-- skyward: struct (nullable = false)
| |-- skyward_number: string (nullable = false)
| |-- tier: string (nullable = false)
| |-- skyward_points: string (nullable = false)

如何解决这个问题?

最佳答案

union 的默认 Spark 行为是标准的 SQL 行为,所以按位置匹配。这意味着,两个 DataFrame 中的架构必须以相同的顺序包含具有相同字段的相同字段。

如果要按名称匹配模式,请使用 unionByName ,在 Spark 2.3 中引入。

您还可以重新映射字段:

val df1 = ...
val df2 = /...
df1.toDF(df2.columns: _*).union(df2)

编辑:我现在看到了编辑。

您可以再次添加这些列:
import org.apache.spark.sql.functions._
val withCorrectedStruct = df1.withColumn("skyward", struct($"skyward_number", $"tier", $"skyward_points"))

关于apache-spark - Spark : Union can only be performed on tables with the compatible column types. 结构<名称,ID> != 结构<id,名称>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52186226/

24 4 0