Hi everyone,
 just a quick note about the examples up at
http://wiki.apache.org/cassandra/ThriftExamples#Perl

As is widely known, none of them work using the Thrift-generated Perl
modules from Cassandra 0.7.0.

As a side note, even after I updated the test script to 0.7.0,
Thrift::BufferedTransport also would not work, the result is :
$VAR1 = bless( {
                 'code' => 0,
                 'message' => 'TSocket: Could not read 4 bytes from
localhost:9160'
               }, 'Thrift::TException' );

After I found a discussion related to a PHP problem with the same sympton, I
tried changing to Thrift::FramedTransport and it fixes the problem. I've
included a Perl snippet that works against 0.7.0 - it is based on the
current Wiki example. The only way to find the method signatures for the
rest of the API at this time seems to be by reading the code in
Cassandra/Cassandra.pm and Cassandra/Type.pm. I wish I had the time to
contribute better Perl documentation, in fact I will if my day job allows me
some time to.

Best wishes,
Jose Fonseca


#!/usr/bin/perl -w

use strict;
use warnings;

# Change for your environment
use lib '/opt/apache-cassandra-0.7.0/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::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,1024,1024);
my $protocol = new Thrift::BinaryProtocol($transport);
my $client = new Cassandra::CassandraClient($protocol);

eval {
   $transport->open();
   my $keyspace = 'test1';
   my $row_key = 'people_code_1';

   # ColumnParent tells the API the ColumnFamily or SuperColumn we're
working on
   my $column_parent      = new Cassandra::ColumnParent({column_family =>
"people"});
   my $consistency_level = Cassandra::ConsistencyLevel::ONE;
   my $auth_request      = new Cassandra::AuthenticationRequest();

   # accessing object internals directly seems to be standard practice on
the Thrift-generated code
   $auth_request->{credentials} = { username => 'user', password => 'pass'
};

   $client->login($auth_request);
   $client->set_keyspace($keyspace);

   my $timestamp = time;

   my $column = new Cassandra::Column();
   $column->{name}         = 'name';
   $column->{value}     = 'Jon Stewart';
   $column->{timestamp} = time;

   $client->insert($row_key, $column_parent, $column, $consistency_level);

   $column->{name}         = 'tv_show';
   $column->{value}     = 'The Daily Show';
   $client->insert($row_key, $column_parent, $column, $consistency_level);

   # -- INSERT ANOTHER TV PERSONALITY ---

   $row_key = 'people_code_2'; # this is analog to a primary key, you'll
later search for this guy using this key

   $column->{name} = 'name';
   $column->{value} = 'Stephen Colbert';
   $column->{timestamp} = time;

   $client->insert($row_key, $column_parent, $column, $consistency_level);

   $column->{name} = 'tv_show';
   $column->{value} = 'The Colbert Report';
   $client->insert($row_key, $column_parent, $column, $consistency_level);

   # -- LET's QUERY THE PEOPLE COLUMN FAMILY TO FIND OUT WHO WE HAVE ON FILE
---

   my $slice_range = new Cassandra::SliceRange();
   $slice_range->{start} = "";
   $slice_range->{finish} = "";

   my $predicate = new Cassandra::SlicePredicate();
   $predicate->{slice_range} = $slice_range;

   # let's load user with primary key = 'people_code_1'
   my $result = $client->get_slice('people_code_1', $column_parent,
$predicate, $consistency_level);
   print "'people_code_1': " . Dumper($result) . "\n";

   # now, let's load user with primary key = 'people_code_2'
   $result = $client->get_slice('people_code_2', $column_parent, $predicate,
$consistency_level);
   print "'people_code_2': " . Dumper($result) . "\n";

   # nice, eh?

   $transport->close();
};

if ($@) {
   warn(Dumper($@));
}

1;

Reply via email to