
[![CodeCov][cov-img]][cov] [![GoDoc][doc-img]][doc] [![Github release][release-img]][release] [![Go Report Card][report-card-img]][report-card] [![lic][license-img]][license] [![made][made-img]][made]
:package: athenadriver - A fully-featured AWS Athena database driver for Go
:shell: athenareader - A moneywise command line utililty to query athena in command line.
(This project is a sandbox project and the development status is STABLE.)
athenadriver is a fully-featured AWS Athena database driver for Go developed at Uber Technologies Inc.
It provides a hassle-free way of querying AWS Athena database with Go standard
library. It not only provides basic features of Athena Go SDK, but
addresses some SDK's limitation, improves and extends it. Moreover, it also includes
advanced features like Athena workgroup and tagging creation, driver read-only mode and so on.
The PDF version of AthenaDriver document is available at :scroll:
Except the basic features provided by Go database/sql like error handling, database pool and reconnection, athenadriver supports the following features out of box:
INSERT INTO...VALUESDB.Exec() and db.ExecContext() support :link:get_driver_version, get_query_id, get_query_id_status, stop_query_id, get_workgroup, list_workgroups, update_workgroup, get_cost, get_execution_report etc :link:athenadriver can extremely simplify your code. Check athenareader out as an example and a convenient tool for your Athena query in command line.
athenadriverTo be able to query AWS Athena, you need to have an AWS account at Amazon AWS's website. To
give it a shot, a free
tier account is enough. You also need to have a pair of AWS access key ID and secret access key.
You can get it from AWS Security Credentials section of Identity and Access Management (IAM).
If you don't have one, please create it. The following is a screenshot from my temporary free tier account:

