Domanda

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
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top