mirror of https://git.sr.ht/~hrbrmstr/awsathena
boB Rudis
4 years ago
21 changed files with 267 additions and 31 deletions
@ -0,0 +1,48 @@ |
|||
#' Get Query Execution Results Metadata (Schema) |
|||
#' |
|||
#' @md |
|||
#' @param qxid query execution id |
|||
#' @param region AWS region string |
|||
#' @param profile if not using the default credentials chain or a dedicated |
|||
#' properties file then provide the named profile from `~/.aws/credentials` |
|||
#' you wish to use |
|||
#' @param properties_file if not using the default credentials provider chain or |
|||
#' a named profile then provide the path to an Athena credentials proeprty file. |
|||
#' @export |
|||
get_query_results_metadata <- function(qxid, region = "us-east-1", profile = NULL, properties_file = NULL) { |
|||
|
|||
client <- aws_athena_client(region = region, profile = profile, properties_file = properties_file) |
|||
|
|||
qx_req <- .jnew("com.amazonaws.services.athena.model.GetQueryResultsRequest") |
|||
qx_req$setQueryExecutionId(qxid) |
|||
qx_req$setMaxResults(.jnew(class = "java/lang/Integer", "1")) |
|||
|
|||
res <- client$getQueryResults(qx_req) |
|||
res_rs <- res$getResultSet() |
|||
res_md <- res_rs$getResultSetMetadata() |
|||
res_ci <- res_md$getColumnInfo() |
|||
|
|||
lapply(res_ci, function(.x) { |
|||
data.frame( |
|||
name = .x$getName(), |
|||
type = .x$getType(), |
|||
caseSensitive = .x$getCaseSensitive(), |
|||
catalogName = .x$getCatalogName(), |
|||
label = .x$getLabel(), |
|||
nullable = .x$getNullable(), |
|||
precision = .x$getPrecision(), |
|||
scale = .x$getScale(), |
|||
schemaName = .x$getSchemaName(), |
|||
tableName = .x$getTableName(), |
|||
stringsAsFactors = FALSE |
|||
) |
|||
}) %>% |
|||
do.call(rbind.data.frame, .) -> out |
|||
|
|||
class(out) <- c("athena_query_metadata", "tbl_df", "tbl", "data.frame") |
|||
|
|||
client$shutdown() |
|||
|
|||
out |
|||
|
|||
} |
@ -0,0 +1,65 @@ |
|||
#' Translate from one type system to another |
|||
#' |
|||
#' @param type type (character) |
|||
#' @param to one of `athena` or `r` |
|||
#' @export |
|||
athena_type_trans <- function(type, to = c("r", "athena")) { |
|||
if (match.arg(tolower(to[1]), c("athena", "r")) == "r") { |
|||
sapply(type, switch, |
|||
type, |
|||
boolean = "logical", |
|||
tinyint = "integer", |
|||
smallint = "integer", |
|||
int = "integer", |
|||
integer = "integer", |
|||
bigint = "integer64", |
|||
double = "double", |
|||
float = "double", |
|||
decimal = "double", |
|||
char = "character", |
|||
varchar = "character", |
|||
binary = "raw", |
|||
date = "Date", |
|||
timestamp = "POSIXct", |
|||
array = "character", |
|||
map = "character", |
|||
struct = "character" |
|||
) |
|||
} else { |
|||
sapply( |
|||
type, switch, |
|||
logical = "boolean", |
|||
integer = "integer", |
|||
integer64 = "bigint", |
|||
double = "double", |
|||
character = "varchar", |
|||
raw = "binary", |
|||
Date = "date", |
|||
POSIXct = "timestamp" |
|||
) |
|||
} |
|||
} |
|||
|
|||
#' @rdname athena_type_trans |
|||
#' @param name,type equal length character vectors; type should be an R type |
|||
#' @export |
|||
to_cols <- function(name, type) { |
|||
|
|||
lapply( |
|||
type, switch, |
|||
logical = readr::col_logical(), |
|||
integer = readr::col_integer(), |
|||
integer64 = readr::col_number(), |
|||
double = readr::col_double(), |
|||
character = readr::col_character(), |
|||
raw = readr::col_character(), |
|||
Date = readr::col_date(), |
|||
POSIXct = readr::col_datetime() |
|||
) -> l |
|||
|
|||
l <- set_names(l) |
|||
|
|||
do.call(readr::cols, l) |
|||
|
|||
} |
|||
|
@ -0,0 +1,21 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/type-trans.R |
|||
\name{athena_type_trans} |
|||
\alias{athena_type_trans} |
|||
\alias{to_cols} |
|||
\title{Translate from one type system to another} |
|||
\usage{ |
|||
athena_type_trans(type, to = c("r", "athena")) |
|||
|
|||
to_cols(name, type) |
|||
} |
|||
\arguments{ |
|||
\item{type}{type (character)} |
|||
|
|||
\item{to}{one of `athena` or `r`} |
|||
|
|||
\item{name, type}{equal length character vectors; type should be an R type} |
|||
} |
|||
\description{ |
|||
Translate from one type system to another |
|||
} |
@ -0,0 +1,28 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/get-query-results-meta.R |
|||
\name{get_query_results_metadata} |
|||
\alias{get_query_results_metadata} |
|||
\title{Get Query Execution Results Metadata (Schema)} |
|||
\usage{ |
|||
get_query_results_metadata( |
|||
qxid, |
|||
region = "us-east-1", |
|||
profile = NULL, |
|||
properties_file = NULL |
|||
) |
|||
} |
|||
\arguments{ |
|||
\item{qxid}{query execution id} |
|||
|
|||
\item{region}{AWS region string} |
|||
|
|||
\item{profile}{if not using the default credentials chain or a dedicated |
|||
properties file then provide the named profile from \verb{~/.aws/credentials} |
|||
you wish to use} |
|||
|
|||
\item{properties_file}{if not using the default credentials provider chain or |
|||
a named profile then provide the path to an Athena credentials proeprty file.} |
|||
} |
|||
\description{ |
|||
Get Query Execution Results Metadata (Schema) |
|||
} |
Loading…
Reference in new issue