/
load.go
174 lines (132 loc) · 2.78 KB
/
load.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package elastic
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"gopkg.in/olivere/elastic.v3"
)
// Load a file into elastic
func Load(host, filename string, index *string) {
log.Printf("Loading %s to %s\n", filename, host)
var err error
counter := 0
workerSize := 5
queue := make(chan []byte)
results := make(chan int)
for w := 0; w < workerSize; w++ {
go worker(host, index, queue, results)
}
file, err := os.Open(filename)
if err != nil {
panic(err)
}
// Combine the defers?
defer file.Close()
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("pkg: %v", r)
}
panic(r)
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
bytes := scanner.Bytes()
/*
bufio.Scanner uses one []byte slice to store tokens (lines)
which won't work when we're sending the slice (which
are pointers) to the workers and they attempt to UnMarshal
while scanner is overwriting the same slice.
*/
newToken := make([]byte, len(bytes))
copy(newToken, bytes)
queue <- newToken
counter++
}
log.Printf("Closing queue - sent %d items", counter)
close(queue)
for w := 0; w < workerSize; w++ {
<-results
}
}
func worker(host string, forceIndex *string, queue <-chan []byte, results chan<- int) {
// Create a client
client := newClient(host)
var err error
var body map[string]interface{}
var documentId string
var docType string
var msg interface{}
var ok bool
var index string
var m map[string]interface{}
if forceIndex != nil && *forceIndex == "" {
forceIndex = nil
} else {
index = *forceIndex
}
defer func() {
// recover panic and print out details of the last document
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("pkg: %v", r)
}
panic(r)
}
}()
count := 0
bulk := client.Bulk()
for rawJson := range queue {
err = json.Unmarshal(rawJson, &msg)
if err != nil {
panic(err)
}
m = msg.(map[string]interface{})
if forceIndex == nil {
index, ok = m["_index"].(string)
if ok == false {
panic("Could not assert _index to string")
}
}
docType, ok = m["_type"].(string)
if ok == false {
panic("Could not assert _type to string")
}
documentId, ok = m["_id"].(string)
if ok == false {
panic("Could not assert _id to string")
}
body, ok = m["_source"].(map[string]interface{})
if ok == false {
panic("Could not assert _source")
}
req := elastic.NewBulkIndexRequest().
Index(index).
Type(docType).
Id(documentId).
Doc(body)
bulk.Add(req)
count++
if count >= 1000 {
_, err = bulk.Do()
if err != nil {
panic(err)
}
count = 0
}
}
if count > 0 {
_, err = bulk.Do()
if err != nil {
panic(err)
}
}
results <- 1
}