前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go实战--golang中使用RethinkDB(gorethink/gorethink.v3)

Go实战--golang中使用RethinkDB(gorethink/gorethink.v3)

作者头像
程序员的酒和故事
发布2018-03-12 17:49:38
1.8K0
发布2018-03-12 17:49:38
举报
文章被收录于专栏:程序员的酒和故事

生命不止,继续go go go !!!

关于golang中操作数据库,曾经介绍了不少:

Go实战–go语言操作sqlite数据库(The way to go) Go实战–go语言操作MySQL数据库(go-sql-driver/mysql)

Go实战–golang中使用redis(redigo和go-redis/redis) Go实战–golang中使用MongoDB(mgo)

今天继续跟大家一起学习分享另一种数据库叫 RethinkDB。

RethinkDB

RethinkDB 是一个主要用来存储 JSON 文档的数据库引擎(MongoDB 存储的是 BSON),可以轻松和多个节点连成分布式数据库,非常好用的查询语言以及支持表的 joins 和 group by 操作等,其实跟mongodb类似。

RethinkDB pushes JSON to your apps in realtime. When your app polls for data, it becomes slow, unscalable, and cumbersome to maintain. RethinkDB is the open-source, scalable database that makes building realtime apps dramatically easier.

What is RethinkDB? RethinkDB is the first open-source, scalable JSON database built from the ground up for the realtime web. It inverts the traditional database architecture by exposing an exciting new access model – instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results to applications in realtime. RethinkDB’s realtime push architecture dramatically reduces the time and effort necessary to build scalable realtime apps.

In addition to being designed from the ground up for realtime apps, RethinkDB offers a flexible query language, intuitive operations and monitoring APIs, and is easy to setup and learn.

官网 https://www.rethinkdb.com/

Windows下安装 下载地址: https://www.rethinkdb.com/docs/install/windows/

解压

创建数据目录: d:\RethinkDB\data\

运行命令:

代码语言:text
复制
rethinkdb.exe -d d:\RethinkDB\data\

成功

代码语言:text
复制
In recursion: removing file 'd:\RethinkDB\data\\tmp'
warn: Trying to delete non-existent file 'd:\RethinkDB\data\\tmp'
Initializing directory d:\RethinkDB\data\
Running rethinkdb 2.3.6-windows (MSC 190024215)...
Running on 6.2.9200 (Windows 8, Server 2012)
Loading data from directory d:\RethinkDB\data\
Listening for intracluster connections on port 29015
Listening for client driver connections on port 28015
Listening for administrative HTTP connections on port 8080
Listening on cluster address: 127.0.0.1
Listening on driver address: 127.0.0.1
Listening on http address: 127.0.0.1
To fully expose RethinkDB on the network, bind to all addresses by running rethinkdb with the `--bind all` command line option.
Server ready, "LAPTOP_MNU6522J_xsq" b8612d2e-7c2b-4511-b85a-17468d91bf6d

可视化 http://localhost:8080

GoRethink - RethinkDB Driver for Go

github地址: https://github.com/GoRethink/gorethink

Star: 1253

获取:

代码语言:c#
复制
go get gopkg.in/gorethink/gorethink.v3

文档地址: https://godoc.org/github.com/GoRethink/gorethink

ConnectOpts

代码语言:javascript
复制
type ConnectOpts struct {

    Address string `gorethink:"address,omitempty"`

    Addresses []string `gorethink:"addresses,omitempty"`

    Database string `gorethink:"database,omitempty"`

    Username string `gorethink:"username,omitempty"`

    Password string `gorethink:"password,omitempty"`

    AuthKey string `gorethink:"authkey,omitempty"`

    Timeout time.Duration `gorethink:"timeout,omitempty"`

    WriteTimeout time.Duration `gorethink:"write_timeout,omitempty"`

    ReadTimeout time.Duration `gorethink:"read_timeout,omitempty"`

    KeepAlivePeriod time.Duration `gorethink:"keep_alive_timeout,omitempty"`

    TLSConfig *tls.Config `gorethink:"tlsconfig,omitempty"`

    HandshakeVersion HandshakeVersion `gorethink:"handshake_version,omitempty"`

    UseJSONNumber bool

    NumRetries int

    InitialCap int `gorethink:"initial_cap,omitempty"`

    MaxOpen int `gorethink:"max_open,omitempty"`

    DiscoverHosts bool `gorethink:"discover_hosts,omitempty"`

    HostDecayDuration time.Duration

    NodeRefreshInterval time.Duration `gorethink:"node_refresh_interval,omitempty"`

    MaxIdle int `gorethink:"max_idle,omitempty"`
}

