Amazon Athena JDBC Driver Wrapper Supporting the 'metis' Package
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.

49 lines
1.3KB

  1. #' Make a JDBC connection to Athena
  2. #'
  3. #' Handles the up-front JDBC config
  4. #'
  5. #' For all connection types it is expected that you have the following environment variables
  6. #' defined (a good place is `~/.Renviron`):
  7. #'
  8. #' - `AWS_S3_STAGING_DIR`: the name of the S3 bucket where Athena can write stuff
  9. #' - `AWS_PROFILE`: the AWS profile ID in `~/.aws/credentials` (defaults to `default` if not present)
  10. #'
  11. #' For `simple` == `FALSE` the expectation is that you're working with a managed
  12. #' `~/.aws/credentials` file.
  13. #'
  14. #' @md
  15. #' @param default_schema def sch
  16. #' @param simple simple
  17. #' @export
  18. athena_connect <- function(default_schema, simple=FALSE) {
  19. athena_jdbc <- Athena()
  20. aws_config <- ini::read.ini(path.expand("~/.aws/credentials"))
  21. aws_profile <- aws_config[Sys.getenv("AWS_PROFILE", "default")][[1]]
  22. Sys.unsetenv("AWS_ACCESS_KEY_ID")
  23. Sys.unsetenv("AWS_SECRET_ACCESS_KEY")
  24. Sys.setenv(AWS_ACCESS_KEY_ID = aws_profile$aws_access_key_id)
  25. Sys.setenv(AWS_SECRET_ACCESS_KEY = aws_profile$aws_secret_access_key)
  26. con <- NULL
  27. if (!simple) {
  28. Sys.unsetenv("AWS_SESSION_TOKEN")
  29. Sys.setenv(AWS_SESSION_TOKEN = aws_profile$aws_session_token)
  30. con <- dbConnect(athena_jdbc, schema_name = default_schema)
  31. } else {
  32. con <- dbConnect(athena_jdbc, provider = NULL, schema_name = default_schema)
  33. }
  34. con
  35. }