February 6, 2018


When using Cassandra you quickly learn that preparing queries is the way to go. It makes them about a billion times faster. But sometimes you need to do fancy things to your queries like set up a specific retry policy or set up a specific consistency policy that is active for just that query. I found few examples of how to do this in the documentation or on the internet. So, in Python, here's how to do both.

__queries = {
    "select": None,  # store the prepared select query
}

def select_data(foo):
    if (__queries["select"] is None):
        __queries["select"] = session.prepare("""
            SELECT col1, col2
            FROM mykeyspace.mytable
            WHERE col3 = :foo
        """)
        __queries["select"].retry_policy = cassandra.policies.DowngradingConsistencyRetryPolicy()
    query = __queries["select"]

    bound = query.bind({"foo": foo})
    bound.consistency_level = cassandra.ConsistencyLevel.QUORUM

    rows = session.execute(bound)
    for row in rows:
        pass

Tada.

Comments

No comments ... yet.

Post Comment

About

Location Seattle, WA
First post September 6, 2004
Total posts 5,943

Tags

  1. 1,794
  2. 693
  3. 631
  4. 585
  5. 552
  6. 494
  7. 268
  8. 192
  9. 191
  10. 139
  11. 131
  12. 105
  13. 104
  14. 86
  15. 78
  16. 70
  17. 58
  18. 56
  19. 41
  20. 26
Load the RSS feed for entries or for comments.