-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
131 lines (110 loc) · 2.82 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package sample
import (
"cloud.google.com/go/bigquery"
"cloud.google.com/go/storage"
"fmt"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
"google.golang.org/appengine"
"google.golang.org/appengine/taskqueue"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"net/url"
)
type SqlConfig struct {
ProjectID string
DatasetID string
TableID string
Query string
}
func init() {
http.HandleFunc("/autosql/task", taskEnqueueHandler)
http.HandleFunc("/autosql/maketable", makeTableHandler)
}
func taskEnqueueHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
sc, err := getSqlConfig(ctx)
if err != nil {
log.Fatalf("Cannot get sql config: %v", err)
}
//taskqueueにpush
task := taskqueue.NewPOSTTask("/autosql/maketable", url.Values{
"project_id": {sc.ProjectID},
"dataset_id": {sc.DatasetID},
"table_id": {sc.TableID},
"query": {sc.Query},
})
taskqueue.Add(ctx, task, "default")
}
func getSqlConfig(ctx context.Context) (SqlConfig, error) {
bucketname := ""
if name, ok := os.LookupEnv("BUCKET_NAME"); ok {
bucketname = name
}
sqlpath := "sample/sample.sql"
confpath := "sample/sample.yaml"
confbuf, err := getFileGCS(ctx, bucketname, confpath)
if err != nil {
log.Fatalf("Cannot read object: %v", err)
}
var sc SqlConfig
if err := yaml.Unmarshal(confbuf, &sc); err != nil {
log.Fatalf("file is not in yaml format: %v", err)
}
log.Printf("yaml format: %v", sc)
querybuf, err := getFileGCS(ctx, bucketname, sqlpath)
if err != nil {
log.Fatalf("Cannot read object: %v", err)
}
sc.Query = string(querybuf)
return sc, err
}
func makeTableHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
project_id := r.FormValue("project_id")
dataset_id := r.FormValue("dataset_id")
table_id := r.FormValue("table_id")
query := r.FormValue("query")
// Creates a client
client, err := bigquery.NewClient(ctx, project_id)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ds := client.Dataset(dataset_id)
// query settings & run
q := client.Query(query)
q.Dst = ds.Table(table_id)
q.CreateDisposition = "CREATE_IF_NEEDED"
q.WriteDisposition = "WRITE_TRUNCATE"
job, err := q.Run(ctx)
if err != nil {
// TODO handle error
}
// Wait until async querying is done.
status, err := job.Wait(ctx)
if err != nil {
// TODO handle error
}
if err := status.Err(); err != nil {
// TODO handle error
}
it, err := job.Read(ctx)
}
func getFileGCS(ctx context.Context, bucketname string, filepath string) ([]byte, error) {
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
rc, err := client.Bucket(bucketname).Object(filepath).NewReader(ctx)
if err != nil {
return nil, err
}
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
log.Fatal(err)
}
return data, err
}