Skip to content

Instantly share code, notes, and snippets.

@kartiksura
Last active June 7, 2023 16:38
Show Gist options
  • Select an option

  • Save kartiksura/93160be1078648a14ec0ddc125c35546 to your computer and use it in GitHub Desktop.

Select an option

Save kartiksura/93160be1078648a14ec0ddc125c35546 to your computer and use it in GitHub Desktop.
Querying AWS Athena using Golang SDK
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/athena"
)
func main() {
awscfg := &aws.Config{}
awscfg.WithRegion("us-east-1")
// Create the session that the service will use.
sess := session.Must(session.NewSession(awscfg))
svc := athena.New(sess, aws.NewConfig().WithRegion("us-east-1"))
var s athena.StartQueryExecutionInput
s.SetQueryString("select PageURL from testtable limit 10")
var q athena.QueryExecutionContext
q.SetDatabase("testdb")
s.SetQueryExecutionContext(&q)
var r athena.ResultConfiguration
r.SetOutputLocation("s3://TestBucket")
s.SetResultConfiguration(&r)
result, err := svc.StartQueryExecution(&s)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("StartQueryExecution result:")
fmt.Println(result.GoString())
var qri athena.GetQueryExecutionInput
qri.SetQueryExecutionId(*result.QueryExecutionId)
var qrop *athena.GetQueryExecutionOutput
duration := time.Duration(2) * time.Second // Pause for 2 seconds
for {
qrop, err = svc.GetQueryExecution(&qri)
if err != nil {
fmt.Println(err)
return
}
if *qrop.QueryExecution.Status.State != "RUNNING" {
break
}
fmt.Println("waiting.")
time.Sleep(duration)
}
if *qrop.QueryExecution.Status.State == "SUCCEEDED" {
var ip athena.GetQueryResultsInput
ip.SetQueryExecutionId(*result.QueryExecutionId)
op, err := svc.GetQueryResults(&ip)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v", op)
} else {
fmt.Println(*qrop.QueryExecution.Status.State)
}
}
@kartiksura
Copy link
Copy Markdown
Author

While the sdk is excellent, the documentation for Athena was quite confusing. One way to figure out things is to follow the Java examples.

@matthewcummings
Copy link
Copy Markdown

This is a handy example, thank you!

@tyanko1
Copy link
Copy Markdown

tyanko1 commented Jul 5, 2018

Agreed, documentation is very confusing. Thanks for posting this.

@pwmcintyre
Copy link
Copy Markdown

pwmcintyre commented Mar 7, 2019

this is a great start, but has anyone any tips for parsing the results? ie. parsing the response VarCharValue into the proper value type?

edit: there's a java example on the official docs, seems simple enough to implement
https://docs.aws.amazon.com/athena/latest/ug/code-samples.html

@brainstorm
Copy link
Copy Markdown

@pwmcintyre: There's also https://github.com/segmentio/go-athena, which tries to simplify precisely that in Go.

@amwill04
Copy link
Copy Markdown

Thanks - incredibly useful! For others QueryExecution.Status.StateChangeReason is useful to get the FAILED state reason.

@Krithika3
Copy link
Copy Markdown

Krithika3 commented Oct 4, 2019

@pwmcintyre @kartiksura: where you able to get the parsing working?

@kartiksura
Copy link
Copy Markdown
Author

kartiksura commented Oct 4, 2019 via email

@Krithika3
Copy link
Copy Markdown

Krithika3 commented Oct 4, 2019

Thanks a lot. I was just planning to parse the returned output into a properly formatted Json which I can send downstream to one of our processes. I was trying to look at some of the Java examples but it’s not very descriptive

@kartiksura
Copy link
Copy Markdown
Author

kartiksura commented Oct 4, 2019 via email

@Krithika3
Copy link
Copy Markdown

Thanks. Let me try this :)

@Krithika3
Copy link
Copy Markdown

This worked. Thanks a bunch

@brainstorm
Copy link
Copy Markdown

@kartiksura ... does this snippet (and aws-go-sdk) support pagination of Athena results implicitly?

@kartiksura
Copy link
Copy Markdown
Author

kartiksura commented Oct 15, 2019 via email

@gauravds
Copy link
Copy Markdown

Thanks I'm seeking for the same.

@sananguliyev
Copy link
Copy Markdown

I would rather read the results from s3 instead of Athena get result API which is not easy to go.

@kevinisbest
Copy link
Copy Markdown

Hi @kartiksura,
Thanks for the useful example.
However, allow me to remind you there is another state that needs to wait: QUEUED, it will be perfect if add this condition inside the for loop.

@kartiksura
Copy link
Copy Markdown
Author

kartiksura commented Aug 21, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment