使用 PySpark 将数据帧移动到 Redshift



>我有一个红移表,如下所示

id, name, address
1, 'aaa', 'xxx'
2, 'bbb', 'yyy'

我在 pyspark 中有一个数据帧作为

id, name, address
1, 'ccc', 'zzz'
5, 'ddd', 'xyx'

现在我需要使用 upsert 模式将 pyspark 中的数据帧上传到 redshift 表。

谁能帮我怎么做

为此,请使用 Redshift 数据源 for Apache Spark。请记住,在加载此数据之前,Redshift 中应该已经存在这些表。

首先,您需要检查预期表在红移中的存在。然后,您可以从以下代码中获取帮助。

# Write back to a table
df.write 
  .format("com.databricks.spark.redshift") 
  .option("url", "jdbc:redshift://redshifthost:5439/database?user=username&password=pass") 
  .option("dbtable", "my_table_copy") 
  .option("tempdir", "s3n://path/for/temp/data") 
  .mode("error") 
  .save()

最新更新