forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbulk-insert.go
49 lines (39 loc) · 1.04 KB
/
bulk-insert.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
package main
import (
"fmt"
"strconv"
"github.com/couchbase/gocb"
)
// bucket reference - reuse as bucket reference in the application
var bucket *gocb.Bucket
func main() {
// Connect to Cluster
cluster, err := gocb.Connect("couchbase://127.0.0.1")
if err != nil {
fmt.Println("ERROR CONNECTING TO CLUSTER:", err)
}
// Open Bucket
bucket, err = cluster.OpenBucket("travel-sample", "")
if err != nil {
fmt.Println("ERROR OPENING BUCKET:", err)
}
// Create a JSON document
type Doc struct {
Item string `json:"item"`
}
key := "goDevguideExampleBulkInsert"
val := Doc{"A bulk insert test value"}
// Create an Array of BulkOps for Insert
var items []gocb.BulkOp
// Add 10 items to the array that will be performed as a bulk operation
for i := 0; i < 10; i++ {
items = append(items, &gocb.InsertOp{Key: key + "_" + strconv.Itoa(i), Value: &val})
}
// Perform the bulk operation
err = bucket.Do(items)
if err != nil {
fmt.Println("ERROR PERFORMING BULK INSERT:", err)
}
// Exiting
fmt.Println("Example Successful - Exiting")
}