Cassandra数据库Golang Driver

搭建cassandra集群后,项目使用Golang语言对cassandra进行操作。需要Golang Driver,有gocql,gocql只提供了curd的操作方法,但不支持创建、删除keyspace。查询后有两种方法可以实现:

考虑后使用第一方法

下载代码包

1
2
go get -u github.com/gocql/gocql
go get -u github.com/kristoiv/gocqltable

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main

import (
"fmt"
"log"
"time"

"github.com/gocql/gocql"
"github.com/kristoiv/gocqltable"
)

func main() {

// Generic initialization of gocql
c := gocql.NewCluster("10.112.68.186", "10.112.68.192")
s, err := c.CreateSession()
if err != nil {
log.Fatalln("Unable to open up a session with the Cassandra database (err=" + err.Error() + ")")
}

// Tell gocqltable to use this session object as the default for new objects
gocqltable.SetDefaultSession(s)
fmt.Println("Gocql session setup complete")

// Now we're ready to create our first keyspace. We start by getting a keyspace object
keyspace := gocqltable.NewKeyspace("gocqltable_test")

// Now lets create that in the database using the simple strategy and durable writes (true)
err = keyspace.Create(map[string]interface{}{
"class": "SimpleStrategy",
"replication_factor": 1,
}, true)
if err != nil { // If something went wrong we print the error and quit.
log.Fatalln(err)
}
fmt.Println("Keyspace created")

// Now that we have a very own keyspace to play with, lets create our first table.

// First we need a Row-object to base the table on. It will later be passed to the table wrapper
// to be used for returning row-objects as the answer to fetch requests.
type User struct {
Email string // Our primary key
Password string
Active bool
Created time.Time
}

// Let's define and instantiate a table object for our user table
userTable := struct {
gocqltable.Table
}{
keyspace.NewTable(
"users", // The table name
[]string{"email"}, // Row keys
nil, // Range keys
User{}, // We pass an instance of the user struct that will be used as a type template during fetches.
),
}

// Lets create this table in our cassandra database
err = userTable.Create()
if err != nil {
log.Fatalln(err)
}
fmt.Println("")
fmt.Println("Table created: users")

// Now that we have a keyspace with a table in it: lets make a few rows! Notice that this is the base example, it uses CQL (not ORM)
// for database interactions such as INSERT/SELECT/UPDATE/DELETE.
err = userTable.Query("INSERT INTO gocqltable_test.users (email, password, active, created) VALUES (?, ?, ?, ?)", "1@example.com", "123456", true, time.Now().UTC()).Exec()
if err != nil {
log.Fatalln(err)
}
fmt.Println("User inserted: 1@example.com")

err = userTable.Query("INSERT INTO gocqltable_test.users (email, password, active, created) VALUES (?, ?, ?, ?)", "2@example.com", "123456", true, time.Now().UTC()).Exec()
if err != nil {
log.Fatalln(err)
}
fmt.Println("User inserted: 2@example.com")

err = userTable.Query("INSERT INTO gocqltable_test.users (email, password, active, created) VALUES (?, ?, ?, ?)", "3@example.com", "123456", true, time.Now().UTC()).Exec()
if err != nil {
log.Fatalln(err)
}
fmt.Println("User inserted: 3@example.com")

// With our database filled up with users, lets query it and print out the results.
iter := userTable.Query("SELECT * FROM gocqltable_test.users").Fetch()
fmt.Println("")
fmt.Println("Fetched all from users:")
for row := range iter.Range() {
user := row.(*User) // Our row variable is a pointer to "interface{}", and here we type assert it to a pointer to "User"
fmt.Println("User:", user) // Let's just print that
}
if err := iter.Close(); err != nil {
log.Fatalln(err)
}

// You can also fetch a single row, obviously
row, err := userTable.Query(`SELECT * FROM gocqltable_test.users WHERE email = ? LIMIT 1`, "2@example.com").FetchRow()
if err != nil {
log.Fatalln(err)
}
user := row.(*User)
fmt.Println("")
fmt.Println("Fetched single row by email: ", user)

// Lets clean up after ourselves by dropping the keyspace.
keyspace.Drop()
fmt.Println("")
fmt.Println("Keyspace dropped")
}