无法创建连接到 Cassandra 的会话



我正在使用这个gocql包。

我正在尝试让这个例子工作。

func main() {
// connect to the cluster
cluster := gocql.NewCluster("192.168.1.1", "192.168.1.2", "192.168.1.3")
cluster.ProtoVersion = 3
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, err := cluster.CreateSession()
defer session.Close()
if err != nil {
fmt.Printf("%vn", err)
return
}
uuid := gocql.TimeUUID()
fmt.Printf("UUID : %v", uuid)
query := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES         
(?, ?, ?)`, "me", uuid, "hello world")
fmt.Println("About to exec")
err = query.Exec()
// insert a tweet
if err != nil {
log.Fatal(err)
}
var id gocql.UUID
var text string
/* Search for a specific set of records whose 'timeline' column matches
* the value 'me'. The secondary index that we created earlier will be
* used for optimizing the search */
if err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?     
LIMIT 1`,"me").Consistency(gocql.One).Scan(&id, &text); err != nil {
log.Fatal(err)
}
fmt.Println("Tweet:", id, text)
// list all tweets
iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`, 
"me").Iter()
for iter.Scan(&id, &text) {
fmt.Println("Tweet:", id, text)
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}

使用 Cassandra shell,我创建了密钥空间"示例"和表"tweet",它们都可以正常工作。

但是,当我运行该程序时,它给了我这个错误:

2017/04/14 20:52:55 gocql: unable to dial control conn 192.168.1.3: dial tcp     
192.168.1.3:9042: i/o timeout
gocql: unable to create session: control: unable to connect to initial 
hosts: dial tcp 192.168.1.3:9042: i/o timeoutpanic: runtime error: invalid 
memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x60236d]
goroutine 1 [running]:
github.com/gocql/gocql.(*Session).Close(0x0)
/home/mda/.local/go/src/github.com/gocql/gocql/session.go:344 +0x2d

由于某种原因,gocql 无法拨打本地主机连接,并且超时。我不确定如何解决这个问题,到目前为止,堆栈溢出和谷歌搜索没有帮助。

有什么想法吗?

看起来 IP 地址不符合您计算机上的群集情况。在给我 nodetool 状态后,我认为您应该更新cluster := gocql.NewCluster,使其仅具有127.0.0.1地址,而不是示例中的地址。

我认为实际上您只是在运行单节点 cassandra 本地实例。这对开发很好。

基本上:

cluster := gocql.NewCluster("127.0.0.1")

然后把这条线一起放下

cluster.ProtoVersion = 3

最新更新