1

Hi i'm using the cassandra-cql gem for Ruby on Rails.

I need to count all users. In my case users are rows in the users column family. Users have (id, first_name, last_name, email) The code below returns a value that is in a kind of binary encoding. Does anybody know how i can return the right counter value as integer?

Or is it possible to return the biggest id without fetching all users? In CQL?

Or is there any kind of index_length when i create an index?

def self.counter
    count = @@db.execute("SELECT count(*) FROM users")
    count.fetch do |f|
      puts f.row.columns.first.value
    end
  end
user934801
  • 1,119
  • 1
  • 12
  • 30

1 Answers1

1

In your code sample you are accessing the raw thrift object which is not what you want.

Try this instead:

def self.counter
  count = @@db.execute("SELECT count(*) FROM users")
  count.fetch do |f|
    puts f.column_values.first
    # or
    puts f[0]
  end
end

That said, counting all the rows in a column family is generally not a good idea. If you expect to be doing this query often you should consider using a counter column that you update as you add or remove users.

Community
  • 1
  • 1
psanford
  • 5,580
  • 1
  • 26
  • 25
  • Can you recommend a website that describes the cassandra cql or better the cassandra-cql gem ?? thx – user934801 Jan 02 '12 at 22:00
  • For CQL in general take a look at the [reference docs](http://www.datastax.com/docs/1.0/references/cql/index). For the cassandra-cql gem you can look at the gem's tests (specs) for usage examples. – psanford Jan 03 '12 at 17:20