Skip to content

Commit

Permalink
Improved Go version of Part 2 (#106)
Browse files Browse the repository at this point in the history
* go version of the mms java examples - part 1,2

This is a complete transliteration of the Java code but more or
less idiomatic Go and driver config to enable shard awareness.

* go version of the mms java examples - part 3

Slightly modified and with an asciiart twist.

* improved go version of the mms java examples - part 2

This version uses github.com/scylladb/gocqlx to make the code much
more clean and readable.

* init removed and replaced separate loose statements with struct

* error handling on separate line
  • Loading branch information
Henrik Johansson authored Jan 30, 2020
1 parent 632f634 commit f27d331
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mms/go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ WORKDIR /build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /build/goapp ./cmd/goapp
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /build/goappp ./cmd/goappp
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /build/godatatypes ./cmd/godatatypes
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /build/gocqlxapp ./cmd/gocqlxapp

FROM alpine:3.10

Expand All @@ -17,5 +18,7 @@ RUN mkdir -p /usr/share/icons/mms
COPY --from=builder /build/cmd/godatatypes/*.jpg /usr/share/icons/mms/
COPY --from=builder /build/godatatypes /usr/local/bin/godatatypes
RUN chmod +x /usr/local/bin/godatatypes
COPY --from=builder /build/gocqlxapp /usr/local/bin/gocqlxapp
RUN chmod +x /usr/local/bin/gocqlxapp

CMD tail -f /dev/null
120 changes: 120 additions & 0 deletions mms/go/cmd/gocqlxapp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package main

import (
"goapp/internal/log"
"goapp/internal/scylla"

"github.com/gocql/gocql"
"github.com/scylladb/gocqlx"
"github.com/scylladb/gocqlx/qb"
"github.com/scylladb/gocqlx/table"
"go.uber.org/zap"
)

var stmts = createStatements()

func main() {
logger := log.CreateLogger("info")

cluster := scylla.CreateCluster(gocql.Quorum, "catalog", "scylla-node1", "scylla-node2", "scylla-node3")
session, err := gocql.NewSession(*cluster)
if err != nil {
logger.Fatal("unable to connect to scylla", zap.Error(err))
}
defer session.Close()

selectQuery(session, logger)
insertQuery(session, "Mike", "Tyson", "12345 Foo Lane", "http://www.facebook.com/mtyson", logger)
insertQuery(session, "Alex", "Jones", "56789 Hickory St", "http://www.facebook.com/ajones", logger)
selectQuery(session, logger)
deleteQuery(session, "Mike", "Tyson", logger)
selectQuery(session, logger)
deleteQuery(session, "Alex", "Jones", logger)
selectQuery(session, logger)
}

func deleteQuery(session *gocql.Session, firstName string, lastName string, logger *zap.Logger) {
logger.Info("Deleting " + firstName + "......")
r := Record{
FirstName: firstName,
LastName: lastName,
}
err := gocqlx.Query(session.Query(stmts.del.stmt), stmts.del.names).BindStruct(r).ExecRelease()
if err != nil {
logger.Error("delete catalog.mutant_data", zap.Error(err))
}
}

func insertQuery(session *gocql.Session, firstName, lastName, address, pictureLocation string, logger *zap.Logger) {
logger.Info("Inserting " + firstName + "......")
r := Record{
FirstName: firstName,
LastName: lastName,
Address: address,
PictureLocation: pictureLocation,
}
err := gocqlx.Query(session.Query(stmts.ins.stmt), stmts.ins.names).BindStruct(r).ExecRelease()
if err != nil {
logger.Error("insert catalog.mutant_data", zap.Error(err))
}
}

func selectQuery(session *gocql.Session, logger *zap.Logger) {
logger.Info("Displaying Results:")
var rs []Record
err := gocqlx.Query(session.Query(stmts.sel.stmt), stmts.sel.names).SelectRelease(&rs)
if err != nil {
logger.Warn("select catalog.mutant", zap.Error(err))
return
}
for _, r := range rs {
logger.Info("\t" + r.FirstName + " " + r.LastName + ", " + r.Address + ", " + r.PictureLocation)
}
}

func createStatements() *statements {
m := table.Metadata{
Name: "mutant_data",
Columns: []string{"first_name", "last_name", "address", "picture_location"},
PartKey: []string{"first_name", "last_name"},
}
tbl := table.New(m)

deleteStmt, deleteNames := tbl.Delete()
insertStmt, insertNames := tbl.Insert()
// Normally a select statement such as this would use `tbl.Select()` to select by
// primary key but now we just want to display all the records...
selectStmt, selectNames := qb.Select(m.Name).Columns(m.Columns...).ToCql()
return &statements{
del: query{
stmt: deleteStmt,
names: deleteNames,
},
ins: query{
stmt: insertStmt,
names: insertNames,
},
sel: query{
stmt: selectStmt,
names: selectNames,
},
}
}

type query struct {
stmt string
names []string
}

type statements struct {
del query
ins query
sel query
}

type Record struct {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Address string `db:"address"`
PictureLocation string `db:"picture_location"`
}
1 change: 1 addition & 0 deletions mms/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/qeesung/image2ascii v1.0.1
github.com/scylladb/gocqlx v1.3.1
github.com/wayneashleyberry/terminal-dimensions v1.0.0 // indirect
go.uber.org/zap v1.13.0
)
Expand Down
8 changes: 8 additions & 0 deletions mms/go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049 h1:K9KHZbXKpGydfDN0aZrsoHpLJlZsBrGMFWbgLDGnPZk=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
Expand All @@ -32,8 +36,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/qeesung/image2ascii v1.0.1 h1:Fe5zTnX/v/qNC3OC4P/cfASOXS501Xyw2UUcgrLgtp4=
github.com/qeesung/image2ascii v1.0.1/go.mod h1:kZKhyX0h2g/YXa/zdJR3JnLnJ8avHjZ3LrvEKSYyAyU=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scylladb/go-reflectx v1.0.1 h1:b917wZM7189pZdlND9PbIJ6NQxfDPfBvUaQ7cjj1iZQ=
github.com/scylladb/go-reflectx v1.0.1/go.mod h1:rWnOfDIRWBGN0miMLIcoPt/Dhi2doCMZqwMCJ3KupFc=
github.com/scylladb/gocql v1.3.1 h1:LkK3OXns6QaMTJrMlWTCGroSnrt/7u1UfBcDzEeU6u0=
github.com/scylladb/gocql v1.3.1/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/scylladb/gocqlx v1.3.1 h1:NTiKaSW1RzDxHQIyPE/KubOJCKRG5xXMUG8FKVKR/j0=
github.com/scylladb/gocqlx v1.3.1/go.mod h1:1CisD8Z+VB7ByxGyc3B9OXusRNgWWtCMkO+hNCpgZAc=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
Expand Down

0 comments on commit f27d331

Please sign in to comment.