无法创建与Cassandra的连接会话。

huangapple go评论124阅读模式
英文:

Unable to create session connecting to Cassandra

问题

我正在使用gocql包。

我正在尝试使这个示例工作。

  1. func main() {
  2. // 连接到集群
  3. cluster := gocql.NewCluster("192.168.1.1", "192.168.1.2", "192.168.1.3")
  4. cluster.ProtoVersion = 3
  5. cluster.Keyspace = "example"
  6. cluster.Consistency = gocql.Quorum
  7. session, err := cluster.CreateSession()
  8. defer session.Close()
  9. if err != nil {
  10. fmt.Printf("%v\n", err)
  11. return
  12. }
  13. uuid := gocql.TimeUUID()
  14. fmt.Printf("UUID : %v", uuid)
  15. query := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES
  16. (?, ?, ?)`, "me", uuid, "hello world")
  17. fmt.Println("About to exec")
  18. err = query.Exec()
  19. // 插入一条推文
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. var id gocql.UUID
  24. var text string
  25. /* 搜索一个特定的记录集,其'timeline'列与值'me'匹配。
  26. * 我们之前创建的二级索引将用于优化搜索 */
  27. if err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?
  28. LIMIT 1`, "me").Consistency(gocql.One).Scan(&id, &text); err != nil {
  29. log.Fatal(err)
  30. }
  31. fmt.Println("Tweet:", id, text)
  32. // 列出所有推文
  33. iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`,
  34. "me").Iter()
  35. for iter.Scan(&id, &text) {
  36. fmt.Println("Tweet:", id, text)
  37. }
  38. if err := iter.Close(); err != nil {
  39. log.Fatal(err)
  40. }
  41. }

我使用Cassandra shell创建了名为"example"的keyspace和名为"tweet"的表,它们都正常工作。

然而,当我运行程序时,它给我返回了这个错误:

  1. 2017/04/14 20:52:55 gocql: unable to dial control conn 192.168.1.3: dial tcp
  2. 192.168.1.3:9042: i/o timeout
  3. gocql: unable to create session: control: unable to connect to initial
  4. hosts: dial tcp 192.168.1.3:9042: i/o timeoutpanic: runtime error: invalid
  5. memory address or nil pointer dereference
  6. [signal 0xc0000005 code=0x0 addr=0x0 pc=0x60236d]
  7. goroutine 1 [running]:
  8. github.com/gocql/gocql.(*Session).Close(0x0)
  9. /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.

  1. func main() {
  2. // connect to the cluster
  3. cluster := gocql.NewCluster("192.168.1.1", "192.168.1.2", "192.168.1.3")
  4. cluster.ProtoVersion = 3
  5. cluster.Keyspace = "example"
  6. cluster.Consistency = gocql.Quorum
  7. session, err := cluster.CreateSession()
  8. defer session.Close()
  9. if err != nil {
  10. fmt.Printf("%v\n", err)
  11. return
  12. }
  13. uuid := gocql.TimeUUID()
  14. fmt.Printf("UUID : %v", uuid)
  15. query := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES
  16. (?, ?, ?)`, "me", uuid, "hello world")
  17. fmt.Println("About to exec")
  18. err = query.Exec()
  19. // insert a tweet
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. var id gocql.UUID
  24. var text string
  25. /* Search for a specific set of records whose 'timeline' column matches
  26. * the value 'me'. The secondary index that we created earlier will be
  27. * used for optimizing the search */
  28. if err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?
  29. LIMIT 1`,"me").Consistency(gocql.One).Scan(&id, &text); err != nil {
  30. log.Fatal(err)
  31. }
  32. fmt.Println("Tweet:", id, text)
  33. // list all tweets
  34. iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`,
  35. "me").Iter()
  36. for iter.Scan(&id, &text) {
  37. fmt.Println("Tweet:", id, text)
  38. }
  39. if err := iter.Close(); err != nil {
  40. log.Fatal(err)
  41. }
  42. }

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:

  1. 2017/04/14 20:52:55 gocql: unable to dial control conn 192.168.1.3: dial tcp
  2. 192.168.1.3:9042: i/o timeout
  3. gocql: unable to create session: control: unable to connect to initial
  4. hosts: dial tcp 192.168.1.3:9042: i/o timeoutpanic: runtime error: invalid
  5. memory address or nil pointer dereference
  6. [signal 0xc0000005 code=0x0 addr=0x0 pc=0x60236d]
  7. goroutine 1 [running]:
  8. github.com/gocql/gocql.(*Session).Close(0x0)
  9. /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 实例,这对于开发来说是可以的。

基本上:

  1. cluster := gocql.NewCluster("127.0.0.1")

并且完全删除这一行:

  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:

  1. cluster := gocql.NewCluster("127.0.0.1")

and just drop this line all together

  1. cluster.ProtoVersion = 3

huangapple
  • 本文由 发表于 2017年4月15日 12:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/43422152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定