📜 ⬆️ ⬇️

Erlang, Cassandra: First Steps

Reading reviews and comparisons of NoSQL solutions, I often stumbled upon the opinion that Cassandra had problems with documentation. While I was familiar with the architecture and the CLI commands of the system, the problem with the documentation seemed outdated. But the very first attempt to do something in Erlang immediately rested in the long hours of googling. Therefore, in order to facilitate my own, and not only further work, I spread a simple “how to” to carry out basic operations with Cassandra in Erlang.


1. Thrift

To work with Cassandra in Erlang, you need a Thrift client and for it the Cassandra service.
You can get the Thrift client for Erlang like this:
')
svn co svn.apache.org/repos/asf/thrift/trunk thrift

Next, download the thrift utility from here: thrift.apache.org and generate the assandra service (windows):

thrift-0.8.0.exe --gen erl interface/cassandra.thrift

where interface / cassandra.thrift is the thrift file from the Cassandra distribution.
Now we have everything you need to work with Cassandra.

2. Connection and recording

 -include("cassandra_thrift.hrl"). -include("cassandra_types.hrl"). * * * {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="myCF"}, #column{name="col_1",value="Hello World !", timestamp=0}, ?cassandra_ConsistencyLevel_ONE ]). 


Erlang client Thrift after each operation returns the connection. This is convenient for recursive calls to Erlang. Wise people do not recommend using the connection once received thrift_client_util: new (). Why look here: http://stackoverflow.com/questions/10503907/cassandra-thrift-erlang-insert
I do not recommend transferring the connection between processes.
It remains to recall that the connection should be closed, or the Erlang process should be completed. If you open too many connections, Erlang will return the error 'system_limit', which in this case indicates the exhaustion of the limit of system handles. The number of handles in windows can be viewed using Process Explorer.

Posting in super column:

 {C1, _} = thrift_client:call(Connect,'insert', [Mid, #columnParent{column_family=" cf_1 ", super_column = "col_A "}, #column{name="S",value= integer_to_list(MState), timestamp=0}, ?cassandra_ConsistencyLevel_ONE]) 


3. Reading

 try thrift_client:call(Connect,'get',[Key, #columnPath{column_family="cf_1", super_column="col_A", column = "r"}, ?cassandra_ConsistencyLevel_ONE]) of {_C1,{ok,Val}} -> dosome() catch { _, {exception, {notFoundException} = Err}} -> doerr() end. 

As you can see, the specification of the column when reading and writing is different. And the Thrift client uses exceptions (with Erlang, I almost forgot what it is).

4. Documentation

Source: https://habr.com/ru/post/144689/


All Articles