Quantcast
Channel: Perl – James' World
Viewing all articles
Browse latest Browse all 24

Perl Clients for Cassandra

$
0
0

The Perl module Net::Async::CassandraCQL supports CQL3 and comes with a few sample programs and several tests.

To get the perldoc sample code to work:

  1. Perl 5.10 or higher is required. If you’re using Redhat 5 or CentOS 5, then use perlbrew or source to install a user-local version of perl (you should leave the vendor perl alone)
  2. install the Perl Cassandra client modules and dependencies with path-to-new-perl/bin/cpan Net::Async::CassandraCQL
  3. run the CREATE commands:
$ cqlsh
create keyspace if not exists "my_keyspace" with replication = { 
   'class' : 'SimpleStrategy', 'replication_factor' : 3
};
create table my_keyspace.numbers (v int primary key);
quit
# Program: cass_sample.pl
# Note: includes bug fixes for Net::Async::CassandraCQL 0.11 version

use strict;
use warnings;
use 5.10.0;

use IO::Async::Loop;
use Net::Async::CassandraCQL;
use Protocol::CassandraCQL qw( CONSISTENCY_QUORUM );

 my $loop = IO::Async::Loop->new;

 my $cass = Net::Async::CassandraCQL->new(
    host => "localhost",
    keyspace => "my_keyspace", # changed dash to underscore in keyspace
    default_consistency => CONSISTENCY_QUORUM,
 );
 $loop->add( $cass );

 $cass->connect->get;

 my @f;
 for my $number (1 .. 100) {  # added brackets around VALUES
    push @f, $cass->query( "INSERT INTO numbers (v) VALUES ($number)" );
 }

 Future->needs_all( @f )->get;

 my $get_stmt = $cass->prepare( "SELECT v FROM numbers" )->get;

 my ( undef, $result ) = $get_stmt->execute( [] )->get;

 for my $row ( $result->rows_hash ) {
    say "We have a number " . $row->{v};
 }

To cleanup the test keyspace:

$ cqlsh
drop keyspace my_keyspace;
quit

RT#97260: lost a sequence Future

Perl Support For Older Versions of Cassandra (Thrift-based)

SO: Using perl to connect to apache cassandra 2.0.1
Perlcassa – an Apache Cassandra Perl Client


Viewing all articles
Browse latest Browse all 24

Trending Articles