从R到Redshift写入数据框时出错

编程入门 行业动态 更新时间:2024-10-23 04:58:18
本文介绍了从R到Redshift写入数据框时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在R中有一个数据框,其中包含各种不同的数据类型。从R将数据帧写入redshift服务器时,仅字符和时间戳值出现错误。我在下面添加了R代码段,以使您对该问题有更多的了解。

I have a dataframe in R with various different data types. While writing the dataframe from R to redshift server, I am getting error only with character and timestamp values. I am adding R code snippet below to give you more idea about the issue.

library(lubridate) library(dplyr) dat <- data.frame(id = letters[1:2], x = 2:3, date = now()) dat str(dat) drv <- dbDriver("PostgreSQL") conn <- dbConnect(drv, host="redshift.amazonaws", port="5439", dbname="abcd", user="xyz", password="abc") DBI::dbGetQuery(conn, "DROP TABLE test21;") DBI::dbGetQuery(conn, "CREATE TABLE test21 ( id VARCHAR(255), x INT, date timestamp);") chunksize = 100 for (i in 1:ceiling(nrow(dat)/chunksize)) { query = paste0('INSERT INTO test21 (',paste0(colnames(dat),collapse = ','),') VALUES ') vals = NULL for (j in 1:chunksize) { k = (i-1)*chunksize+j if (k <= nrow(dat)) { vals[j] = paste0('(', paste0(dat[k,],collapse = ','), ')') } } query = paste0(query, paste0(vals,collapse=',')) DBI::dbExecute(conn, query) }

在运行最后一部分时,我得到了出现以下错误:

While running the last part, I am getting the below error:

RS-DBI driver: (could not Retrieve the result : ERROR: column "date" is of type timestamp without time zone but expression is of type numeric HINT: You will need to rewrite or cast the expression.

当我在redshift表中手动输入值时,就如预期的那样。

When I manually entered the values into the redshift table, it came as expected.

DBI :: dbGetQuery(conn, INSERT INTO test21(id,x,date)values('a','2','2019-02-08 15:21:08'),('b','3','2019-02-08 15:21:08'))

我感觉到此问题是由某些程序错误引起的。

I am sensing that this issue is coming from some programmatic error. requesting your advise on the same where I am doing wrong with the code.

推荐答案

在数据框的日期字段中,尝试替换

In the date field of your dataframe, try replacing

now()

with

substr(now(),1,19)

更多推荐

从R到Redshift写入数据框时出错

本文发布于:2023-10-24 14:40:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Redshift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!