func Connect Connect creates a new database session.

代码语言:lua
复制
func Connect(opts ConnectOpts) (*Session, error)

func Expr Expr converts any value to an expression and is also used by many other terms such as Insert and Update. This function can convert the following basic Go types (bool, int, uint, string, float) and even pointers, maps and structs.

代码语言:javascript
复制
func Expr(val interface{}) Term

func (Term) Run

代码语言:javascript
复制
func (t Term) Run(s QueryExecutor, optArgs ...RunOpts) (*Cursor, error)

Run runs a query using the given connection.

func (*Cursor) One

代码语言:javascript
复制
func (c *Cursor) One(result interface{}) error

One retrieves a single document from the result set into the provided slice and closes the cursor.

func DB

代码语言:go
复制
func DB(args ...interface{}) Term

DB references a database.

func TableDrop

代码语言:go
复制
func TableDrop(args ...interface{}) Term

TableDrop deletes a table. The table and all its data will be deleted.

func TableCreate

代码语言:php
复制
func TableCreate(name interface{}, optArgs ...TableCreateOpts) Term

TableCreate creates a table. A RethinkDB table is a collection of JSON documents.

官方例子 main.go

代码语言:javascript
复制
package main

import (
    "fmt"
    "log"

    r "gopkg.in/gorethink/gorethink.v3"
)

func main() {
    session, err := r.Connect(r.ConnectOpts{
        Address: "localhost:28015",
    })
    if err != nil {
        log.Fatalln(err)
    }

    res, err := r.Expr("Hello World").Run(session)
    if err != nil {
        log.Fatalln(err)
    }

    var response string
    err = res.One(&response)
    if err != nil {
        log.Fatalln(err)
    }

    fmt.Println(response)
}

如果rethinkdb服务没有开启则: 2017/12/12 14:37:34 gorethink: dial tcp [::1]:28015: connectex: No connection could be made because the target machine actively refused it.

开启后运行: Hello World

rethinkdb应用

访问: http://localhost:8080/#tables 创建一个database,命名为players

读写数据库

代码语言:javascript
复制
package main

import (
    "fmt"
    "log"
    "math/rand"
    "strconv"
    "time"

    r "gopkg.in/gorethink/gorethink.v3"
)

//ScoreEntry for scores
type ScoreEntry struct {
    ID         string `gorethink:"id,omitempty"`
    PlayerName string
    Score      int
}

func main() {
    fmt.Println("Connecting to RethinkDB: localhost:28015")

    session, err := r.Connect(r.ConnectOpts{
        Address:  "localhost:28015",
        Database: "players",
    })

    if err != nil {
        log.Fatal("Could not connect")
    }

    err = r.DB("players").TableDrop("scores").Exec(session)
    err = r.DB("players").TableCreate("scores").Exec(session)
    if err != nil {
        log.Fatal("Could not create table")
    }

    err = r.DB("players").Table("scores").IndexCreate("Score").Exec(session)
    if err != nil {
        log.Fatal("Could not create index")
    }

    for i := 0; i < 1000; i++ {
        player := new(ScoreEntry)
        player.ID = strconv.Itoa(i)
        player.PlayerName = fmt.Sprintf("Player %d", i)
        player.Score = rand.Intn(100)
        _, err := r.Table("scores").Insert(player).RunWrite(session)
        if err != nil {
            log.Fatal(err)
        }
    }

    for {
        var scoreentry ScoreEntry
        pl := rand.Intn(1000)
        sc := rand.Intn(6) - 2
        res, err := r.Table("scores").Get(strconv.Itoa(pl)).Run(session)
        if err != nil {
            log.Fatal(err)
        }

        err = res.One(&scoreentry)
        scoreentry.Score = scoreentry.Score + sc
        _, err = r.Table("scores").Update(scoreentry).RunWrite(session)
        time.Sleep(100 * time.Millisecond)
    }
}

