英文:
Unable to create session connecting to Cassandra
问题
我正在使用gocql包。
我正在尝试使这个示例工作。
func main() {
// 连接到集群
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("%v\n", 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()
// 插入一条推文
if err != nil {
log.Fatal(err)
}
var id gocql.UUID
var text string
/* 搜索一个特定的记录集,其'timeline'列与值'me'匹配。
* 我们之前创建的二级索引将用于优化搜索 */
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)
// 列出所有推文
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创建了名为"example"的keyspace和名为"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无法拨号到本地主机连接,并且超时。我不确定如何解决这个问题,stackoverflow和谷歌搜索到目前为止都没有帮助。
有什么想法吗?
英文:
I am using this gocql package.
I am trying to get this example working.
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("%v\n", 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)
}
}
Using the Cassandra shell I have created the keyspace "example" and the table "tweet" and they all work fine.
However, when I run the program, it gives me this error:
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
For some reason, gocql is unable to dial the localhost connection, and it times out. I am not sure how to fix this and a stackoverflow and google search hasn't helped so far.
Any ideas?
答案1
得分: 2
看起来你的计算机上的 IP 地址与集群情况不符。在给我 nodetool status 命令之后,我认为你应该更新 cluster := gocql.NewCluster
,只使用 127.0.0.1
地址,而不是示例中的其他地址。
我认为实际上你只是在运行单节点的本地 Cassandra 实例,这对于开发来说是可以的。
基本上:
cluster := gocql.NewCluster("127.0.0.1")
并且完全删除这一行:
cluster.ProtoVersion = 3
英文:
Looks like the ip addresses are not according to the cluster situation on your computer. After giving me the nodetool status I think you should update the cluster := gocql.NewCluster
to have just the 127.0.0.1
address and not the addresses from example.
I think in reality you are just running single node cassandra local instance. Which is fine for development.
Basically:
cluster := gocql.NewCluster("127.0.0.1")
and just drop this line all together
cluster.ProtoVersion = 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论