In addition to AWS credentials, you also need an s3 bucket to store query result. Just go to
AWS S3 web console page to create one.
In the examples below, the s3 bucket I use is s3://myqueryresults/.
In most cases, you need the following 4 prerequisites:
access key IDsecret access keyFor more details on athenadriver's support on AWS credentials & S3 query result bucket, please refer to section
Support Multiple AWS Authorization Methods.
Before Go 1.17, go get can be used to install athenadriver:
go get -u github.com/uber/athenadriver
Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.
go install github.com/uber/athenadriver@latest
We provide unit tests and integration tests in the codebase.
All the unit tests are self-contained and passed even in no-internet environment. Test coverage is 100%.
$ cd $GOPATH/src/github.com/uber/athenadriver/go ✔ /opt/share/go/path/src/github.com/uber/athenadriver [uber|✚ 1…12] 21:35 $ go test -coverprofile=coverage.out github.com/uber/athenadriver/go && \ go tool cover -func=coverage.out |grep -v 100.0% ok github.com/uber/athenadriver/go 9.255s coverage: 100.0% of statements
All integration tests are under examples folder.
Please make sure all prerequisites are met so that you can run the code on your own machine.
All the code snippets in examples folder are fully tested in our machines. For example,
to run some stress and crash test, you can use examples/perf/concurrency.go. Build it first:
$cd $GOPATH/src/github.com/uber/athenadriver $go build examples/perf/concurrency.go
Run it, wait for some output but not all, and unplug your network cable:
$./concurrency > examples/perf/concurrency.output.`date +"%Y-%m-%d-%H-%M-%S"`.log 58,13,53,54,78,96,32,48,40,11,35,31,65,61,1,73,74,22,34,49,80,5,69,37,0,79, 2020/02/09 13:49:29 error [38]RequestError: send request failed caused by: Post https://athena.us-east-1.amazonaws.com/: dial tcp: lookup athena.us-east-1.amazonaws.com: no such host ... 2020/02/09 13:49:29 error [89]RequestError: send request failed caused by: Post https://athena.us-east-1.amazonaws.com/: dial tcp: lookup athena.us-east-1.amazonaws.com: no such host
You can see RequestError is thrown out from the code. The active Athena queries failed because the network is down.
Now re-plugin your cable and wait for network coming back, you can see the program automatically reconnects to Athena, and resumes to output data correctly:
72,25,92,98,15,93,41,7,8,90,81,56,66,2,18,84,87,63, 44,45,82,99,86,3,52,76,71,16,39,67,23,12,42,17,4,
athenadriverathenadriver is very easy to use. What you need to do it to import it in your code and then use the standard Go database/sql as usual.
import athenadriver "github.com/uber/athenadriver/go"
The following are coding examples to demonstrate athenadriver's features and how you should use athenadriver in your Go application.
Please be noted the code is for demonstration purpose only, so please follow your own coding style or best practice if necessary.
The following is the simplest example for demonstration purpose. The source code is available at dml_select_simple.go.
package main import ( "database/sql" drv "github.com/uber/athenadriver/go" ) func main() { // Step 1. Set AWS Credential in Driver Config. conf, _ := drv.NewDefaultConfig("s3://myqueryresults/", "us-east-2", "DummyAccessID", "DummySecretAccessKey") // Step 2. Open Connection. db, _ := sql.Open(drv.DriverName, conf.Stringify()) // Step 3. Query and print results var url string _ = db.QueryRow("SELECT url from sampledb.elb_logs limit 1").Scan(&url) println(url) }
To make it work for you, please replace OutputBucket, Region, AccessID and
SecretAccessKey with your own values. sampledb is provided by Amazon so you don't have to worry about it.
To Build it:
$ go build examples/query/dml_select_simple.go
Run it and you can see output like:
$ ./dml_select_simple https://www.example.com/articles/553
athenadriver uses access keys(Access Key ID and Secret Access Key) to sign programmatic requests to AWS.
When if the AWS_SDK_LOAD_CONFIG environment variable was set, athenadriver uses Shared Config, respects AWS CLI Configuration and Credential File Settings and gives it even higher priority over the values set in athenadriver.Config.
When environment variable AWS_SDK_LOAD_CONFIG is set, it will read aws_access_key_id(AccessID) and aws_secret_access_key(SecretAccessKey)
from ~/.aws/credentials, region from ~/.aws/config. For details about ~/.aws/credentials and ~/.aws/config, please check here.
But you still need to specify correct OutputBucket in athenadriver.Config because it is not in the AWS client config.
OutputBucket is critical in Athena. Even if you have a default value set in Athena web console, you must pass one programmatically or you will get error:
No output location provided. An output location is required either through the Workgroup result configuration setting or as an API input.
The sample code below enforces AWS_SDK_LOAD_CONFIG is set, so athenadriver's AWS Session will be created from the configuration values from the shared config (~/.aws/config) and shared credentials (~/.aws/credentials) files.
Even if we pass all dummy values as parameters in NewDefaultConfig() except OutputBucket, they are overridden by
the values in AWS CLI config files, so it doesn't really matter.
// To use AWS CLI's Config for authentication func useAWSCLIConfigForAuth() { os.Setenv("AWS_SDK_LOAD_CONFIG", "1") // 1. Set AWS Credential in Driver Config. conf, err := drv.NewDefaultConfig(secret.OutputBucketProd, drv.DummyRegion, drv.DummyAccessID, drv.DummySecretAccessKey) if err != nil { return } // 2. Open Connection. db, _ := sql.Open(drv.DriverName, conf.Stringify()) // 3. Query and print results var i int _ = db.QueryRow("SELECT 456").Scan(&i) println("with AWS CLI Config:", i) os.Unsetenv("AWS_SDK_LOAD_CONFIG") }
If your AWS CLI setting is valid like mine, this function should output:
with AWS CLI Config: 456
The above authentication method also works for querying Athena in AWS Lambda. In lambda, you don't have to provide access ID, key and region, and you don't need AWS CLI config files either. You just need to specify the correct output bucket. Please check the AWS Lambda Go same code here.
athenadriver Config For AuthenticationWhen environment variable AWS_SDK_LOAD_CONFIG is NOT set, you may explicitly define credentials by passing valid (NOT dummy) accessID, secretAccessKey, region, and outputBucket into athenadriver.NewDefaultConfig().
The sample code below ensure AWS_SDK_LOAD_CONFIG is not set, then pass four valid parameters into NewDefaultConfig():
// To use athenadriver's Config for authentication func useAthenaDriverConfigForAuth() { os.Unsetenv("AWS_SDK_LOAD_CONFIG") // 1. Set AWS Credential in Driver Config. conf, err := drv.NewDefaultConfig(secret.OutputBucketDev, secret.Region, secret.AccessID, secret.SecretAccessKey) if err != nil { return } // 2. Open Connection. db, _ := sql.Open(drv.DriverName, conf.Stringify()) // 3. Query and print results var i int _ = db.QueryRow("SELECT 123").Scan(&i) println("with AthenaDriver Config:", i) }
The sample output:
with AthenaDriver Config: 123
The full code is here at examples/auth.go.
If environment variable AWS_SDK_LOAD_CONFIG is NOT set and credentials are not supplied in the athenadriver configuration, the AWS SDK will look up credentials using its default methodology described here: https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials.
Region and OutputBucket bucket still need to be explictly defined.
The sample code below ensures AWS_SDK_LOAD_CONFIG is not set, then creates a athenadriver config with OutputBucket and Region values set.
// To use AWS SDK Default Credentials func useAthenaDriverConfigForAuth() { os.Unsetenv("AWS_SDK_LOAD_CONFIG") // 1. Set OutputBucket and Region in Driver Config. conf := drv.NewNoOpsConfig() conf.SetOutputBucket(secret.OutputBucketDev) conf.SetRegion(secret.Region) // 2. Open Connection. db, _ := sql.Open(drv.DriverName, conf.Stringify()) // 3. Query and print results var i int _ = db.QueryRow("SELECT 123").Scan(&i) println("with AthenaDriver Config:", i) }
The sample output:
with AthenaDriver Config: 123
As we said, athenadriver supports all Athena data types.
In the following sample code, we use an SQL statement to SELECT som simple data of all the advanced types and then print them out.
package main import ( "context" "database/sql" drv "github.com/uber/athenadriver/go" ) func main() { // 1. Set AWS Credential in Driver Config. conf, err := drv.NewDefaultConfig("s3://myqueryresults/", "us-east-2", "DummyAccessID", "DummySecretAccessKey") if err != nil { panic(err) } // 2. Open Connection. dsn := conf.Stringify() db, _ := sql.Open(drv.DriverName, dsn) // 3. Query and print results query := "SELECT JSON '\"Hello Athena\"', " + "ST_POINT(-74.006801, 40.70522), " + "ROW(1, 2.0), INTERVAL '2' DAY, " + "INTERVAL '3' MONTH, " + "TIME '01:02:03.456', " + "TIME '01:02:03.456 America/Los_Angeles', " + "TIMESTAMP '2001-08-22 03:04:05.321 America/Los_Angeles';" rows, err := db.Query(query) if err != nil { panic(err) } defer rows.Close() println(drv.ColsRowsToCSV(rows)) }
Sample output:
"Hello Athena",00 00 00 00 01 01 00 00 00 20 25 76 6d 6f 80


全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化 、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。


零代码AI应用开发平台
零代码AI应用开发平台,用户只需一句话简 单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号