This repository has been archived by the owner on Sep 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bulk.go
134 lines (111 loc) · 2.77 KB
/
bulk.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
package main
import (
"bufio"
"fmt"
"log"
"io"
_ "net/http"
_ "net/http/httputil"
"os"
"strings"
)
var cmdBulk = &Command{
Run: runBulk,
Usage: "bulk [-v] <index>",
Short: "bulk import",
Long: `
The bulk API enables many index/delete operations in a single API call.
Use the -v/--verbose flag to print progress.
Notice that if the bulk data file specifies the index name,
the argument specified on the command line will have no effect.
Example:
$ es bulk -v twitter < twitter-data.json
`,
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/bulk.html",
}
func init() {
cmdBulk.Flag.BoolVar(&verbose, "v", false, "verbose")
}
func bulkReader(input chan string, done chan bool) {
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString(byte('\n'))
if err == io.EOF {
done <- true
return
}
if err != nil {
log.Fatal("error reading from input\n")
}
input <- strings.TrimRight(line, "\n")
}
}
func bulkCommitter(index string, input chan string, done chan bool) {
// collect up to bulkSize lines before committing
numLines := 0
bulkSize := 1000
lines := make([]string, 0, bulkSize)
for {
select {
case line := <-input:
numLines++
lines = append(lines, line)
if numLines%bulkSize == 0 {
bulkCommit(index, lines, numLines)
lines = make([]string, 0, bulkSize)
}
case <-done:
if numLines > 0 {
bulkCommit(index, lines, numLines)
lines = make([]string, 0, bulkSize)
}
return
}
}
if verbose {
fmt.Fprintln(os.Stderr, "\n")
}
}
func bulkCommit(index string, lines []string, numLines int) {
if verbose {
fmt.Fprintf(os.Stderr, "Bulk importing %9d\r", numLines)
}
type itemsData struct {
Index string `json:"_index,omitempty"`
Type string `json:"_type,omitempty"`
Id string `json:"_id,omitempty"`
Version string `json:"_version,omitempty"`
Ok bool `json:"ok,omitempty"`
}
type items struct {
Create itemsData `json:"create,omitempty"`
Delete itemsData `json:"delete,omitempty"`
}
var response struct {
Took int `json:"took,omitempty"`
Items []items `json:"items,omitempty"`
}
bodyOfLines := strings.Join(lines, "\n") + "\n"
req := ESReq("POST", "/"+index+"/_bulk")
req.SetBodyString(bodyOfLines)
req.Do(&response)
// dump, _ := httputil.DumpRequest((*http.Request)(req), true)
// log.Print(string(dump))
//body := req.Do(&response)
//if len(response.Error) > 0 {
// log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
//}
//log.Println(body)
}
func runBulk(cmd *Command, args []string) {
if len(args) < 1 {
cmd.printUsage()
os.Exit(1)
}
index := args[0]
// create a channel that reads line by line from Stdin
done := make(chan bool)
input := make(chan string)
go bulkReader(input, done)
bulkCommitter(index, input, done)
}