cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J T <jt4websi...@googlemail.com>
Subject Re: simple erlang example
Date Sat, 19 Feb 2011 19:35:45 GMT
Hi Sasha,

Below is some code from an old module I was working with a while back.

It should give you an idea of the direction to go but really if you look at
the files generated by thrift for cassandra it should help as well:

*  cassandra_thrift.erl
*  cassandra_thrift.hrl
*  cassandra_types.erl
*  cassandra_types.hrl

% -- Snip --

get_connection( Host, KeySpace, Credentials) when is_list(KeySpace) ->
    get_connection( Host, list_to_binary(KeySpace), Credentials );

get_connection( Host, KeySpace, Credentials) when is_binary(KeySpace) ->
    case thrift_client_util:new(Host, 9160, cassandra_thrift, [ { framed,
true } ] ) of
        {ok, Connection } -> set_keyspace( Connection, KeySpace ),
                             authenticate( Connection, Credentials ),
                             Connection;
        _                 -> throw({ get_connection, "Failed to get
connection to cassandra!" })
    end.

    authenticate( Connection, _Credentials ) ->
        thrift_client:call( Connection, login, [ #authenticationRequest{
credentials = dict:new() } ]).

    set_keyspace( Connection, KeySpace ) ->
        thrift_client:call( Connection, set_keyspace, [ KeySpace ]).

% Key = ...              e.g. <<"ARowKey">>
% CF  = ColumnFamily     e.g. <<"TestColumnFamily">>
% SC  = SuperColumn      e.g. <<"TestSuperColumn">>
% C   = Column           e.g. <<"TestColumn">>
% CL  = ConsistencyLevel e.g. ?cassandra_ConsistencyLevel_QUORUM

get( Connection, Key, CF, SC, C, CL  ) when is_binary(CF), is_binary(C) ->
    try thrift_client:call( Connection, get, [ Key, #columnPath{
column_family = CF, super_column = SC, column = C }, CL ]) of
        { _, { ok, R  }} -> R;
        { _, { _,  E1 }} -> throw( { ?MODULE, get, E1 } );
        X                -> throw( { ?MODULE, get, X  } )
    catch
        throw:notFoundException   -> notfound
    end.

% -- Snip --

Example Call Might be:

Connection  = get_connection( "localhost", "TestKeySpace", "" ),
ColumnValue = ?MODULE:get( Connection, <<"ARowKey">>, <<"TestColumnFamily
">>, <<"TestSuperColumn">>, <<"TestColumn">>,
?cassandra_ConsistencyLevel_QUORUM
)


On Fri, Feb 18, 2011 at 10:39 PM, Sasha Dolgy <sdolgy@gmail.com> wrote:

> hi,
>
> does anyone have an erlang example for connecting to cassandra and
> performing an operation like a get?
>
> I'm not having much luck with: \thrift-0.5.0\test\erl\src\* as a reference
> point.
>
> I generated all of the erlang files using thrift and have successfully
> compiled them but am having a pretty rough go at it.
>
> Found this old post:
> http://www.mail-archive.com/cassandra-user@incubator.apache.org/msg02893.html ...
> but seems the examples never made it to the wiki.
>
> -sd
>
> --
> Sasha Dolgy
> sasha.dolgy@gmail.com
>

Mime
View raw message