-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
151 lines (124 loc) · 3.13 KB
/
data.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package data
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
"cloud.google.com/go/spanner"
"golang.org/x/net/context"
"google.golang.org/api/option"
)
type BatchMutation struct {
Mutation []*spanner.Mutation
}
func MapToBatchMutation(objects *[]map[string]interface{}) *[]BatchMutation {
table := os.Getenv("SPANNER_SKYLINE_TABLE")
batchMutations := []BatchMutation{}
batchMutation := BatchMutation{}
for index, element := range *objects {
batchMutation.Mutation = append(batchMutation.Mutation, spanner.InsertOrUpdateMap(table, element))
if (len(batchMutation.Mutation) == 1000) || (index == len((*objects))-1) {
batchMutations = append(batchMutations, batchMutation)
batchMutation = BatchMutation{}
}
}
if len(batchMutations) == 0 && len(batchMutation.Mutation) > 0 {
batchMutations = append(batchMutations, batchMutation)
}
return &batchMutations
}
func JSONToMap(jsonData []byte) *map[string]interface{} {
object := map[string]interface{}{}
err := json.Unmarshal(jsonData, &object)
if err != nil {
return nil
}
return &object
}
func Write(mutations []*spanner.Mutation, credentials []byte) bool {
project := os.Getenv("SPANNER_SKYLINE_PROJECT")
instance := os.Getenv("SPANNER_SKYLINE_INSTANCE")
database := os.Getenv("SPANNER_SKYLINE_DATABASE")
ctx := context.Background()
db := "projects/" + project + "/instances/" + instance + "/databases/" + database
client, err := spanner.NewClient(ctx, db, option.WithCredentialsJSON(credentials))
if err != nil {
panic(err)
}
defer client.Close()
_, err = client.Apply(ctx, mutations)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
return false
}
return true
}
func worker(credentials []byte, job chan []*spanner.Mutation, done chan bool, wg *sync.WaitGroup) {
for {
select {
case k := <-job:
result := Write(k, credentials)
wg.Done()
done <- result
}
}
}
func SendToSpanner(objects *[]map[string]interface{}) {
credentialsPath := os.Getenv("SPANNER_SKYLINE_CREDENTIALS_PATH")
credentials, err := ioutil.ReadFile(credentialsPath)
if err != nil {
panic(err)
}
batchs := MapToBatchMutation(objects)
batchSize := len(*batchs)
job := make(chan []*spanner.Mutation)
done := make(chan bool)
var wg sync.WaitGroup
fmt.Printf("START: Batchs: %v\n", batchSize)
count := 0
for i := 0; i < 150; i++ {
go worker(credentials, job, done, &wg)
if count <= len((*batchs))-1 {
wg.Add(1)
job <- (*batchs)[count].Mutation
count++
}
}
run := true
for run == true {
select {
case <-done:
count++
if count <= batchSize-1 {
wg.Add(1)
job <- (*batchs)[count].Mutation
} else {
run = false
break
}
}
}
wg.Wait()
fmt.Printf("FINISH: Batchs: %v\n", batchSize)
}
func ReadByFile(path string) (*[]map[string]interface{}, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
objects := []map[string]interface{}{}
for scanner.Scan() {
object := JSONToMap(scanner.Bytes())
if object != nil {
objects = append(objects, *object)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &objects, nil
}