Merging other languages

This commit is contained in:
James Miranda
2016-09-23 18:01:10 -03:00
parent 380a8ee74c
commit de3c5bdaa4
490 changed files with 24539 additions and 24588 deletions

View File

@@ -1,114 +1,113 @@
# 5.6 NOSQL数据库操作
NoSQL(Not Only SQL)指的是非关系型的数据库。随着Web2.0的兴起传统的关系数据库在应付Web2.0网站特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。
而Go语言作为21世纪的C语言对NOSQL的支持也是很好目前流行的NOSQL主要有redis、mongoDB、Cassandra和Membase等。这些数据库都有高性能、高并发读写等特点目前已经广泛应用于各种应用中。我接下来主要讲解一下redis和mongoDB的操作。
## redis
redis是一个key-value存储系统。和Memcached类似它支持存储的value类型相对更多包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。
目前应用redis最广泛的应该是新浪微博平台其次还有Facebook收购的图片社交网站instagram。以及其他一些有名的[互联网企业](http://redis.io/topics/whos-using-redis)
Go目前支持redis的驱动有如下
- https://github.com/alphazero/Go-Redis
- http://code.google.com/p/tideland-rdc/
- https://github.com/simonz05/godis
- https://github.com/hoisie/redis.go
目前我fork了最后一个驱动更新了一些bug目前应用在我自己的短域名服务项目中(每天200W左右的PV值)
https://github.com/astaxie/goredis
接下来的以我自己fork的这个redis驱动为例来演示如何进行数据的操作
package main
import (
"github.com/astaxie/goredis"
"fmt"
)
func main() {
var client goredis.Client
// 设置端口为redis默认端口
client.Addr = "127.0.0.1:6379"
//字符串操作
client.Set("a", []byte("hello"))
val, _ := client.Get("a")
fmt.Println(string(val))
client.Del("a")
//list操作
vals := []string{"a", "b", "c", "d", "e"}
for _, v := range vals {
client.Rpush("l", []byte(v))
}
dbvals,_ := client.Lrange("l", 0, 4)
for i, v := range dbvals {
println(i,":",string(v))
}
client.Del("l")
}
我们可以看到操作redis非常的方便而且我实际项目中应用下来性能也很高。client的命令和redis的命令基本保持一致。所以和原生态操作redis非常类似。
## mongoDB
MongoDB是一个高性能开源无模式的文档型数据库是一个介于关系数据库和非关系数据库之间的产品是非关系数据库当中功能最丰富最像关系数据库的。他支持的数据结构非常松散采用的是类似json的bjson格式来存储数据因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大其语法有点类似于面向对象的查询语言几乎可以实现类似关系数据库单表查询的绝大部分功能而且还支持对数据建立索引。
下图展示了mysql和mongoDB之间的对应关系我们可以看出来非常的方便但是mongoDB的性能非常好。
![](images/5.6.mongodb.png?raw=true)
5.1 MongoDB和Mysql的操作对比图
目前Go支持mongoDB最好的驱动就是[mgo](http://labix.org/mgo)这个驱动目前最有可能成为官方的pkg。
下面我将演示如何通过Go来操作mongoDB
package main
import (
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Person struct {
Name string
Phone string
}
func main() {
session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"})
if err != nil {
panic(err)
}
result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil {
panic(err)
}
fmt.Println("Phone:", result.Phone)
}
我们可以看出来mgo的操作方式和beedb的操作方式几乎类似都是基于struct的操作方式这个就是Go Style。
## links
* [目录](<preface.md>)
* 上一节: [使用beedb库进行ORM开发](<05.5.md>)
* 下一节: [小结](<05.7.md>)
# 5.6 NoSQL database
A NoSQL database provides a mechanism for the storage and retrieval of data that uses looser consistency models than typical relational databases in order to achieve horizontal scaling and higher availability. Some authors refer to them as "Not only SQL" to emphasize that some NoSQL systems do allow SQL-like query languages to be used.
As the C language of the 21st century, Go has good support for NoSQL databases, including the popular redis, mongoDB, Cassandra and Membase NoSQL databases.
## redis
redis is a key-value storage system like Memcached, that supports the string, list, set and zset(ordered set) value types.
There are some Go database drivers for redis:
- [https://github.com/alphazero/Go-Redis](https://github.com/alphazero/Go-Redis)
- [http://code.google.com/p/tideland-rdc/](http://code.google.com/p/tideland-rdc/)
- [https://github.com/simonz05/godis](https://github.com/simonz05/godis)
- [https://github.com/hoisie/redis.go](https://github.com/hoisie/redis.go)
I forked the last of these packages, fixed some bugs, and used it in my short URL service (2 million PV every day).
- [https://github.com/astaxie/goredis](https://github.com/astaxie/goredis)
Let's see how to use the driver that I forked to operate on a database:
package main
import (
"github.com/astaxie/goredis"
"fmt"
)
func main() {
var client goredis.Client
// Set the default port in Redis
client.Addr = "127.0.0.1:6379"
// string manipulation
client.Set("a", []byte("hello"))
val, _ := client.Get("a")
fmt.Println(string(val))
client.Del("a")
// list operation
vals := []string{"a", "b", "c", "d", "e"}
for _, v := range vals {
client.Rpush("l", []byte(v))
}
dbvals,_ := client.Lrange("l", 0, 4)
for i, v := range dbvals {
println(i,":",string(v))
}
client.Del("l")
}
We can see that it's quite easy to operate redis in Go, and it has high performance. Its client commands are almost the same as redis' built-in commands.
## mongoDB
mongoDB (from "humongous") is an open source document-oriented database system developed and supported by 10gen. It is part of the NoSQL family of database systems. Instead of storing data in tables as is done in a "classical" relational database, MongoDB stores structured data as JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.
![](images/5.6.mongodb.png?raw=true)
Figure 5.1 MongoDB compared to Mysql
The best driver for mongoDB is called `mgo`, and it is possible that it will be included in the standard library in the future.
Here is the example:
package main
import (
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Person struct {
Name string
Phone string
}
func main() {
session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"})
if err != nil {
panic(err)
}
result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil {
panic(err)
}
fmt.Println("Phone:", result.Phone)
}
We can see that there are no big differences when it comes to operating on mgo or beedb databases; they are both based on structs. This is the Go way of doing things.
## Links
- [Directory](preface.md)
- Previous section: [Develop ORM based on beedb](05.5.md)
- Next section: [Summary](05.7.md)