-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
280 lines (222 loc) · 5.85 KB
/
main.go
File metadata and controls
280 lines (222 loc) · 5.85 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
_ "github.com/lib/pq"
"gopkg.in/olivere/elastic.v2"
"io/ioutil"
"log"
"os"
"strconv"
"sync"
"sync/atomic"
)
// Options
type options struct {
URI string `json:"url"`
MaxBulkActions int `json:"max_bulk_actions"`
MaxFetchRows int `json:"max_fetch_rows"`
Timeout string `json:"timeout"`
DB struct {
Host string `json:"host"`
Port int `json:"port"`
Database string `json:"database"`
User string `json:"user"`
Password string `json:"password"`
Table string `json:"table"`
} `json:"db"`
Index string `json:"index"`
Type string `json:"type"`
Mappings []json.RawMessage `json:"mappings"`
}
// Create buffered channel to send inserts through
var indexQ chan string
var status = make(chan int)
// Global counters
var succeded, failed uint64
func sendBulkService(bulkService *elastic.BulkService) {
trying := bulkService.NumberOfActions()
if bulkResponse, err := bulkService.Do(); err != nil {
atomic.AddUint64(&failed, uint64(trying))
} else {
atomic.AddUint64(&succeded, uint64(len(bulkResponse.Succeeded())))
atomic.AddUint64(&failed, uint64(len(bulkResponse.Failed())))
}
status <- 1
}
// Index worker function to insert docs
func index(wg *sync.WaitGroup, opts options) {
// Connect client
client, err := elastic.NewClient(elastic.SetURL(opts.URI), elastic.SetSniff(false))
if err != nil {
log.Fatalln(err.Error())
}
// Create new bulk service request
bulkService := elastic.NewBulkService(client).Index(opts.Index).Type(opts.Type).Timeout(opts.Timeout)
for doc := range indexQ {
//Add index to request
bIndex := elastic.NewBulkIndexRequest().Index(opts.Index).OpType("create").Doc(doc)
bulkService.Add(bIndex)
// Send request after MaxBulkActions limit is reached
if bulkService.NumberOfActions() > opts.MaxBulkActions-1 {
sendBulkService(bulkService)
}
}
// Send last indexes
if bulkService.NumberOfActions() > 0 {
sendBulkService(bulkService)
}
wg.Done()
}
func empty(str string) bool {
return len(str) == 0
}
// Make sure all required options are passed
func check(opts options) error {
if empty(opts.Index) {
return errors.New("No elastic index found in options.")
}
if empty(opts.Type) {
return errors.New("No elastic type found in options.")
}
if empty(opts.URI) {
return errors.New("No elastic url found in options.")
}
if empty(opts.DB.Host) {
return errors.New("No postgres host found in options.")
}
if opts.DB.Port == 0 {
return errors.New("No postgres dataportbase found in options.")
}
if empty(opts.DB.User) {
return errors.New("No postgres user found in options.")
}
if empty(opts.DB.Database) {
return errors.New("No postgres database found in options.")
}
if empty(opts.DB.Password) {
return errors.New("No postgres password found in options.")
}
if empty(opts.DB.Table) {
return errors.New("No table found in options.")
}
return nil
}
func print() {
s := atomic.LoadUint64(&succeded)
f := atomic.LoadUint64(&failed)
fmt.Printf("\rSucceded: %d Failed: %d", s, f)
}
func setup(opts options) {
// Connect client
client, err := elastic.NewClient(elastic.SetURL(opts.URI), elastic.SetSniff(false))
if err != nil {
log.Fatalln(err.Error())
}
// Create index if does not exist
fmt.Print("Checking is index exists...")
exs := client.IndexExists(opts.Index)
if ok, err := exs.Do(); !ok {
if err != nil {
log.Fatalln(err.Error())
}
fmt.Print("no. Creating...")
newIndex := elastic.NewIndexService(client).Index(opts.Index)
_, err := newIndex.Do()
if err != nil {
log.Fatalln(err.Error())
}
fmt.Println("done.")
} else {
fmt.Println("yes")
}
//Create mappings
fmt.Print("Putting mappings...")
for _, mapping := range opts.Mappings {
// Send raw json from options
_, err := client.PutMapping().Index(opts.Index).Type(opts.Type).BodyString(string(mapping)).Do()
if err != nil {
log.Fatalln(err.Error())
}
}
fmt.Println("done.")
client.Stop()
fmt.Println("\nSetup finished.")
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage: postgres2elasticsearch <config.json> [number of workers]")
}
workers := 1
if len(os.Args) >= 3 {
var err error
workers, err = strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalln(err.Error())
}
}
//Load Input File
file, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatalln(err.Error())
}
// Load options
var opts options
if err := json.Unmarshal(file, &opts); err != nil {
log.Fatalln(err.Error())
}
// Check options for required
if err := check(opts); err != nil {
log.Fatalln(err.Error())
}
if opts.MaxBulkActions == 0 {
opts.MaxBulkActions = 1000 // Default to 1000 insert actions at one time
}
limit := "ALL"
if opts.MaxFetchRows > 0 {
limit = strconv.Itoa(opts.MaxFetchRows)
}
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d dbname=%s user=%s sslmode=disable password=%s", opts.DB.Host, opts.DB.Port, opts.DB.Database, opts.DB.User, opts.DB.Password))
if err != nil {
log.Fatalln(err.Error())
}
// Setup index
setup(opts)
wg := new(sync.WaitGroup)
indexQ = make(chan string, opts.MaxBulkActions*workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go index(wg, opts)
}
go func() {
for {
<-status
print()
}
}()
// Print start of progress
status <- 1
//Postgres go library doesn't allow dynamic table placeholders
statement := fmt.Sprintf("SELECT row_to_json(t) FROM %s as t LIMIT %s", opts.DB.Table, limit)
rows, err := db.Query(statement)
if err != nil {
log.Fatalln(err.Error())
}
defer rows.Close()
for rows.Next() {
var doc string
if err := rows.Scan(&doc); err != nil {
fmt.Println(err)
}
indexQ <- doc
}
if err := rows.Err(); err != nil {
log.Fatalln(err.Error())
}
close(indexQ)
wg.Wait()
print() // Print last update
fmt.Println("\n\nFinished")
}