Last active
June 7, 2023 16:38
-
-
Save kartiksura/93160be1078648a14ec0ddc125c35546 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) | |
} | |
} |
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.
Thanks for the tip Kevin!
On Fri, Aug 21, 2020 at 1:26 PM Kevin Hsiao ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi @kartiksura <https://github.com/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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/93160be1078648a14ec0ddc125c35546#gistcomment-3425935>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABBPYWLQWBFTOVMOZ33TQTLSBYSCZANCNFSM4HLSEDNQ>
.
--
Sent from Gmail Mobile
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks I'm seeking for the same.