-
Notifications
You must be signed in to change notification settings - Fork 0
/
glue_client.go
95 lines (77 loc) · 1.78 KB
/
glue_client.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//go:generate mockgen -source=glue_client.go -package=main -destination=mock_glue_client_for_test.go
package main
import (
"database/sql"
"time"
athena "github.com/segmentio/go-athena"
log "github.com/sirupsen/logrus"
)
type glueClient interface {
runQuery(query string) (map[string]interface{}, error)
getDBName() string
getTenant() string
getDB() *sql.DB
}
type glueClientImpl struct {
db *sql.DB
dbName string
tenant string
}
func (gc *glueClientImpl) getDB() *sql.DB {
return gc.db
}
func (gc *glueClientImpl) getDBName() string {
return gc.dbName
}
func (gc *glueClientImpl) getTenant() string {
return gc.tenant
}
func (gc *glueClientImpl) runQuery(query string) (map[string]interface{}, error) {
log.WithField("query", query).Debug("Running query")
rows, err := gc.db.Query(query)
if err != nil {
return nil, err
}
cols, _ := rows.Columns()
m := map[string]interface{}{}
for rows.Next() {
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
columnPointers[i] = &columns[i]
}
if err := rows.Scan(columnPointers...); err != nil {
return m, err
}
for i, colName := range cols {
val := columnPointers[i].(*interface{})
m[colName] = *val
}
}
return m, nil
}
func mustNewGlueClient(
accessKeyID,
secretAccessKey,
regionID,
outputLocation,
database,
tenant string,
) glueClient {
awsSession := mustNewAWSSession(accessKeyID, secretAccessKey, regionID)
cfg := athena.Config{
Session: awsSession,
Database: database,
OutputLocation: outputLocation,
PollFrequency: time.Second * 5,
}
db, err := athena.Open(cfg)
if err != nil {
log.WithError(err).Fatal("Failed to open connection to athena")
}
return &glueClientImpl{
db,
database,
tenant,
}
}