我正在寻找如何在 postgresql 中将字符串编写为 jsonb 类型的解决方案。所以DynamicFrame有一个字符串列来保存json数据。当尝试保存到 postgres 时
DataSink0 = glueContext.write_dynamic_frame.from_catalog(frame = Transform0, database = "cms", table_name = "cms_public_listings", transformation_ctx = "DataSink0")
我收到以下错误:
遇到错误:
An error occurred while calling o1623.pyWriteDynamicFrame.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 134.0 failed 4 times, most recent failure: Lost task 0.3 in stage 134.0 (TID 137, ip-172-31-27-18.ec2.internal, executor 24): java.sql.BatchUpdateException: Batch entry 0 INSERT INTO "public".listings ([REMOVED_COLUMNS]) VALUES ([REMOVED_VALUES]) was aborted: ERROR: column "schema" is of type jsonb but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 207 Call getNextException to see other errors in the batch.
我无法更改架构来保存字符串,因此要么使用 AWS Glue ETL,要么必须制作 Python Shell 作业。我更愿意找到一种将 PySpark 与 AWS Glue 结合使用的方法。
我更喜欢使用原生 Spark 数据框,因为它允许我进行更多定制。我可以使用字符串类型属性将 json 字段从数据帧转换为表中的 jsonb 字段。对于这种情况,我的数据框有两个字段。
from pyspark import SparkConf
sc = SparkContext.getOrCreate(SparkConf())
spark = SparkSession(sc)
df = spark.read.format('csv') \
.option('delimiter','|') \
.option('header','True') \
.load('your_path')
##some transformation...
url = 'jdbc:postgresql://your_host:5432/your_databasename'
properties = {'user':'*****',
'password':'*****',
'driver': "org.postgresql.Driver",
'stringtype':"unspecified"}
df.write.jdbc(url=url, table='your_tablename', mode='append', properties=properties)
在执行上面的脚本之前,你应该在postgresql中创建表,因为属性mode被设置为append。如下:
create table your_tablename
(
my_json_field jsonb,
another_field int
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)