我正在尝试使用JDBC驱动程序将R与Netezza连接。
我设法成功连接数据库,但结果不是正确的。
# Here are the connection details
library(RJDBC)
drv <- JDBC(driverClass="org.netezza.Driver", classPath = "C://JDBC//nzjdbc.jar", "'")
con <- dbConnect(drv, "jdbc:netezza://10.206.0.66:5480//DBASE", "USER", "PASS")
# > con
# An object of class "JDBCConnection"
# Slot "jc":
# [1] "Java-Object{org.netezza.sql.NzConnection@bce3d7}"
# Slot "identifier.quote":
# [1] "'"
res <- dbSendQuery(con, "SELECT * FROM DBASE.MARBEL.DATOS limit 10000;")
res <- fetch(res, n = -1)
问题是字段被重新列为具有“垂直”变量的列表而不是表的列!
head(res)
SUBSCRIPTION_ID
1 245206318120314
2 235109338101206
3 238463669110624
4 214177015090830
5 212403495090830
6 13874138618090824
SUB_ACCOUNT_ID
1 MV_SUBCTA_45206318_20120316
2 MV_SUBCTA_35109338_20101207
3 MV_SUBCTA_38463669_20110627
4 MV_SUBCTA_45223848_20120316
5 MV_SUBCTA_12403495_20081224
6 MV_SUBCTA_18932919_20091012
ACCOUNT_ID
1 MV_CTA_44123765_20120316
2 MV_CTA_35213277_20101207
3 MV_CTA_37772612_20110627
4 MV_CTA_14217213_20090330
5 MV_CTA_12477560_20081224
6 MV_CTA_18758944_20091012
ACCESS_METHOD_ID
1 1167391804
2 1159354610
3 2966407995
4 1153360304
5 1131960835
6 3874138618
知道怎么解决这个问题?我有一个有效的ODBC连接,但我宁愿使用JDBC。
答案 0 :(得分:1)
我将输出一直向右滚动,看起来列中的字符串非常宽(它们是CHAR而不是VARCHAR吗?),因此结果不符合R控制台的宽度。因此R以这种方式显示它们。
因此,请尝试在查询中修剪它们
select rtrim(SUB_ACCOUNT_ID), ...
或在R:
require('stringr')
res$SUB_ACCOUNT_ID <- str_trim(res$SUB_ACCOUNT_ID)
答案 1 :(得分:1)
根据Alex的回答,我写了这个函数,在所有变量中使用rtrim。
query_nzz <- function(con, select="select * ", from="", where = "", limit = " 10000; "){
options(scipen=666)
# Get variable Names
query_names = paste(select, " from ",from, where, sep = " ")
names <- dbGetQuery(con, paste(query_names,"limit 1;", sep= " "))
names <- names(names)
# Trim spaces
select <- paste0("trim(",names,") as ", names, collapse = ",")
query = paste0("select ", select, " from ", from, where, " limit ",deparse(limit),";")
data = dbGetQuery(con, query)
data
}
功能用途
dt <- query_nzz(
con,
select = "select * ",
from = "DATABASE.TABLENAME",
where = "",
limit = 100000
)