View Source ReqAthena (ReqAthena v0.1.4)

Req plugin for AWS Athena.

ReqAthena makes it easy to make Athena queries. Query results are decoded into the ReqAthena.Result struct. The struct implements the Table.Reader protocol and thus can be efficiently traversed by rows or columns.

Summary

Functions

Link to this function

attach(request, options \\ [])

View Source

Attaches to Req request.

Request Options

  • :access_key_id - Required. The Access Key ID from AWS credentials.

  • :secret_access_key - Required. The Secret Access Key from AWS credentials.

  • :token - Optional. The Session Token from AWS credentials.

  • :region - Required. The AWS region where AWS Athena is installed.

  • :database - Required. The AWS Athena database name.

  • :output_location - Conditional. The S3 url location to output AWS Athena query results.

  • :workgroup - Conditional. The AWS Athena workgroup.

  • :cache_query - Optional. Forces a non-cached result from AWS Athena.

  • :athena - Required. The query to execute. It can be a plain sql string or a {query, params} tuple, where query can contain ? placeholders and params is a list of corresponding values.

Conditional fields must always be defined, and can be one of the fields or both.

If you want to set any of these options when attaching the plugin, pass them as the second argument.

Examples

With plain query string:

iex> opts = [
...>   access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"),
...>   secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY"),
...>   region: System.fetch_env!("AWS_REGION"),
...>   database: "default",
...>   output_location: System.fetch_env!("AWS_ATHENA_OUTPUT_LOCATION")
...> ]
iex> query = "SELECT id, type, tags, members, timestamp, visible FROM planet WHERE id = 470454 and type = 'relation'"
iex> req = Req.new() |> ReqAthena.attach(opts)
iex> Req.post!(req, athena: query).body
%ReqAthena.Result{
  columns: ["id", "type", "tags", "members", "timestamp", "visible"],
  output_location: "s3://my-bucket/c594d5df-9879-4bf7-8796-780e0b87a673.csv",
  query_execution_id: "c594d5df-9879-4bf7-8796-780e0b87a673",
  rows: [
    [470454, "relation",
     "{ref=17229A, site=geodesic, name=Mérignac A, source=©IGN 2010 dans le cadre de la cartographie réglementaire, type=site, url=http://geodesie.ign.fr/fiches/index.php?module=e&action=fichepdf&source=carte&sit_no=17229A, network=NTF-5}",
     "[{type=node, ref=670007839, role=}, {type=node, ref=670007840, role=}]",
     ~N[2017-01-21 12:51:34.000], true]
  ],
  statement_name: nil
}

With parameterized query:

iex> opts = [
...>   access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"),
...>   secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY"),
...>   region: System.fetch_env!("AWS_REGION"),
...>   database: "default",
...>   output_location: System.fetch_env!("AWS_ATHENA_OUTPUT_LOCATION")
...> ]
iex> query = "SELECT id, type FROM planet WHERE id = ? and type = ?"
iex> req = Req.new() |> ReqAthena.attach(opts)
iex> Req.post!(req, athena: {query, [239_970_142, "node"]}).body
%ReqAthena.Result{
  columns: ["id", "type"],
  output_location: "s3://my-bucket/dda41d66-1eea-4588-850a-945c9def9163.csv",
  query_execution_id: "dda41d66-1eea-4588-850a-945c9def9163",
  rows: [[239_970_142, "node"]],
  statement_name: "query_C71EF77B8B7B92D9846C6D7E70136448"
}
Link to this function

fetch_option!(request, key)

View Source