可以通过localhost:8080可视化查看:

RethinkDB的CRUD

再来一个比较复杂的例子,代码结构会更好一点: bookmarket_store.go 其中包括了: create update Delete getAll GetByID

代码语言:javascript
复制
package main

import (
    "time"

    r "gopkg.in/gorethink/gorethink.v3"
)

// Bookmark type reperesents the metadata of a bookmark.
type Bookmark struct {
    ID                          string `gorethink:"id,omitempty" json:"id"`
    Name, Description, Location string
    Priority                    int // Priority (1 -5)
    CreatedOn                   time.Time
    Tags                        []string
}

// BookmarkStore provides CRUD operations against the Table "bookmarks".
type BookmarkStore struct {
    Session *r.Session
}

// Create inserts the value of struct Bookmark into Table.
func (store BookmarkStore) Create(b *Bookmark) error {

    resp, err := r.Table("bookmarks").Insert(b).RunWrite(store.Session)
    if err == nil {
        b.ID = resp.GeneratedKeys[0]
    }

    return err
}

// Update modifies an existing value of a Table.
func (store BookmarkStore) Update(b Bookmark) error {

    var data = map[string]interface{}{
        "description": b.Description,
        "location":    b.Location,
        "priority":    b.Priority,
        "tags":        b.Tags,
    }
    // partial update on RethinkDB
    _, err := r.Table("bookmarks").Get(b.ID).Update(data).RunWrite(store.Session)
    return err
}

// Delete removes an existing value from the Table.
func (store BookmarkStore) Delete(id string) error {
    _, err := r.Table("bookmarks").Get(id).Delete().RunWrite(store.Session)
    return err
}

// GetAll returns all documents from the Table.
func (store BookmarkStore) GetAll() ([]Bookmark, error) {
    bookmarks := []Bookmark{}

    res, err := r.Table("bookmarks").OrderBy("priority", r.Desc("createdon")).Run(store.Session)
    err = res.All(&bookmarks)
    return bookmarks, err
}

// GetByID returns single document from the Table.
func (store BookmarkStore) GetByID(id string) (Bookmark, error) {
    var b Bookmark
    res, err := r.Table("bookmarks").Get(id).Run(store.Session)
    res.One(&b)
    return b, err
}

main.go

代码语言:javascript
复制
package main

import (
    "fmt"
    "log"
    "time"

    r "gopkg.in/gorethink/gorethink.v3"
)

var store BookmarkStore
var id string

func initDB(session *r.Session) {
    var err error
    // Create Database
    _, err = r.DBCreate("bookmarkdb").RunWrite(session)
    if err != nil {
        log.Fatalf("[initDB]: %s\n", err)
    }
    // Create Table
    _, err = r.DB("bookmarkdb").TableCreate("bookmarks").RunWrite(session)
    if err != nil {
        log.Fatalf("[initDB]: %s\n", err)
    }
}

func changeFeeds(session *r.Session) {
    bookmarks, err := r.Table("bookmarks").Changes().Field("new_val").Run(session)
    if err != nil {
        log.Fatalf("[changeFeeds]: %s\n", err)
    }
    // Luanch a goroutine to print real-time updates.
    go func() {
        var bookmark Bookmark
        for bookmarks.Next(&bookmark) {
            if bookmark.ID == "" { // for delete, new_val will be null.
                fmt.Println("Real-time update: Document has been deleted")
            } else {
                fmt.Printf("Real-time update: Name:%s, Description:%s, Priority:%d\n",
                    bookmark.Name, bookmark.Description, bookmark.Priority)
            }
        }
    }()
}

func init() {
    session, err := r.Connect(r.ConnectOpts{
        Address:  "localhost:28015",
        Database: "bookmarkdb",
        MaxIdle:  10,
        MaxOpen:  10,
    })

    if err != nil {
        log.Fatalf("[RethinkDB Session]: %s\n", err)
    }
    r.Table("bookmarks").Delete().Run(session)
    // Create Database and Table.
    initDB(session)
    store = BookmarkStore{
        Session: session,
    }
    // Subscribe real-time changes
    changeFeeds(session)
}

