You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by Apache Wiki <wi...@apache.org> on 2009/06/04 07:25:08 UTC

[Cassandra Wiki] Update of "ClientExamples" by Chris Goffinet

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cassandra Wiki" for change notification.

The following page has been changed by Chris Goffinet:
http://wiki.apache.org/cassandra/ClientExamples

New page:
The most common way to access Cassandra is via the [http://incubator.apache.org/thrift/ Thrift] interface.

== PHP ==
{{{

<?php
$GLOBALS['THRIFT_ROOT'] = '/usr/share/php/Thrift';

require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/Cassandra.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';

try {
  $socket = new TSocket('localhost', 9160);
  $transport = new TBufferedTransport($socket, 1024, 1024);
  $protocol = new TBinaryProtocol($transport);
  $client = new CassandraClient($protocol);

  $transport->open();
  /* Insert some data into base attributes */
  $key_user_id = "1";
  $timestamp = time();
  $client->insert("users",$key_user_id,"base_attributes:name", "Chris Goffinet", $timestamp, false);
  $client->insert("users",$key_user_id,"base_attributes:age", "24", $timestamp, false);

  $start = -1;
  $end = -1;
  $result = $client->get_slice("users", $key_user_id,"base_attributes", $start, $end);
  
  print_r($result);
  $transport->close();

} catch (TException $tx) {
   print 'TException: '.$tx->why."\n";
}

?>

}}}

== Python ==
Make sure that when you generate the gen-py folder, you move the cassandra/ folder into your Python's site-packages.

{{{
from thrift import Thrift
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
from cassandra import Cassandra
from cassandra.ttypes import *
import time

socket = TSocket.TSocket("localhost", 9160)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Cassandra.Client(protocol)

try:
    transport.open()
    key_user_id = "1";
    timestamp = time.time();
    client.insert("users",key_user_id,"base_attributes:name", "Chris Goffinet", timestamp, false);
    client.insert("users",key_user_id,"base_attributes:age", "24", timestamp, false);

    start = -1;
    end = -1;
    result = client.get_slice("users", key_user_id,"base_attributes", start, end);

    print result
    
except Thrift.TException, tx:
    print 'Thrift: %s' % tx.message
finally:
    transport.close()

}}}