-
Notifications
You must be signed in to change notification settings - Fork 0
/
athena.go
81 lines (67 loc) · 1.97 KB
/
athena.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/m-mizutani/minerva/internal/service"
"github.com/m-mizutani/minerva/pkg/models"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func createPartition(region, athenaDB string, p *models.PartitionQueue, meta *service.MetaService, output string) error {
ssn := session.Must(session.NewSession(&aws.Config{Region: aws.String(region)}))
pkey := p.Location
if has, err := meta.HeadPartition(pkey); err != nil {
return err
} else if has {
return nil // Nothing to do
}
var keys []string
for k, v := range p.Keys {
keys = append(keys, fmt.Sprintf("%s='%s'", k, v))
}
sql := fmt.Sprintf("ALTER TABLE %s.%s ADD IF NOT EXISTS PARTITION (%s) LOCATION '%s'",
athenaDB, p.TableName, strings.Join(keys, ", "), pkey)
athenaClient := athena.New(ssn)
input := &athena.StartQueryExecutionInput{
QueryString: aws.String(sql),
ResultConfiguration: &athena.ResultConfiguration{
OutputLocation: &output,
},
}
logger.WithField("input", input).Info("Athena Query")
output1, err := athenaClient.StartQueryExecution(input)
logger.WithFields(logrus.Fields{
"err": err,
"input": input,
"output": output1,
}).Debug("done")
if err != nil {
return errors.Wrap(err, "Fail to execute a partitioning query")
}
if os.Getenv("CHECK_QUERY_RESULT") != "" {
for {
output2, err := athenaClient.GetQueryExecution(&athena.GetQueryExecutionInput{
QueryExecutionId: output1.QueryExecutionId,
})
if err != nil {
return errors.Wrap(err, "Fail to get an execution result")
}
if *output2.QueryExecution.Status.State == "RUNNING" {
logger.WithField("output", output2).Debug("Waiting...")
time.Sleep(time.Second * 3)
continue
}
logger.WithField("output", output2).Debug("done")
break
}
}
if err := meta.PutPartition(pkey); err != nil {
return err
}
return nil
}