-
Notifications
You must be signed in to change notification settings - Fork 37
Getting Started Guide
export GOPATH=$HOME/dosa-sample
mkdir -p $GOPATH/src/dosa-sample
cd $GOPATH/src/dosa-sample
If you don't already have it, see https://github.com/Masterminds/glide/blob/master/README.md#install for specific instructions.
Starting in the base directory of your sources:
export PATH="$PATH:$GOPATH/bin"
In your code, create an object that embeds (github.com/uber-go/dosa).Entity
, we'll call it entities.go
:
package main
import (
"time"
"github.com/uber-go/dosa"
)
type MyFirstDosaObject struct {
dosa.Entity `dosa:"primaryKey=((K1, K2), K3)"`
K1 dosa.UUID
K2 string
K3 int64
C1 time.Time
}
This example describes an object with a partitioning key of (K1, K2)
and
a clustering key of K3
. There is a single non-key field, C1
.
Create main.go as follows:
package main
import (
"context"
"github.com/uber-go/dosa"
_ "github.com/uber-go/dosa/connectors/cassandra"
)
func main() {
registry, err := dosa.NewRegistrar("dosakeyspace", "appname", &MyFirstDosaObject{})
if err != nil {
panic(err.Error())
}
connector, err := dosa.GetConnector("cassandra", nil)
if err != nil {
panic(err.Error())
}
client := dosa.NewClient(registry, connector)
if err := client.Initialize(context.TODO()); err != nil {
panic(err.Error())
}
if err := client.Upsert(context.TODO(), dosa.All(), &MyFirstDosaObject{K1: dosa.NewUUID(), K2: "testdata", K3: 1, C1: time.Now()}); err != nil {
panic(err.Error())
}
}
glide init
go get -u github.com/uber-go/dosa/cmd/dosa
The dosa
binary should now be installed in $GOPATH/bin
.
ls -l $GOPATH/bin/dosa
glide install
go build
Do this to check for errors in your entities, use the CLI's schema dump
command, prefix
is required:
dosa schema dump
This command takes an optional list of directories to look for entities. If none are provided, it will default to the current directory. Since the example assumes entities are defined in ./entities.go
, no arguments are needed.
echo "create keyspace dosakeyspace with replication = { 'class': 'SimpleStrategy', 'replication_factor': 1 };" | cqlsh
dosa schema dump | cqlsh
Now just build and run the app like this:
go build
./dosa-sample
If it worked, you'll find a row in the Cassandra database:
echo 'select * from dosakeyspace.myfirstdosaobject;' | cqlsh
k1 | k2 | k3 | c1
--------------------------------------+----------+----+---------------------------------
b68281f9-4096-4e23-9359-a3b28ebc026e | testdata | 1 | 2017-09-27 10:44:12.799000+0000