Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra thrift Erlang insert

I'm currently trying to get my head wrap around Cassandra/thrift with Erlang...

I have a Column Family named "mq" (as in message queue)...

I would like to have a row per user (with an user_id), each message would be a new column with timestamp for name and the message as the value.

Here's in Cassandra-cli what I'm doing :

create keyspace my_keyspace;
use my_keyspace;
create column family mq with comparator=UTF8Type and key_validation_class=UTF8Type;

%% Adding to user_id (00000001) the message "Hello World!" 
set mq['00000001']['1336499385041308'] = 'Hello Wold';

Everything works great with Cassandra-cli

However, When I'm trying to insert from Erlang, I'm running into some issue :

1>rr(cassandra_types).
2>{ok, C}=thrift_client_util:new("localhost", 9160, cassandra_thrift,[{framed, true}]).
3>thrift_client:call(C, 'set_keyspace', ["peeem"]).
4>thrift_client:call(C,'insert',["00000001",
                     #columnPath{column_family="mq"},
                     #column{name="1336499385041308", value="Hello World!"},
                     1
                     ]
                   ).

Here's the error :

{error,{bad_args,insert,
              ["00000001",
               #columnPath{column_family = "mq",super_column = undefined,
                           column = undefined},
               #column{name = "1336499385041308",value = "Hello World!",
                       timestamp = undefined,ttl = undefined},1]}}}

Any help would be appreciated...

EDIT 1 :

I have found out that it should be (as it works for someone else) :

thrift_client:call(C,'insert', ["00000001", #columnParent{column_family="mq"}, #column{name="123456",value="Hello World!"}, 2]).

Here's the related error message :

** exception error: no match of right hand side value {{protocol,thrift_binary_protocol,
                                                                 {binary_protocol,{transport,thrift_framed_transport,
                                                                                             {framed_transport,{transport,thrift_socket_transport,
                                                                                                                          {data,#Port<0.730>,infinity}},
                                                                                                               [],[]}},
                                                                                  true,true}},
                                                       {error,closed}}
     in function  thrift_client:send_function_call/3 (src/thrift_client.erl, line 83)
     in call from thrift_client:call/3 (src/thrift_client.erl, line 40)
like image 933
TheSquad Avatar asked Nov 13 '22 05:11

TheSquad


1 Answers

Ok I have found out what was the issue, or issues to be correct...

Here's my code in order to connect and insert...

rr(cassandra_types).
{ok, C}=thrift_client_util:new("localhost", 9160, cassandra_thrift,[{framed, true}]).
{C1, _} = thrift_client:call(C, 'set_keyspace', ["my_keyspace"]).
thrift_client:call(C1,'insert', ["00000001", #columnParent{column_family="mq"}, #column{name="1234567",value="Hello World !", timestamp=0}, 2]).

In fact We should insert into the new thrift_client that 'set_keyspace' returns... apparently for every call done through thrift a new thrift_client is generated and should be used.

Further more, We should use columnParent instead of columnPath (not sure why yet, it just works). And timestamp in the #column is mandatory...

I hope this helps someone else.

like image 92
TheSquad Avatar answered Jan 22 '23 06:01

TheSquad