1
votes

I've been working on Cassandra from the past two weeks and am stuck in a few places i was hoping you could help me out with it.

I've installed the apache-cassandra-2.0.1,perlcassa-master,Perl5.10, Thrift::XS and Time::HiRes.I am still not able to connect to Cassandra .when i run perl Makefile.PL i get the follow warning

"Warning: prerequisite Class::Accessor 0 not found. Writing Makefile for perlcassa" Ignoring the warning when i ran my script i got the following error. "Base class package "Class::Accessor" is empty. (Perhaps you need to 'use' the module which defines that package first,or make that module available in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl enter code here/usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .). at /usr/local/share/perl5/Cassandra/Types.pm line 38 BEGIN failed--compilation aborted at /usr/local/share/perl5/Cassandra/Types.pm line 38. Compilation failed in require at /usr/local/share/perl5/Cassandra/Cassandra.pm line 11. BEGIN failed--compilation aborted at /usr/local/share/perl5/Cassandra/Cassandra.pm line 11. Compilation failed in require at /usr/local/share/perl5/perlcassa/Client.pm line 18. BEGIN failed--compilation aborted at /usr/local/share/perl5/perlcassa/Client.pm line 18. Compilation failed in require at /usr/local/share/perl5/perlcassa.pm line 159. BEGIN failed--compilation aborted at /usr/local/share/perl5/perlcassa.pm line 159. Compilation failed in require at ./cassconn.pl line 2. BEGIN failed--compilation aborted at ./cassconn.pl line 2."

my script cassconn.pl

 #!/usr/bin/perl
 use perlcassa;

    my $result = $obj->exec(
    "SELECT n,ei,toj,sal,d FROM sample.test WHERE n='divya'",
    {key_value => 'n'}
     );
    my $row = $result->fetchone();
    print "Row key, col01: ".$row->{key}.", ".$row->{col01}."\n";
1

1 Answers

0
votes

I don't know if it will help you, I will explain how I did it.

Download and install Thrift compiler on your machine.

Install Thrift library for Perl client application using cpan

cpan Thrift

Move to the directory you installed Cassandra, there is interface directory inside it

cd /path/to/cassandra/interface/

Then generate Cassandra library for Perl using Thrift compiled you installed earlier

/path/to/thrift/binary/Thrift --gen Perl cassandra.thrift

It will create a directory gen-perl in the present directory, you will need to include it using use statement in your Perl scripts. Here's an example script, althou it doesn't work with newest versions of Cassandra and Thrift it is pretty similar, in case if you have issues I provide you with example that work with Cassandra-2.0.8 and Thrift-0.9.1.

#!/usr/bin/perl -w

use strict;
use warnings;

# Change for your environment
use lib '/path/to/cassandra/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;

# localhost and 9160 are default in storage conf for rpc listener
my $socket = new Thrift::Socket('localhost', 9160);
my $transport = new Thrift::FramedTransport($socket);
my $protocol = new Thrift::BinaryProtocol($transport);
my $client = new Cassandra::CassandraClient($protocol);

eval {
    $transport->open();
    my $consistency_level = Cassandra::ConsistencyLevel::ONE;
    my $keyspace = 'DEMO';
    my $column_parent = new Cassandra::ColumnParent();
    $column_parent->{column_family} = "User";
    $column_parent->{super_column} = undef;
    $column_parent->{column} = 'age';

    $client->set_keyspace($keyspace);

    my $column = new Cassandra::Column();

    my $row_key = 'Spider-Man';

    $column->{name} = 'first';
    $column->{value} = 'Peter';
    $column->{timestamp} =  time;
    $client->insert($row_key, $column_parent, $column, $consistency_level);

    $column->{name} = 'last';
    $column->{value} = 'Parker';
    $column->{timestamp} =  time;
    $client->insert($row_key, $column_parent, $column, $consistency_level);

    $column->{name} = 'age';
    $column->{value} = '18';
    $column->{timestamp} =  time;
    $client->insert($row_key, $column_parent, $column, $consistency_level);

    $transport->close();
}; if($@){
    warn(Dumper($@));
}
1;

It adds an entry to Cassandra keyspace DEMO with key spider-man and following column values peter, parker, 18.

Here is the result of query on cassandra-cli after execution of this script:

[default@DEMO] list User;
Using default limit of 100
Using default cell limit of 100
(...)
-------------------
RowKey: spidey
=> (name=age, value=18, timestamp=1404222046)
=> (name=first, value=Peter, timestamp=1404222046)
=> (name=last, value=Parker, timestamp=1404222046)
(...)

4 Rows Returned.
Elapsed time: 367 msec(s).