athena

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2023 License: MIT Imports: 23 Imported by: 0

README

This is forked from segmentio/go-athena and we described what changes we added here.

go-athena

go-athena is a simple Golang database/sql driver for Amazon Athena.

import (
    "database/sql"
    _ "github.com/speee/go-athena"
)

func main() {
  db, _ := sql.Open("athena", "db=default&output_location=s3://results")
  rows, _ := db.Query("SELECT url, code from cloudfront")

  for rows.Next() {
    var url string
    var code int
    rows.Scan(&url, &code)
  }
}

It provides a higher-level, idiomatic wrapper over the AWS Go SDK, comparable to the Athena JDBC driver AWS provides for Java users.

For example,

Caveats

database/sql exposes lots of methods that aren't supported in Athena. For example, Athena doesn't support transactions so Begin() is irrelevant. If a method must be supplied to satisfy a standard library interface but is unsupported, the driver will panic indicating so. If there are new offerings in Athena and/or helpful additions, feel free to PR.

Result Mode

go-athena has the following modes to get the result of the query.

  • API (default)
  • DL
  • GZIP DL

Note

  • DL and GZIP DL Mode are used only in the Select statement.
    • Other statements automatically use API mode under DL or GZIP DL Mode.
  • Detailed explanation is described here.
  • Usages of Result Mode.

Prepared Statements

You can use Athena Prepared Statements. Click here for details on how to use.

Testing

Athena doesn't have a local version and revolves around S3 so our tests are integration tests against AWS itself. Thus, our tests require AWS credentials. The simplest way to provide them is via AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, but you can use anything supported by the Default Credential Provider Chain.

The tests support a few environment variables:

  • ATHENA_DATABASE can be used to override the default database "go_athena_tests"
  • S3_BUCKET can be used to override the default S3 bucket of "go-athena-tests"
  • ATHENA_REGION or AWS_DEFAULT_REGION can be used to override the default region of "us-east-1"
  • ATHENA_WORK_GROUP can be used to override the default workgroup of "primary"

Documentation

Index

Constants

View Source
const (
	// TimestampLayout is the Go time layout string for an Athena `timestamp`.
	TimestampLayout             = "2006-01-02 15:04:05.999"
	TimestampWithTimeZoneLayout = "2006-01-02 15:04:05.999 MST"
	DateLayout                  = "2006-01-02"
)
View Source
const (
	CATALOG_AWS_DATA_CATALOG string = "AwsDataCatalog"
)

Variables

View Source
var CatalogContextKey string = contextPrefix + catalogContextKey

CatalogContextKey context key of setting catalog

View Source
var ResultModeContextKey string = contextPrefix + resultModeContextKey

ResultModeContextKey context key of setting result mode

View Source
var TimeoutContextKey string = contextPrefix + timeoutContextKey

TimeoutContextKey context key of setting timeout

Functions

func Open

func Open(cfg Config) (*sql.DB, error)

Open is a more robust version of `db.Open`, as it accepts a raw aws.Session. This is useful if you have a complex AWS session since the driver doesn't currently attempt to serialize all options into a string.

func SetAPIMode

func SetAPIMode(ctx context.Context) context.Context

SetAPIMode set APIMode to ResultMode from context

func SetCatalog

func SetCatalog(ctx context.Context, catalog string) context.Context

SetCatalog set catalog from context

func SetDLMode

func SetDLMode(ctx context.Context) context.Context

SetDLMode set DownloadMode to ResultMode from context

func SetGzipDLMode

func SetGzipDLMode(ctx context.Context) context.Context

SetGzipDLMode set CTASMode to ResultMode from context

func SetTimeout

func SetTimeout(ctx context.Context, timeout uint) context.Context

SetTimeout set timeout from context

Types

type Config

type Config struct {
	Session        *session.Session
	Database       string
	OutputLocation string
	WorkGroup      string

	PollFrequency time.Duration

	ResultMode ResultMode
	Timeout    uint
	Catalog    string
}

Config is the input to Open().

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver is a sql.Driver. It's intended for db/sql.Open().

func NewDriver

func NewDriver(cfg *Config) *Driver

NewDriver allows you to register your own driver with `sql.Register`. It's useful for more complex use cases. Read more in PR #3. https://github.com/segmentio/go-athena/pull/3

Generally, sql.Open() or athena.Open() should suffice.

func (*Driver) Open

func (d *Driver) Open(connStr string) (driver.Conn, error)

Open should be used via `db/sql.Open("athena", "<params>")`. The following parameters are supported in URI query format (k=v&k2=v2&...)

- `db` (required) This is the Athena database name. In the UI, this defaults to "default", but the driver requires it regardless.

- `output_location` (required) This is the S3 location Athena will dump query results in the format "s3://bucket/and/so/forth". In the AWS UI, this defaults to "s3://aws-athena-query-results-<ACCOUNTID>-<REGION>", but the driver requires it.

- `poll_frequency` (optional) Athena's API requires polling to retrieve query results. This is the frequency at which the driver will poll for results. It should be a time/Duration.String(). A completely arbitrary default of "5s" was chosen.

- `region` (optional) Override AWS region. Useful if it is not set with environment variable.

- `workgroup` (optional) Athena's workgroup. This defaults to "primary".

Credentials must be accessible via the SDK's Default Credential Provider Chain. For more advanced AWS credentials/session/config management, please supply a custom AWS session directly via `athena.Open()`.

type ResultMode

type ResultMode int

ResultMode Results mode

const (
	// ResultModeAPI api access Mode
	ResultModeAPI ResultMode = 0

	// ResultModeDL download results Mode
	ResultModeDL ResultMode = 1

	// ResultModeGzipDL ctas query and download gzip file Mode
	ResultModeGzipDL ResultMode = 2
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL