1

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

1 Answers1

2

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]
Aaron
  • 55,518
  • 11
  • 116
  • 132
  • thanks Aaron. Despite not having written, implicitly this is the solution I would not want due to I have double "unecessary" iteration. I thought that maybe some bulk load data to array directly. But I'm very thankfull for your answer – Vanderci Curvelo Junior Jun 16 '21 at 20:17