forked from uber/athenadriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
77 lines (72 loc) · 2.74 KB
/
types.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"context"
"database/sql"
secret "github.com/uber/athenadriver/examples/constants"
drv "github.com/uber/athenadriver/go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
// main will query Athena and print all columns and rows information in csv format
func main() {
// 1. Set AWS Credential in Driver Config.
conf, err := drv.NewDefaultConfig(secret.OutputBucket, secret.Region,
secret.AccessID, secret.SecretAccessKey)
if err != nil {
panic(err)
}
// 2. Open Connection.
dsn := conf.Stringify()
db, _ := sql.Open(drv.DriverName, dsn)
// 3. Query and print results
query := "select JSON '\"Hello Athena\"', " +
"ST_POINT(-74.006801, 40.70522), " +
"ROW(1, 2.0), INTERVAL '2' DAY, " +
"INTERVAL '3' MONTH, " +
"TIME '01:02:03.456', " +
"TIME '01:02:03.456 America/Los_Angeles', " +
"TIMESTAMP '2001-08-22 03:04:05.321 America/Los_Angeles';"
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = ""
atom := zap.NewAtomicLevel()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atom,
))
atom.SetLevel(drv.DebugLevel)
ctx := context.WithValue(context.Background(), drv.LoggerKey, logger)
rows, err := db.QueryContext(ctx, query)
if err != nil {
panic(err)
}
defer rows.Close()
println(drv.ColsRowsToCSV(rows))
}
/*
select TIMESTAMP '2001-08-22 03:04:05.321 PDT';
SYNTAX_ERROR: line 1:145: '2001-08-22 03:04:05.321 PDT' is not a valid timestamp literal
Sample output:
_col0,_col1,_col2,_col3,_col4,_col5
2 00:00:00.000,0-3,0000-01-01T01:02:03.456-07:52,0000-01-01T01:02:03.456-07:52,2001-08-22T03:04:05.321-07:00,2001-08-22T03:04:05.321-07:00
*/