-
-
Save nirdosh17/60c03f9f5c133ce8868227a4e4ac9f63 to your computer and use it in GitHub Desktop.
Querying AWS Athena using Golang SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment