Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from thrift import Thrift
- from thrift.transport import TTransport
- from thrift.transport import TSocket
- from thrift.protocol import TBinaryProtocol
- from cassandra import Cassandra
- from cassandra.ttypes import Column, ConsistencyLevel, SlicePredicate, RowPredicate, Clock
- from cassandra.ttypes import SliceRange, ColumnParent, KeyRange
- import time
- # Boilerplate: change these to match cassandra config
- hostname = "cass1.dev.meebo.com"
- port = 9160
- keyspace = "TestKeySpace"
- column_family_name = "TestNormalCF" # Warning: will be truncated
- # Boilerplate: Connect to the server and set the keyspace
- socket = TSocket.TSocket(hostname, port)
- transport = TTransport.TFramedTransport(socket)
- protocol = TBinaryProtocol.TBinaryProtocol(transport)
- client = Cassandra.Client(protocol, protocol)
- transport.open()
- client.set_keyspace(keyspace)
- client.truncate(column_family_name) # Empty the target column family
- timestamp = int(time.time() * 1e6) # GMT epoch microseconds
- column_parent = ColumnParent(column_family=column_family_name)
- # Insert 10 values
- keys_inserted = []
- for i in xrange(10):
- key = str(i)
- column = Column(name=key, value=key, clock=Clock(timestamp))
- client.insert(key, column_parent, column, ConsistencyLevel.ALL)
- keys_inserted.append(key)
- time.sleep(1)
- # Read back max of 7 values, works fine
- row_predicate = RowPredicate(key_range=KeyRange(start_key="", end_key="", count=7))
- slice_predicate = SlicePredicate(slice_range=SliceRange(start="", finish=""))
- scan_results = client.scan(column_parent, row_predicate, slice_predicate, ConsistencyLevel.ALL)
- keys = [keyslice.key for keyslice in scan_results]
- print "%d scan results, keys: %s" % (len(scan_results), keys)
- num_results_scan1 = len(scan_results)
- # INTERESTING PART BEGINS HERE
- # Read back another set of results, starting with last key from the last result selt
- scan2_begin_key = scan_results[len(scan_results)-1].key
- print "Scanning again starting at %s" % scan2_begin_key
- row_predicate = RowPredicate(key_range=KeyRange(start_key=scan2_begin_key, end_key="", count=7))
- scan_results = client.scan(column_parent, row_predicate, slice_predicate, ConsistencyLevel.ALL)
- keys = [keyslice.key for keyslice in scan_results]
- print "%d more scan results, keys: %s" % (len(scan_results), keys)
- num_results_scan2 = len(scan_results)
- if num_results_scan1 + num_results_scan2 - 1 > 10: # -1 since result sets overlap by one key
- print "Too many results were returned!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement