-
-
Save kartiksura/93160be1078648a14ec0ddc125c35546 to your computer and use it in GitHub Desktop.
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) | |
} | |
} |
This is a handy example, thank you!
Agreed, documentation is very confusing. Thanks for posting this.
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
@pwmcintyre: There's also https://github.com/segmentio/go-athena, which tries to simplify precisely that in Go.
Thanks - incredibly useful! For others QueryExecution.Status.StateChangeReason
is useful to get the FAILED
state reason.
@pwmcintyre @kartiksura: where you able to get the parsing working?
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
Thanks. Let me try this :)
This worked. Thanks a bunch
@kartiksura ... does this snippet (and aws-go-sdk) support pagination of Athena results implicitly?
Thanks I'm seeking for the same.
I would rather read the results from s3 instead of Athena get result API which is not easy to go.
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.
While the sdk is excellent, the documentation for Athena was quite confusing. One way to figure out things is to follow the Java examples.