Vladimir Prudnikov
Vladimir Prudnikov

Reputation: 7242

Cassandra and Pycassa: Best way to determine if row with particular key exists

Using pycassa, what is the best way to determine if a record with particular key exists? Is this

try:
    cf.get(key, columns=[])
except pycassa.NotFoundException:
    # Not exists
else:
    # Exists

is a good solution? Will this use only key cache?

UPDATE: I just tried this query and it always raises pycassa.NotFoundException if columns=[] specified.

Upvotes: 2

Views: 935

Answers (1)

Schildmeijer
Schildmeijer

Reputation: 20946

To see if a specific row key exists in a cf you do:

>>> cf.get(key)
{'col_name': 'col_val', 'col_name2': 'col_val2'}

If that row key happens to be in the key cache then the value from cache will be used. You will need to look into the appropriate sstables to find the actual values that corresponds to this row key. This could require a (slow) disk seek/access unless you are lucky and hit the row cache or the (linux) page cache.

Upvotes: 1

Related Questions