SHARE
TWEET

Untitled

a guest Jul 28th, 2010 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from thrift import Thrift
  2. from thrift.transport import TTransport
  3. from thrift.transport import TSocket
  4. from thrift.protocol import TBinaryProtocol
  5. from cassandra import Cassandra
  6. from cassandra.ttypes import Column, ConsistencyLevel, SlicePredicate, RowPredicate, Clock
  7. from cassandra.ttypes import SliceRange, ColumnParent, KeyRange
  8. import time
  9.  
  10. # Boilerplate: change these to match cassandra config
  11. hostname = "cass1.dev.meebo.com"
  12. port = 9160
  13. keyspace = "TestKeySpace"
  14. column_family_name = "TestNormalCF"  # Warning: will be truncated
  15.  
  16. # Boilerplate: Connect to the server and set the keyspace
  17. socket = TSocket.TSocket(hostname, port)
  18. transport = TTransport.TFramedTransport(socket)
  19. protocol = TBinaryProtocol.TBinaryProtocol(transport)
  20. client = Cassandra.Client(protocol, protocol)
  21. transport.open()
  22. client.set_keyspace(keyspace)
  23.  
  24. client.truncate(column_family_name) # Empty the target column family
  25.  
  26. timestamp = int(time.time() * 1e6)  # GMT epoch microseconds
  27. column_parent = ColumnParent(column_family=column_family_name)
  28.  
  29. # Insert 10 values
  30. keys_inserted = []
  31. for i in xrange(10):
  32.     key = str(i)
  33.     column = Column(name=key, value=key, clock=Clock(timestamp))
  34.     client.insert(key, column_parent, column, ConsistencyLevel.ALL)
  35.     keys_inserted.append(key)
  36.  
  37. time.sleep(1)
  38.  
  39. # Read back max of 7 values, works fine
  40. row_predicate = RowPredicate(key_range=KeyRange(start_key="", end_key="", count=7))
  41. slice_predicate = SlicePredicate(slice_range=SliceRange(start="", finish=""))
  42. scan_results = client.scan(column_parent, row_predicate, slice_predicate, ConsistencyLevel.ALL)
  43.  
  44. keys = [keyslice.key for keyslice in scan_results]
  45. print "%d scan results, keys: %s" % (len(scan_results), keys)
  46.  
  47. num_results_scan1 = len(scan_results)
  48.  
  49. # INTERESTING PART BEGINS HERE
  50.  
  51. # Read back another set of results, starting with last key from the last result selt
  52. scan2_begin_key = scan_results[len(scan_results)-1].key
  53. print "Scanning again starting at %s" % scan2_begin_key
  54. row_predicate = RowPredicate(key_range=KeyRange(start_key=scan2_begin_key, end_key="", count=7))
  55.  
  56. scan_results = client.scan(column_parent, row_predicate, slice_predicate, ConsistencyLevel.ALL)
  57. keys = [keyslice.key for keyslice in scan_results]
  58. print "%d more scan results, keys: %s" % (len(scan_results), keys)
  59. num_results_scan2 = len(scan_results)
  60.  
  61. if num_results_scan1 + num_results_scan2 - 1 > 10: # -1 since result sets overlap by one key
  62.     print "Too many results were returned!"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top