Created
April 8, 2012 10:56
-
-
Save larsen/2336609 to your computer and use it in GitHub Desktop.
Using Cassandra's thrift interface, simple example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use strict; | |
| use warnings; | |
| use feature 'say'; | |
| use lib 'gen-perl'; | |
| use Thrift::XS; | |
| use Thrift::Socket; | |
| use Thrift::FramedTransport; | |
| use Thrift::XS::BinaryProtocol; | |
| use Cassandra::Cassandra; | |
| use Cassandra::Types; | |
| use Data::Dumper; | |
| my $host = 'localhost'; | |
| my $port = 9160; | |
| my $socket = Thrift::Socket->new( $host, $port ); | |
| my $transport = Thrift::FramedTransport->new($socket); | |
| my $protocol = Thrift::XS::BinaryProtocol->new($transport); | |
| my $client = Cassandra::CassandraClient->new($protocol); | |
| eval { | |
| $transport->open; | |
| say "Logging in..."; | |
| my $auth_request = Cassandra::AuthenticationRequest->new( | |
| { | |
| credentials => { } | |
| } | |
| ); | |
| $client->login( $auth_request ); | |
| say "Setting keyspace..."; | |
| $client->set_keyspace( "Test" ); | |
| # insert(binary key, ColumnParent column_parent, Column column, ConsistencyLevel consistency_level) | |
| say "Inserting value..."; | |
| $client->insert( | |
| '1', | |
| Cassandra::ColumnParent->new( { column_family => 'TestCF' } ), | |
| Cassandra::Column->new( { | |
| name => 'Foo', | |
| value => 'Bar', | |
| timestamp => time() | |
| } ) | |
| ); | |
| }; | |
| if ($@) { | |
| say Dumper( $@ ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment