如何使用pool包逃避Athena database.table?

时间:2022-10-27 09:46:02

I'm trying to connect to Amazon Athena via JDBC and pool:

我正在尝试通过JDBC和池连接到Amazon Athena:

What has worked so far:

到目前为止有效:

library(RJDBC)
library(DBI)
library(pool)
library(dplyr)
library(dbplyr)

drv <- RJDBC::JDBC('com.amazonaws.athena.jdbc.AthenaDriver', '/opt/jdbc/AthenaJDBC41-1.1.0.jar')

pool_instance <- dbPool(
  drv = drv,
  url = "jdbc:awsathena://athena.us-west-2.amazonaws.com:443/",
  user = "me",
  s3_staging_dir = "s3://somedir",
  password = "pwd"
)

mydata <- DBI::dbGetQuery(pool_instance, "SELECT * 
                                          FROM myDB.myTable
                                          LIMIT 10")

mydata

---> Works fine. Correct data is beeing returned.

--->工作正常。正确的数据正在返回。

That does not work:

这不起作用:

pool_instance %>% tbl("myDB.myTable") %>% head(10)
# Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ",  : 
#   Unable to retrieve JDBC result set for SELECT *
# FROM "myDB.myTable" AS "zzz2"
# WHERE (0 = 1) ( Table myDB.myTable not found. Please check your query.)

The problem here is that Athena expects the following syntax as SQL:

这里的问题是Athena期望以下语法为SQL:

Either:

或者:

SELECT *
FROM "myDB"."myTable"

Or:

要么:

SELECT *
FROM myDB.myTable

So basically, by passing the string "myDB.myTable":

所以基本上,通过传递字符串“myDB.myTable”:

pool_instance %>% tbl("myDB.myTable") %>% head(10)

The following syntax is being used:

正在使用以下语法:

SELECT *
FROM "myDB.myTable"

which results in the following error since such table doesn't exist:

由于此表不存在,导致以下错误:

# Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ",  : 
#  Unable to retrieve JDBC result set for SELECT *
# FROM "myDB.myTable" AS "zzz6"
# WHERE (0 = 1) ( Table myDB.myTable not found. Please check your query.)

What I have tried:

我试过的:

So therefore I have tried to pass either "myDB"."myTable" or myDB.myTable to tbl() unsuccessfully:

所以我试图将“myDB”。“myTable”或myDB.myTable传递给tbl()失败:

I have tried to use capture.output(cat('\"myDB\".\"myTable\"')):

我试过使用capture.output(cat('\“myDB \”。\“myTable \”')):

pool_instance %>% tbl(capture.output(cat('\"myDB\".\"myTable\"'))) %>% head(10)
# Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ",  : 
#   Unable to retrieve JDBC result set for SELECT *
# FROM """myDB"".""myTable""" AS "zzz4"
# WHERE (0 = 1) ( Table ""myDB"".""myTable"" not found. Please check your query.)

pool_instance %>% tbl(noquote("myDB"."myTable") %>% head(10)
# Error in UseMethod("as.sql") : 
#   no applicable method for 'as.sql' applied to an object of class "noquote"

1 个解决方案

#1


4  

You can use dbplyr::in_schema:

您可以使用dbplyr :: in_schema:

pool_instance %>% tbl(in_schema("myDB", "myTable")) %>% head(10)

#1


4  

You can use dbplyr::in_schema:

您可以使用dbplyr :: in_schema:

pool_instance %>% tbl(in_schema("myDB", "myTable")) %>% head(10)