gocql how to walk through data come from a select query

I have looking for a way to select data from cassandra and load it to an array because I want to processing series data where I need to get data from previous or next indexes. What I've found is an Iterator and all examples in doc make it walk to next

Thanks

Upvotes: 1

Views: 1968

Answers (1)

Aaron
Aaron

Reputation: 57798

looking for a way to select data from cassandra and load it to an array

Sounds simple enough. First, make sure you're connecting:

//connect
cluster := gocql.NewCluster(hostname)

if len(username) > 0 && len(password) > 0 {
    cluster.Authenticator = gocql.PasswordAuthenticator{Username: username,Password: password}
}

cluster.ProtoVersion = protocol_version
cluster.Consistency = consistency
session, _ := cluster.CreateSession()
defer session.Close()

Next, I'll query the system.peers table, iterate through the result set and store the peers in an array using append.

var peer string
var peers []string

localCql := "SELECT peer FROM system.peers"

iter := session.Query(localCql).Iter()
for iter.Scan(&peer) {
    peers = append(peers, peer)
}
if err := iter.Close(); err != nil {
    log.Fatal(err)
}

fmt.Printf("peers = %s\n",peers)

And running it yields the contents of the array:

» ./testCassandra 1.1.1.4 -u aploetz -p flynnLives
peers = [1.1.1.5 1.1.1.6 1.0.1.1 1.0.1.3 1.0.1.2]

Upvotes: 3

Related Questions