将Dataframe复制到impala数据库时出错

mwecs4sa  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(443)

我正在尝试将Dataframe结果复制到impala db。但是这样做的时候我犯了错误。

library(RJDBC)
library(implyr)
drv <- JDBC("com.cloudera.impala.jdbc41.Driver","/User/ImpalaJDBC41.jar",identifier.quote="`")      
conn <- dbConnect(drv, "username/password")

RJDBC::dbWriteTable(conn, 'default.segments', df)

我得到下面的错误。

Error in .local(conn, statement, ...) : 
  execute JDBC update query failed in dbSendUpdate ([Cloudera][ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 0, SQL state: TStatus(statusCode:ERROR_STATUS, sqlState:HY000, errorMessage:AnalysisException: Syntax error in line 1:
...ents (id DOUBLE PRECISION,eventdate VARCH...
                         ^
Encountered: IDENTIFIER
Expected: BLOCK_SIZE, COMMENT, COMPRESSION, DEFAULT, ENCODING, INTERMEDIATE, LOCATION, NOT, NULL, PRIMARY, COMMA

CAUSED BY: Exception: Syntax error
), Query: CREATE TABLE default.segments (id DOUBLE 
PRECISION,eventdate VARCHAR(255),segment INTEGER).)

假设数据类型有问题。我已经通过指定数据类型并将值插入数据库来创建表。

RJDBC::dbSendUpdate(conn, paste("CREATE TABLE default.segments (id bigint,eventdate timestamp, segment bigint)",";"))

state1 <- paste0("INSERT INTO default.segments VALUES (", apply(df, 1, function(x) paste(x, collapse = ",")), ")" )
RJDBC::dbSendUpdate(conn, state1)

这也给了我与数据类型相关的错误。

Error in .local(conn, statement, ...) : 
execute JDBC update query failed in dbSendUpdate ([Cloudera] 
[ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 0, 
SQL state: TStatus(statusCode:ERROR_STATUS, sqlState:HY000, 
errorMessage:AnalysisException: Target table 
'default.segments' is incompatible with source expressions.
Expression '2016 - 5 - 29' (type: BIGINT) is not compatible with column 
'eventdate' (type: TIMESTAMP)
), Query: INSERT INTO default.segments VALUES (      3,2016- 
05-29, 79).)

下面是我的Dataframe的结构。

> str(df)
'data.frame':   19065 obs. of  3 variables:
$ id: num  3 3 3 69 102 102 102 102 102 102 ...
$ eventdate: Date, format: "2016-05-29" ...
$ segment: int  79 76 76 18 11 15 7 11 7 11 ...

最后一个错误是 Expression '2016 - 5 - 29' (type: BIGINT) is not compatible with column 'eventdate' (type: TIMESTAMP) 但我在dataframe中的date列是 Date 格式。那会是什么问题呢?有人能帮忙吗。

iqih9akk

iqih9akk1#

日期应以引号提供。可以在插入之前转换列:

df$eventdate <- paste0("'", df$eventdate, "'")

或者,或者,

df$eventdate <- sQuote(df$eventdate)

否则,它将被识别为整数类型。

相关问题