Connect R and Netezza using RJDBC

后端 未结 2 581
孤街浪徒
孤街浪徒 2021-01-23 08:23

I´m trying to connect R with Netezza using the JDBC driver.

I manage to connect succesfully with the database, but the results are not corretc.

# Here ar         


        
相关标签:
2条回答
  • 2021-01-23 09:04

    I scrolled your output all the way to the right and it looks like the strings in your columns are very wide (are they CHAR instead of VARCHAR?), so the result does not fit the width of R console. Hence R displays them that way.

    So try to either trim them in your query

    select rtrim(SUB_ACCOUNT_ID), ...
    

    or in R:

    require('stringr')
    res$SUB_ACCOUNT_ID <- str_trim(res$SUB_ACCOUNT_ID)
    
    0 讨论(0)
  • 2021-01-23 09:11

    Based on Alex answer i wrote this function to use rtrim in all variables.

    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
    }
    

    Function usage

      dt <- query_nzz(
        con,
        select = "select * ",
        from = "DATABASE.TABLENAME",
        where = "",
        limit = 100000
      )
    
    0 讨论(0)
提交回复
热议问题