I tried to test Couchbase community edition, by installing using these steps:
echo '
deb [ arch=amd64 ] http://packages.couchbase.com/releases/couchbase-server/enterprise/deb/ xenial xenial/main
deb [ arch=amd64 ] http://packages.couchbase.com/releases/couchbase-server/community/deb/ xenial xenial/main
deb http://packages.couchbase.com/ubuntu xenial xenial/main
' | sudo tee /etc/apt/sources.list.d/couchbase.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6EF1EAC479CF7903
sudo apt-get update
sudo apt-get install couchbase-server-community
apt list -a couchbase-server-community
# make command line available
echo '
export PATH=$PATH:/opt/couchbase/bin
' | tee -a .bashrc
export PATH=$PATH:/opt/couchbase/bin
# init cluster
couchbase-cli cluster-init -c 127.0.0.1 \
--cluster-username Administrator \
--cluster-password YourPassword \
--services data,index,query \
--cluster-ramsize 512 \
--cluster-index-ramsize 256
# create bucket
couchbase-cli bucket-create -c 127.0.0.1:8091 --username Administrator \
--password YourPassword --bucket test1 --bucket-type couchbase \
--bucket-ramsize 512
# start n1ql
cbq -u Administrator -p YourPassword -engine=http://127.0.0.1:8091/
And the source code:
package main
import (
"fmt"
"github.com/kokizzu/gotro/L"
"gopkg.in/couchbase/gocb.v1"
"math/rand"
"time"
)
const Username = `Administrator`
const Password = `YourPassword`
const Bucket = `test1`
type Score struct {
User int64 `json:"user"`
RefId int64 `json:"ref_id"`
Epoch int64 `json:"epoch"`
Score int `json:"score"`
Type string `json:"type"`
}
func main() {
cluster, err := gocb.Connect("couchbase://127.0.0.1")
if L.IsError(err,`cannot connect to couchbase`) {
return
}
cluster.Authenticate(gocb.PasswordAuthenticator{Username: Username, Password: Password})
bucket, _ := cluster.OpenBucket(Bucket, "")
m := bucket.Manager("", "")
err = m.CreatePrimaryIndex("", true, false)
if L.IsError(err, `failed create primary index`) {
return
}
indexes := []string{`type`, `epoch`, `user`, `refid`}
for _, index := range indexes {
err = m.CreateIndex(index, []string{index}, true, false)
if L.IsError(err, `failed create index %s`, index) {
return
}
}
for x := 0; x < 10000; x++ {
userId := 1 + rand.Intn(1000)
refId := 1 + rand.Intn(100)
epoch := time.Now().AddDate(0, 0, rand.Intn(365)+1).Unix()
score := 10 + rand.Intn(100)
_, err := bucket.Upsert(
fmt.Sprintf("user%dref%d", userId, refId),
Score{
User: int64(userId),
RefId: int64(refId),
Epoch: epoch,
Score: score,
Type: `score`,
}, 0)
if L.IsError(err, `failed upsert`) {
return
}
}
// Use query
sql := `SELECT user,SUM(score) FROM ` + Bucket + ` WHERE epoch > $1 GROUP BY user ORDER BY 2 DESC`
query := gocb.NewN1qlQuery(sql)
window := []int{1, 7, 30, 365}
for _, delta := range window {
fmt.Println(delta)
epoch := time.Now().AddDate(0, 0, delta).Unix()
rows, err := bucket.ExecuteN1qlQuery(query, []interface{}{epoch})
if L.IsError(err, `failed query %s`, sql) {
return
}
var row interface{}
defer rows.Close()
for rows.Next(&row) {
fmt.Printf("Row: %v", row)
}
}
}
It shows error:
2020-02-15 23:58:33.271 IsError ▶ &gocb.n1qlMultiError{
{Code:0x1388, Message:"GSI CreateIndex() - cause: Fails to create index. There is no available index service that can process this request at this time. Index Service can be in bootstrap, recovery, or non-reachable. Please retry the operation at a later time."},
}
Am I missing some steps? or is this the limitation of Couchbase Community Edition 6.0?
On the web UI, the cluster already has "data,index,query" label, so shouldn't index can be created on that cluster?