Created
March 15, 2011 07:02
-
-
Save mix3/870410 to your computer and use it in GitHub Desktop.
Accessing Cassandra 0.7.0 from Perl
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use lib $ENV{'CASSANDRA_HOME'}.'/interface/gen-perl/'; | |
use Cassandra::Cassandra; | |
use Cassandra::Constants; | |
use Cassandra::Types; | |
use Thrift; | |
use Thrift::BinaryProtocol; | |
use Thrift::Socket; | |
use Thrift::FramedTransport; | |
use Data::Dumper; | |
# settings | |
my $socket = new Thrift::Socket('localhost', 9160); | |
my $transport = new Thrift::FramedTransport($socket,1024,1024); | |
my $protocol = new Thrift::BinaryProtocol($transport); | |
my $client = new Cassandra::CassandraClient($protocol); | |
eval { | |
# open | |
$transport->open(); | |
# set keyspace | |
my $keyspace = 'mysqlslap'; | |
$client->set_keyspace($keyspace); | |
# keyspace: mysqlslap | |
# column_family: t1 | |
# key: 1 | |
# colmun: { intcol1 } | |
# get_slice, unset range | |
my $column_parent = new Cassandra::ColumnParent( { column_family => 't1' } ); | |
my $consistency_level = Cassandra::ConsistencyLevel::ONE; | |
my $slice_range = new Cassandra::SliceRange( { start => '', finish => '' } ); | |
my $predicate = new Cassandra::SlicePredicate( { slice_range => $slice_range } ); | |
my $result = $client->get_slice(1, $column_parent, $predicate, $consistency_level); | |
#if(!defined $$result[0]){ | |
my $column = new Cassandra::Column(); | |
$column->{name} = 'intcol1'; | |
$column->{value} = int(rand(100000)); | |
$column->{timestamp} = time; | |
$client->insert(1, $column_parent, $column, $consistency_level); | |
#} | |
print Dumper $client->get_slice(1, $column_parent, $predicate, $consistency_level); | |
# close | |
$transport->close(); | |
}; | |
die Dumper($@) if $@; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment