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 marcin. http://wiki.apache.org/cassandra/ClientExamples?action=diff&rev1=61&rev2=62 -------------------------------------------------- = High level clients = - These are often more convenient than raw Thrift, which has a certain lowest-common-denominator flavor to it, because that's what it does. But, it's good to understand the Thrift [[API]] to have an idea of what's going on under the hood. See the individual clients for their respective documentation. * Ruby: @@ -21, +20 @@ * http://github.com/rantav/hector * PHP : * http://github.com/mjpearson/Pandra/tree/master + * http://code.google.com/p/simpletools-php/wiki/SimpleCassie * Grails : * http://github.com/wolpert/grails-cassandra * C++ : @@ -29, +29 @@ * http://github.com/mattvv/hectorsharp === Older clients === - These are not up to date with the latest Cassandra features. * Scala: * http://github.com/viktorklang/Cassidy/tree/master - * http://github.com/nodeta/scalandra/tree/master + * http://github.com/nodeta/scalandra/tree/master * Java: * http://code.google.com/p/cassandra-java-client = Thrift examples = - The rest of this page shows examples of using the low-level [[http://incubator.apache.org/thrift/|Thrift]] interface. These examples are for Cassandra trunk, which will become 0.4. See ClientExamples03 for examples fitting the 03 API. @@ -329, +327 @@ 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 - {{{ - #!/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, 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: - 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 """ + . 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) - 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]] - {{{ - #!/usr/bin/perl -w + {{{#!/usr/bin/perl -w + use strict; use warnings; - use strict; - use warnings; + # Change for your environment use lib '/var/lib/cassandra/interface/gen-perl'; use Cassandra::Cassandra; use Cassandra::Constants; use Cassandra::Types; - # Change for your environment - use lib '/var/lib/cassandra/interface/gen-perl'; - use Cassandra::Cassandra; - use Cassandra::Constants; - use Cassandra::Types; + use Thrift; use Thrift::BinaryProtocol; use Thrift::Socket; use Thrift::BufferedTransport; - use Thrift; - use Thrift::BinaryProtocol; - use Thrift::Socket; - use Thrift::BufferedTransport; use Data::Dumper; + # localhost and 9160 are default in storage conf for rpc listener my $socket = new Thrift::Socket('localhost', 9160); my $transport = new Thrift::BufferedTransport($socket,1024,1024); my $protocol = new Thrift::BinaryProtocol($transport); my $client = new Cassandra::CassandraClient($protocol); - # localhost and 9160 are default in storage conf for rpc listener - my $socket = new Thrift::Socket('localhost', 9160); - my $transport = new Thrift::BufferedTransport($socket,1024,1024); - my $protocol = new Thrift::BinaryProtocol($transport); - my $client = new Cassandra::CassandraClient($protocol); eval { - $transport->open(); + - # See http://wiki.apache.org/cassandra/DataModel or http://wiki.apache.org/cassandra/ClientExamples#PHP for more explanations + . $transport->open(); # See http://wiki.apache.org/cassandra/DataModel or http://wiki.apache.org/cassandra/ClientExamples#PHP for more explanations my $keyspace = 'Keyspace1'; my $row_key = 'firsttest'; + # See http://wiki.apache.org/cassandra/API#ConsistencyLevel my $consistency_level = Cassandra::ConsistencyLevel::ONE; # Authentication only available in 0.6 + my $auth_request = new Cassandra::AuthenticationRequest(); $auth_request->{credentials} = { username => 'user', password => 'password' }; $client->login($keyspace, $auth_request); + my $column_path = new Cassandra::ColumnPath(); $column_path->{column_family} = 'Standard1'; $column_path->{super_column} = undef; $column_path->{column} = 'email'; my $timestamp = time; - my $keyspace = 'Keyspace1'; - my $row_key = 'firsttest'; - - # See http://wiki.apache.org/cassandra/API#ConsistencyLevel - my $consistency_level = Cassandra::ConsistencyLevel::ONE; - - # Authentication only available in 0.6 - my $auth_request = new Cassandra::AuthenticationRequest(); - $auth_request->{credentials} = { username => 'user', password => 'password' }; - $client->login($keyspace, $auth_request); - - my $column_path = new Cassandra::ColumnPath(); - $column_path->{column_family} = 'Standard1'; - $column_path->{super_column} = undef; - $column_path->{column} = 'email'; - - my $timestamp = time; - - my $value = '[email protected]'; - $client->insert($keyspace, $row_key, $column_path, $value, $timestamp, $consistency_level); + my $value = ' [email protected] '; $client->insert($keyspace, $row_key, $column_path, $value, $timestamp, $consistency_level); + $column_path->{column} = 'age'; $timestamp = time; $value = "24"; $client->insert($keyspace, $row_key, $column_path, $value, $timestamp, $consistency_level); my $column1 = new Cassandra::Column(); + $column1->{name} = 'foo'; $column1->{value} = 'fooey value'; $column1->{timestamp} = time; my $column2 = new Cassandra::Column(); + $column2->{name} = 'bar'; $column2->{value} = 'bar like thing'; $column2->{timestamp} = time; + my $super_column = new Cassandra::SuperColumn(); $super_column->{name} = 'SuperColumnName'; $super_column->{columns} = [$column1, $column2]; + my $c_or_sc = new Cassandra::ColumnOrSuperColumn(); $c_or_sc->{super_column} = $super_column; - - $column_path->{column} = 'age'; - $timestamp = time; - $value = "24"; - $client->insert($keyspace, $row_key, $column_path, $value, $timestamp, $consistency_level); - - my $column1 = new Cassandra::Column(); - $column1->{name} = 'foo'; - $column1->{value} = 'fooey value'; - $column1->{timestamp} = time; - - my $column2 = new Cassandra::Column(); - $column2->{name} = 'bar'; - $column2->{value} = 'bar like thing'; - $column2->{timestamp} = time; - - my $super_column = new Cassandra::SuperColumn(); - $super_column->{name} = 'SuperColumnName'; - $super_column->{columns} = [$column1, $column2]; - - my $c_or_sc = new Cassandra::ColumnOrSuperColumn(); - $c_or_sc->{super_column} = $super_column; - - my $mutation = { Super1 => [$c_or_sc] }; - $client->batch_insert($keyspace, 'KeyName', $mutation, $consistency_level); + my $mutation = { Super1 => [$c_or_sc] }; $client->batch_insert($keyspace, 'KeyName', $mutation, $consistency_level); + my $column_parent = new Cassandra::ColumnParent(); $column_parent->{column_family} = "Standard1"; $column_parent->{super_column} = undef; + my $slice_range = new Cassandra::SliceRange(); $slice_range->{start} = ""; $slice_range->{finish} = ""; + my $predicate = new Cassandra::SlicePredicate(); my @list = $predicate->{column_names}; $predicate->{slice_range} = $slice_range; - - my $column_parent = new Cassandra::ColumnParent(); - $column_parent->{column_family} = "Standard1"; - $column_parent->{super_column} = undef; - - my $slice_range = new Cassandra::SliceRange(); - $slice_range->{start} = ""; - $slice_range->{finish} = ""; - - my $predicate = new Cassandra::SlicePredicate(); - my @list = $predicate->{column_names}; - $predicate->{slice_range} = $slice_range; - - my $result = $client->get_slice($keyspace, $row_key, $column_parent, $predicate, $consistency_level); + my $result = $client->get_slice($keyspace, $row_key, $column_parent, $predicate, $consistency_level); # Removal available after 0.6 + $client->remove($keyspace, $row_key, $column_path, $timestamp, $consistency_level); my $result_after = $client->get_slice($keyspace, $row_key, $column_parent, $predicate, $consistency_level); # To get a range of all the keys within that column_parent - - # Removal available after 0.6 - $client->remove($keyspace, $row_key, $column_path, $timestamp, $consistency_level); - my $result_after = $client->get_slice($keyspace, $row_key, $column_parent, $predicate, $consistency_level); - - # To get a range of all the keys within that column_parent - $predicate = new Cassandra::SlicePredicate(); - $predicate->{slice_range} = new Cassandra::SliceRange( {start => '', finish => '' } ); + $predicate = new Cassandra::SlicePredicate(); $predicate->{slice_range} = new Cassandra::SliceRange( {start => '', finish => '' } ); - - my $paged_result = $client->get_range_slice($keyspace, $column_parent, $predicate, '', '', 10, $consistency_level); + my $paged_result = $client->get_range_slice($keyspace, $column_parent, $predicate, '', '', 10, $consistency_level); print Dumper($result, $result_after, $paged_result); + # All of these require 0.6 see http://wiki.apache.org/cassandra/API for more information print Dumper($client->describe_keyspaces); print Dumper($client->describe_keyspace('Keyspace1')); print Dumper($client->describe_cluster_name); print Dumper($client->describe_version); - - print Dumper($result, $result_after, $paged_result); - - # All of these require 0.6 see http://wiki.apache.org/cassandra/API for more information - print Dumper($client->describe_keyspaces); - print Dumper($client->describe_keyspace('Keyspace1')); - print Dumper($client->describe_cluster_name); - print Dumper($client->describe_version); - - $transport->close(); + $transport->close(); + }; if($@){ + - warn(Dumper($@)); + . warn(Dumper($@)); - } - 1; - }}} + } + + 1; }}} == C# == {{{