func createUpdate() {
    bookmark := Bookmark{
        Name:        "mgo",
        Description: "Go driver for MongoDB",
        Location:    "https://github.com/go-mgo/mgo",
        Priority:    1,
        CreatedOn:   time.Now(),
        Tags:        []string{"go", "nosql", "mongodb"},
    }
    // Insert a new document.
    if err := store.Create(&bookmark); err != nil {
        log.Fatalf("[Create]: %s\n", err)
    }
    id = bookmark.ID
    fmt.Printf("New bookmark has been inserted with ID: %s\n", id)
    // Retrieve the updated document.
    bookmark.Priority = 2
    if err := store.Update(bookmark); err != nil {
        log.Fatalf("[Update]: %s\n", err)
    }
    fmt.Println("The value after update:")
    // Retrieve an existing document by id.
    getByID(id)
    bookmark = Bookmark{
        Name:        "gorethink",
        Description: "Go driver for RethinkDB",
        Location:    "https://github.com/dancannon/gorethink",
        Priority:    1,
        CreatedOn:   time.Now(),
        Tags:        []string{"go", "nosql", "rethinkdb"},
    }
    // Insert a new document.
    if err := store.Create(&bookmark); err != nil {
        log.Fatalf("[Create]: %s\n", err)
    }
    id = bookmark.ID
    fmt.Printf("New bookmark has been inserted with ID: %s\n", id)

}

func getByID(id string) {
    bookmark, err := store.GetByID(id)
    if err != nil {
        log.Fatalf("[GetByID]: %s\n", err)
    }
    fmt.Printf("Name:%s, Description:%s, Priority:%d\n", bookmark.Name, bookmark.Description, bookmark.Priority)
}

func getAll() {
    // Layout for formatting dates.
    layout := "2006-01-02 15:04:05"
    // Retrieve all documents.
    bookmarks, err := store.GetAll()
    if err != nil {
        log.Fatalf("[GetAll]: %s\n", err)
    }
    fmt.Println("Read all documents")
    for _, v := range bookmarks {
        fmt.Printf("Name:%s, Description:%s, Priority:%d, CreatedOn:%s\n", v.Name, v.Description, v.Priority, v.CreatedOn.Format(layout))
    }

}

func delete() {
    if err := store.Delete(id); err != nil {
        log.Fatalf("[Delete]: %s\n", err)
    }
    bookmarks, err := store.GetAll()
    if err != nil {
        log.Fatalf("[GetAll]: %s\n", err)
    }
    fmt.Printf("Number of documents in the table after delete:%d\n", len(bookmarks))
}

func main() {
    createUpdate()
    getAll()
    delete()
}

输出:

代码语言:css
复制
Real-time update: Name:mgo, Description:Go driver for MongoDB, Priority:1
New bookmark has been inserted with ID: 1f98916d-a5d5-400b-828e-8a53d4193521
Real-time update: Name:mgo, Description:Go driver for MongoDB, Priority:2
The value after update:
Name:mgo, Description:Go driver for MongoDB, Priority:1
Real-time update: Name:gorethink, Description:Go driver for RethinkDB, Priority:1
New bookmark has been inserted with ID: 0da9d082-265c-40e8-af54-7210a78cdb19
Read all documents
Name:gorethink, Description:Go driver for RethinkDB, Priority:1, CreatedOn:2017-12-12 15:10:13
Name:mgo, Description:Go driver for MongoDB, Priority:2, CreatedOn:2017-12-12 15:10:13
Real-time update: Document has been deleted
Number of documents in the table after delete:1

悲伤的消息

Today(OCTOBER 05, 2016) I have sad news to share. After more than seven years of development, the company behind RethinkDB is shutting down. We worked very hard to make RethinkDB successful, but in spite of all our efforts we were ultimately unable to build a sustainable business. There is a lot of information to unpack – over the next few months, I’ll write about lessons learned so the startup community can benefit from our mistakes.

如何评价RethinkDB公司倒闭? https://www.zhihu.com/question/51345388?sort=created

尾声: RethinkDB: why we failed 结论: Pick a large market but build for specific users. Learn to recognize the talents you’re missing, then work like hell to get them on your team. Read The Economist religiously. It will make you better faster.

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-12-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 人生有味是多巴胺 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • RethinkDB
  • GoRethink - RethinkDB Driver for Go
  • rethinkdb应用
  • RethinkDB的CRUD
  • 悲伤的消息
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档