Access and Query Amazon Athena via DBI/JDBC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.3 KiB

пре 5 година
list(
"-7" = as.logical, # BIT
"-6" = as.integer, # TINYINT
"-5" = bit64::as.integer64, # BIGINT
"-4" = as.character, # LONGVARBINARY
"-3" = as.character, # VARBINARY
"-2" = as.character, # BINARY
"-1" = as.character, # LONGVARCHAR
"0" = as.character, # NULL
"1" = as.character, # CHAR
"2" = as.double, # NUMERIC
"3" = as.double, # DECIMAL
"4" = as.integer, # INTEGER
"5" = as.integer, # SMALLINT
"6" = as.double, # FLOAT
"7" = as.double, # REAL
"8" = as.double, # DOUBLE
"12" = as.character, # VARCHAR
пре 5 година
"16" = as_logical, # BOOLEAN
пре 5 година
"91" = as_date, # DATE
"92" = as.character, # TIME
"93" = as_posixct, # TIMESTAMP
пре 5 година
"2003" = as.character, # ARRAY
пре 5 година
"1111" = as.character # OTHER
) -> .jdbc_converters
пре 5 година
#' Retrieve connection/driver/database metadata
#'
#' @param dbObj driver/connection
#' @param ... unused
пре 5 година
#' @export
пре 5 година
#' @keywords internal
setMethod("dbGetInfo", "AthenaDriver", def=function(dbObj, ...)
list(
name = "AthenaJDBC",
пре 5 година
driver_version = metis.jars::simba_driver_version(),
package_version = utils::packageVersion("metis.jars")
пре 5 година
)
)
пре 5 година
пре 5 година
#' Retrieve connection/driver//database metadata
#'
#' @param dbObj driver/connection
#' @param ... unused
пре 5 година
#' @export
#' @keywords internal
setMethod("dbGetInfo", "AthenaConnection", def=function(dbObj, ...)
list(
name = "AthenaJDBC",
driver_version = list.files(system.file("java", package="metis.lite"), "jar$")[1],
пре 5 година
package_version = utils::packageVersion("metis")
пре 5 година
)
)
пре 5 година
пре 5 година
#' Fetch records from a previously executed query
#'
#' Fetch the next `n` elements (rows) from the result set and return them
#' as a data.frame.
#'
#' @param res An object inheriting from [DBIResult-class], created by
#' [dbSendQuery()].
#' @param n maximum number of records to retrieve per fetch. Use `n = -1`
#' or `n = Inf`
#' to retrieve all pending records. Some implementations may recognize other
#' special values.
пре 5 година
#' @param block clock size
пре 5 година
#' @param ... Other arguments passed on to methods.
#' @export
setMethod(
"fetch",
signature(res="AthenaResult", n="numeric"),
def = function(res, n, block = 1000L, ...) {
пре 5 година
nms <- c()
athena_type_convert <- list()
пре 5 година
cols <- .jcall(res@md, "I", "getColumnCount")
пре 5 година
for (i in 1:cols) {
пре 5 година
ct <- as.character(.jcall(res@md, "I", "getColumnType", i))
пре 5 година
athena_type_convert[[i]] <- .jdbc_converters[[ct]]
пре 5 година
nms <- c(nms, .jcall(res@md, "S", "getColumnLabel", i))
пре 5 година
# message(ct, "|", tail(nms, 1))
пре 5 година
}
athena_type_convert <- set_names(athena_type_convert, nms)
пре 5 година
out <- callNextMethod(res = res, n = n, block = block, ...)
пре 5 година
пре 5 година
# print(str(out))
пре 5 година
for (nm in names(athena_type_convert)) {
пре 5 година
f <- athena_type_convert[[nm]]
if (length(f) == 0) f <- as.character # catchall in case AMZN is tricksy
out[[nm]] <- f(out[[nm]])
пре 5 година
}
пре 5 година
out
}
)
#' AthenaJDBC
#'
#' @param conn Athena connection
#' @param statement SQL statement
#' @param ... unused
#' @importFrom rJava .jcall
#' @export
setMethod(
"dbGetQuery",
signature(conn="AthenaConnection", statement="character"),
пре 5 година
definition = function(conn, statement, ...) {
пре 5 година
r <- dbSendQuery(conn, statement, ...)
on.exit(.jcall(r@stat, "V", "close"))
пре 5 година
res <- fetch(r, -1, block = conn@fetch_size)
пре 5 година
пре 5 година
class(res) <- c("tbl_df", "tbl", "data.frame")
res
}
)