3

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
SomeGuyFortune
  • 1,024
  • 13
  • 26
  • can you provide notedool status and cassandra.yaml file from one of the nodes? I wouldn't really change the protocol version at all. – Marko Švaljek Apr 15 '17 at 05:58
  • I'm not entirely sure what to look for. But when i type "nodetool status" on the command promt, this is what i get. **Datacenter: datacenter1 ======================== Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns (effective) Host ID Rack UN 127.0.0.1 182.43 KB 256 100.0% ###some host ID rack1** – SomeGuyFortune Apr 15 '17 at 06:05
  • the cassandra.yaml file is large. Is there any information in particular that you want from the cassandra.yaml file? If it helps, i haven't changed anything in it. It is just as it was when i downloaded cassandra – SomeGuyFortune Apr 15 '17 at 06:11
  • Any luck with 127.0.0.1? – Marko Švaljek Apr 15 '17 at 23:21
  • Yeah.... 127.0.0.1 works. Thanks! Sorry for the late reply – SomeGuyFortune Apr 16 '17 at 17:39
  • No problem man :) always glad to help – Marko Švaljek Apr 16 '17 at 17:56

1 Answers1

2

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
Marko Švaljek
  • 2,071
  • 1
  • 14
  • 26