【发布时间】:2021-09-16 15:39:59
【问题描述】:
所以我正在使用 PySpark(Spark 3.0.1、Scala 2.12)将数据从 MySQL (5.7) 数据库移动到 PostgreSQL (12.7) 数据库。命运模型中的表有一个 Enum 列。
CREATE TYPE ORDER_STATUS AS ENUM (
'SHIPPED','PAID','REFUNDED','PARTIALLY_REFUNDED','PROCESSING');
插入时:
df_orders.select(df_orders.columns).write.format('jdbc').options(**postgres_write_opts_table).mode('append').save()
我得到下一个异常
Caused by: org.postgresql.util.PSQLException: ERROR: column "status" is of type order_status but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
基本上我需要将列状态转换为 ORDER_STATUS 。我曾尝试使用UserDefinedType(PySpark 没有SQLUserDefinedType),但我不知道我在做什么,因为文档不是很清楚。
class StatusUDT(UserDefinedType):
@classmethod
def sqlType(self):
return NullType()
@classmethod
def module(cls):
return cls.__module__
def serialize(self, obj):
return f"{obj.value}::order_status_type"
def deserialize(self, datum):
return {x.value: x for x in Some}[datum]
然后我尝试铸造
df_orders = df_orders.withColumn("status", col("status").cast(StatusUDT()))
然后我收到下一个错误:
AnalysisException: cannot resolve 'CAST(`status` AS NULL)' due to data type mismatch: cannot cast string to null;;
有什么方法可以转换这个 Enum 吗?
【问题讨论】:
标签: postgresql apache-spark pyspark