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

生命不止,继续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\

运行命令:

rethinkdb.exe -d d:\RethinkDB\data\

成功

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

获取:

go get gopkg.in/gorethink/gorethink.v3

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

ConnectOpts

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.

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.

func Expr(val interface{}) Term

func (Term) Run

func (t Term) Run(s QueryExecutor, optArgs ...RunOpts) (*Cursor, error)

Run runs a query using the given connection.

func (*Cursor) One

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

func DB(args ...interface{}) Term

DB references a database.

func TableDrop

func TableDrop(args ...interface{}) Term

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

func TableCreate

func TableCreate(name interface{}, optArgs ...TableCreateOpts) Term

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

官方例子 main.go

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

读写数据库

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

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

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()
}

输出:

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.

原文发布于微信公众号 - 程序员的酒和故事(cppdabaojian)

原文发表时间:2017-12-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ml

hadoop安装及配置入门篇

声明: author: 龚细军 时间: 17-08-01 类型: 笔记 转载时请注明出处及相应链接。 ...

69211
来自专栏王磊的博客

LINQ能不能用系列(二)LINQ to SQL 效率比对

前言 很多人听说过LINQ TO SQL与ADO.NET传统方式用于不同的环境,LINQ TO SQL与ADO.NET传统方式也没有可比性,就像公交车与私家车一...

2995
来自专栏AhDung

【C#】分享基于Win32 API的服务操作类(解决ManagedInstallerClass.InstallHelper不能带参数安装的问题)

------------------201508250915更新------------------

1522
来自专栏我是攻城师

ElasticSearch之Java Api聚合分组实战

4756
来自专栏MasiMaro 的技术博文

windows 下文件的高级操作

本文主要说明在Windows下操作文件的高级方法,比如直接读写磁盘,文件的异步操作,而文件普通的读写方式在网上可以找到一大堆资料,在这也就不再进行专门的说明。

2723
来自专栏Hadoop实操

如何使用Flume采集Kafka数据写入Kudu

1.9K2
来自专栏一个会写诗的程序员的博客

6.3 Spring Boot集成mongodb开发小结

本章我们通过SpringBoot集成mongodb,Java,Kotlin开发一个极简社区文章博客系统。

2103
来自专栏解Bug之路

MySql之自动生成CRUD代码

MyBatis能够通过获取MySql中的information_schema从而获取表的字段等信息,最后通过这些信息生成代码。 笔者受此启发,将MyBatis...

1333
来自专栏二进制文集

Struts1 增删改查

本篇文章介绍如何使用Struts1进行增删改查,仅是对自己学习的记录,并没有深入分析每个模块(不重复造轮子O(∩_∩)O~)。

4443
来自专栏后台开发+音视频+ffmpeg

dpvs源码分析

dpvs是爱奇艺开源的,它是一款基于dpdk的高性能4层负载均衡器。源自于LVS和改版后的alibaba/LVS. dpvs即dpdk-lvs. 等多关于dpv...

1.3K2

扫码关注云+社区

领取腾讯云代金券