Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Get Go-ing with RethinkDB

Get Go-ing with RethinkDB

Avatar for Daniel Cannon

Daniel Cannon

June 24, 2015

Other Decks in Technology

Transcript

  1. Get Go-ing with RethinkDB Daniel Cannon @_dancannon Software Engineer at

    Hailo London RethinkDB Meetup London, UK 24th June 2015
  2. Overview • What is Go and why should I care?

    • Why is Go a good fit for RethinkDB? • Code Examples
  3. What is Go and why should I care? • Relatively

    new language developed by Google • Statically typed language • Compiles to a single binary • Easy to work with • Fast execution • Goroutines and Channels
  4. What is Go and why should I care? • Relatively

    new language developed by Google • Statically typed language • Compiles to a single binary • Easy to work with • Fast execution • Goroutines and Channels (??)
  5. Goroutines and Channels messageChannel := make(chan string) go func() {

    for range time.Tick(time.Second) { // Put a message on the channel messageChannel <- "Tick" } }() // Keep waiting for messages for message := range messageChannel { fmt.Println(message) }
  6. Why should I use RethinkDB? • Fast for development •

    Really simple to get deploy • Easy to scale (distributed) • Built for real-time applications • But great for other applications too!
  7. Go + RethinkDB = GoRethink • GoRethink is the Go

    driver for RethinkDB • Most popular community supported driver • Up to date with the official drivers • v1.0 release is coming very soon! • 465 stars on Github • github.com/dancannon/gorethink
  8. Go + RethinkDB = GoRethink • You guessed it, fast

    development • Basic compile time checking of queries • Typed data structures • Built-in support for connecting to RethinkDB clusters • Changefeeds + Channels
  9. Examples Connecting to a RethinkDB cluster session, err = r.Connect(r.ConnectOpts{

    Database: "test", Addresses: []string{ "localhost:28016", "localhost:28017", "localhost:28018" }, DiscoverHosts: true, }) if err != nil { fmt.Printf("ERROR: %s", err) os.Exit(-1) }
  10. Examples type Doc struct { ID string `gorethink:"id,omitempty"` Name string

    `gorethink:"name"` Created time.Time `gorethink:"created_at"` } r.Table("test").Insert(Doc{ Name: "Hello!", Created: time.Now(), }).RunWrite(session) Inserting a document
  11. Examples Reading multiple documents type Doc struct { ID string

    `gorethink:"id,omitempty"` Name string `gorethink:"name"` Created time.Time `gorethink:"created_at"` } cursor, err := r.Table("test").Run(session) if err != nil { fmt.Printf("ERROR: %s", err) os.Exit(-1) } var docs []Doc cursor.All(&docs) printJSON(docs)
  12. Examples Reading multiple documents [ { "ID": "2651712e-bdea-4582-af21-98763b21234d", "Name": "Hello!",

    "Created": "2015-06-22T21:42:42.348000049Z" }, { "ID": "e0a18c50-d8ea-4913-8c96-272164c04d28", "Name": "Hello!", "Created": "2015-06-22T21:42:41.332999944Z" }, { "ID": "9657daa2-3d62-4aa3-a369-5e123f3cb31c", "Name": "Hello!", "Created": "2015-06-22T21:42:43.361999988Z" }, { "ID": "81c68587-1a22-4689-9dfc-46aaaef5f248", "Name": "Hello!", "Created": "2015-06-22T21:42:40.316999912Z" } ]
  13. Examples Changefeeds and Channels! type Change struct { NewVal *Doc

    `gorethink:"new_val"` OldVal *Doc `gorethink:"old_val"` } cursor, err := r.Table("test").Changes().Run(session) if err != nil { fmt.Printf("ERROR: %s", err) os.Exit(-1) } channel := make(chan Change) cursor.Listen(channel) for doc := range channel { printJSON(doc) }
  14. Examples Changefeeds and Channels! … { "NewVal": { "ID": "5207db38-da67-4745-b413-279e9a4a85df",

    "Name": "Hello!", "Created": "2015-06-23T19:28:15.684000015Z" }, "OldVal": null } { "NewVal": { "ID": "c0473970-8d7b-4521-89e5-27bd03abd65f", "Name": "Hello!", "Created": "2015-06-23T19:28:16.701999902Z" }, "OldVal": null } …
  15. Give GoRethink a try! • Github:
 https://github.com/dancannon/gorethink • Gitter:
 https://gitter.im/dancannon/gorethink

    Gitter is a great place to ask questions. If you dont have time today feel free to ask there.