Dear Wiki user, You have subscribed to a wiki page or wiki category on "Cassandra Wiki" for change notification.
The "ClientExamples" page has been changed by BaijuMuthukadan. The comment on this change is: cleanup Python example. http://wiki.apache.org/cassandra/ClientExamples?action=diff&rev1=64&rev2=65 -------------------------------------------------- A caveat of doing things this way is that a client cannot go up and down, and then up again without shutting down the entire VM. I.e., you can't initClient(), stopClient() and then initClient() again. == Python == - {{{#!/usr/bin/env python # encoding: utf-8 """ Sample Cassandra Client + {{{#!python + #!/usr/bin/env python # encoding: utf-8 + """Sample Cassandra Client - Created by Chris Goffinet on 2009-08-26. """ + Created by Chris Goffinet on 2009-08-26.""" - 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, pprint + 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) transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = Cassandra.Client(protocol) - pp = pprint.PrettyPrinter(indent = 2) keyspace = "Keyspace1" column_path = ColumnPath(column_family="Standard1",column="email") key = "1" value = " [email protected] " timestamp = time.time() try: - . transport.open() """ Insert the data into Keyspace 1 """ - client.insert(keyspace, key, column_path, value, timestamp, ConsistencyLevel.ZERO) """" Query for data """ column_parent = ColumnParent(column_family="Standard1") slice_range = SliceRange(start="", finish="") predicate = SlicePredicate(slice_range=slice_range) result = client.get_slice(keyspace, key, column_parent, predicate, ConsistencyLevel.ONE) pp.pprint(result) + socket = TSocket.TSocket("localhost", 9160) + transport = TTransport.TBufferedTransport(socket) + protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) + client = Cassandra.Client(protocol) + pp = pprint.PrettyPrinter(indent=2) + keyspace = "Keyspace1" + column_path = ColumnPath(column_family="Standard1", column="email") + key = "1" + value = "[email protected] " + timestamp = time.time() + try: + transport.open() + #Insert the data into Keyspace 1 + client.insert(keyspace, + key, + column_path, + value, + timestamp, + ConsistencyLevel.ZERO) + #Query for data + column_parent = ColumnParent(column_family="Standard1") + slice_range = SliceRange(start="", finish="") + predicate = SlicePredicate(slice_range=slice_range) + result = client.get_slice(keyspace, + key, + column_parent, + predicate, + ConsistencyLevel.ONE) + pp.pprint(result) - except Thrift.TException, tx: + except Thrift.TException, tx: - . print 'Thrift: %s' % tx.message + print 'Thrift: %s' % tx.message - finally: + finally: - . transport.close() + transport.close() if __name__ == '__main__': - - . main() + main() - }}} == Perl == This is almost a direct port from the PHP version above at [[http://wiki.apache.org/cassandra/ClientExamples#PHP|PHP Example]] + {{{#!perl - {{{#!/usr/bin/perl -w + #!/usr/bin/perl -w use strict; use warnings;
