Go实战--golang中使用RethinkDB(gorethink/gorethink.v3)
生命不止,继续go go go !!! 关于golang中操作数据库,曾经介绍了不少: Go实战–go语言操作sqlite数据库(The way to go) Go实战–golang中使用redis(redigo和go-redis/redis) 今天继续跟大家一起学习分享另一种数据库叫 RethinkDB。 RethinkDBRethinkDB 是一个主要用来存储 JSON 文档的数据库引擎(MongoDB 存储的是 BSON),可以轻松和多个节点连成分布式数据库,非常好用的查询语言以及支持表的 joins 和 group by 操作等,其实跟mongodb类似。 RethinkDB pushes JSON to your apps in realtime. What is RethinkDB? 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. 官网 Windows下安装 解压 创建数据目录: 运行命令: rethinkdb.exe -d d:RethinkDBdata
成功 In recursion: removing file 'd:RethinkDBdatatmp'
warn: Trying to delete non-existent file 'd:RethinkDBdatatmp'
Initializing directory d:RethinkDBdata
Running rethinkdb 2.3.6-windows (MSC 190024215)...
Running on 6.2.9200 (Windows 8,Server 2012)
Loading data from directory d:RethinkDBdata
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
可视化 GoRethink - RethinkDB Driver for Gogithub地址: Star: 获取: go get gopkg.in/gorethink/gorethink.v3
文档地址: 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 func Connect(opts ConnectOpts) (*Session,error)
func Expr 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. 官方例子 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服务没有开启则: 开启后运行: rethinkdb应用访问: 读写数据库 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再来一个比较复杂的例子,代码结构会更好一点: 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]: %sn",err)
}
// Create Table
_,err = r.DB("bookmarkdb").TableCreate("bookmarks").RunWrite(session)
if err != nil {
log.Fatalf("[initDB]: %sn",err)
}
}
func changeFeeds(session *r.Session) {
bookmarks,err := r.Table("bookmarks").Changes().Field("new_val").Run(session)
if err != nil {
log.Fatalf("[changeFeeds]: %sn",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:%dn",bookmark.Name,bookmark.Description,bookmark.Priority)
}
}
}()
}
func init() {
session,Database: "bookmarkdb",MaxIdle: 10,MaxOpen: 10,})
if err != nil {
log.Fatalf("[RethinkDB Session]: %sn",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]: %sn",err)
}
id = bookmark.ID
fmt.Printf("New bookmark has been inserted with ID: %sn",id)
// Retrieve the updated document.
bookmark.Priority = 2
if err := store.Update(bookmark); err != nil {
log.Fatalf("[Update]: %sn",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","rethinkdb"},id)
}
func getByID(id string) {
bookmark,err := store.GetByID(id)
if err != nil {
log.Fatalf("[GetByID]: %sn",err)
}
fmt.Printf("Name:%s,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]: %sn",err)
}
fmt.Println("Read all documents")
for _,v := range bookmarks {
fmt.Printf("Name:%s,Priority:%d,CreatedOn:%sn",v.Name,v.Description,v.Priority,v.CreatedOn.Format(layout))
}
}
func delete() {
if err := store.Delete(id); err != nil {
log.Fatalf("[Delete]: %sn",err)
}
bookmarks,err)
}
fmt.Printf("Number of documents in the table after delete:%dn",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,Priority:2
The value after update:
Name:mgo,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,Priority:1,CreatedOn:2017-12-12 15:10:13
Name:mgo,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公司倒闭? 尾声: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |