Databases Reference
In-Depth Information
Additionally, you could specify the languages as parameters to the Thrift generator program. For
example, to create only the Java Thrift interface run:
thrift --gen java interface/cassandra.thrift
Once the Thrift modules are generated, you can use it in your program. Assuming you have
generated the Python Thrift interfaces and modules successfully, you can connect to the
CarDataStore keyspace and query for data as depicted in Listing 2-5.
LISTING 2-5: Querying CarDataStore keyspace using the Thrift interface
Available for
download on
Wrox.com
from thrift import Thrift
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated
from cassandra import Cassandra
from cassandra.ttypes import *
import time
import pprint
def main():
socket = TSocket.TSocket(“localhost”, 9160)
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
transport = TTransport.TBufferedTransport(socket)
client = Cassandra.Client(protocol)
pp = pprint.PrettyPrinter(indent=2)
keyspace = “CarDataStore”
column_path = ColumnPath(column_family=”Cars”, column=”make”)
key = “1”
try:
transport.open()
#Query for data
column_parent = ColumnParent(column_family=”Cars”)
slice_range = SliceRange(start=””, finish=””)
predicate = SlicePredicate(slice_range=slice_range)
result = client.get_slice(keyspace,
key,
column_parent,
pp.pprint(result)
except Thrift.TException, tx:
print 'Thrift: %s' % tx.message
finally:
transport.close()
if __name__ == '__main__':
main()
query_cardatastore_using_thrift.py
Search WWH ::




Custom Search