mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/couchbase/gocb/v2"
|
|
)
|
|
|
|
func main() {
|
|
// Uncomment following line to enable logging
|
|
// gocb.SetLogger(gocb.VerboseStdioLogger())
|
|
|
|
// Update this to your cluster details
|
|
connectionString := "cb.<your-endpoint>.cloud.couchbase.com"
|
|
bucketName := "travel-sample"
|
|
username := "username"
|
|
password := "Password!123"
|
|
|
|
options := gocb.ClusterOptions{
|
|
Authenticator: gocb.PasswordAuthenticator{
|
|
Username: username,
|
|
Password: password,
|
|
},
|
|
}
|
|
|
|
// Sets a pre-configured profile called "wan-development" to help avoid latency issues
|
|
// when accessing Capella from a different Wide Area Network
|
|
// or Availability Zone (e.g. your laptop).
|
|
if err := options.ApplyProfile(gocb.ClusterConfigProfileWanDevelopment); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Initialize the Connection
|
|
cluster, err := gocb.Connect("couchbases://"+connectionString, options)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
bucket := cluster.Bucket(bucketName)
|
|
|
|
err = bucket.WaitUntilReady(5*time.Second, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Get a reference to the default collection, required for older Couchbase server versions
|
|
// col := bucket.DefaultCollection()
|
|
|
|
col := bucket.Scope("tenant_agent_00").Collection("users")
|
|
|
|
// Create and store a Document
|
|
type User struct {
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
Interests []string `json:"interests"`
|
|
}
|
|
|
|
_, err = col.Upsert("u:jade",
|
|
User{
|
|
Name: "Jade",
|
|
Email: "jade@test-email.com",
|
|
Interests: []string{"Swimming", "Rowing"},
|
|
}, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Get the document back
|
|
getResult, err := col.Get("u:jade", nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var inUser User
|
|
err = getResult.Content(&inUser)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("User: %v\n", inUser)
|
|
|
|
// Perform a N1QL Query
|
|
inventoryScope := bucket.Scope("inventory")
|
|
queryResult, err := inventoryScope.Query(
|
|
fmt.Sprintf("SELECT * FROM airline WHERE id=10"),
|
|
&gocb.QueryOptions{},
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Print each found Row
|
|
for queryResult.Next() {
|
|
var result interface{}
|
|
err := queryResult.Row(&result)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(result)
|
|
}
|
|
|
|
if err := queryResult.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|