-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Go version of Part 2 (#106)
* 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
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters