-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetExample.go
57 lines (46 loc) · 1.23 KB
/
tweetExample.go
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
package main
import (
"fmt"
"github.com/gocql/gocql"
)
func main() {
/*
create table example.tweet(timeline text, id UUID, text text, PRIMARY KEY(id));
create index on example.tweet(timeline);
*/
cluster := gocql.NewCluster("127.0.0.1", "127.0.0.2", "127.0.0.3")
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, err := cluster.CreateSession()
if err != nil {
panic(err)
}
defer session.Close()
err = session.Query(`INSERT INTO tweet(timeline, id, text) VALUES(?, ?, ?)`, "me", gocql.TimeUUID(), "Hello World!").Exec()
if err != nil {
panic(err)
}
err = session.Query(`INSERT INTO tweet(timeline, id, text) VALUES(?, ?, ?)`, "you", gocql.TimeUUID(), "Welcome").Exec()
if err != nil {
panic(err)
}
var id gocql.UUID
var text string
err = session.Query(`SELECT id, text FROM tweet WHERE timeline = ? LIMIT 1`, "me").Consistency(gocql.One).Scan(&id, &text)
if err != nil {
panic(err)
}
fmt.Println("Tweet:", id, text)
fmt.Println()
scanner := session.Query(`SELECT id, text FROM tweet`).Iter().Scanner()
for scanner.Next() {
err = scanner.Scan(&id, &text)
if err != nil {
panic(err)
}
fmt.Println("Tweet:", id, text)
}
if err = scanner.Err(); err != nil {
panic(err)
}
}