首页 > 解决方案 > R将tbl对象更新为红移

问题描述

我在 redshift db 中有一个表,我使用 copy_to 命令从 R 更新值。插入到 R 中的变量的类是:

class(df)
[1] "tbl_dbi"  "tbl_sql"  "tbl_lazy" "tbl" 

使用的 copy_to 命令是:

copy_to(acc_conn, df,name = "test_output",temporary = FALSE)

然而,copy_to 命令只插入而不更新。还有其他更新红移表的可能性吗?

标签: ramazon-redshift

解决方案


我写了一篇详尽的博文,解释了从 Amazon Redshift 读取/写入数据的许多细微差别:https ://auth0.com/blog/a-comprehensive-guide-for-connecting-with-r-to-redshift /

特别是,使用 R 读取数据的最佳方法是使用 RPostgres 库,并且我建议使用我创建的 R 包写入数据:https ://github.com/sicarul/redshiftTools

特别是,它具有 rs_upsert_table 函数,该函数使用您的示例执行 data.frame 的 upsert:

rs_upsert_table(df, dbcon=acc_conn, table_name="test_output", bucket="mybucket", split_files=4)

您需要一个 S3 存储桶和 AWS 密钥才能访问它,因为此包上传到 S3,然后将 redshift 定向到该存储桶,这是批量上传数据的最快方式。


推荐阅读