Riak::Light - Fast and lightweight Perl client for Riak
version 0.060
use Riak::Light; # create a new instance - using pbc only my $client = Riak::Light->new( host => '127.0.0.1', port => 8087 ); $client->is_alive() or die "ops, riak is not alive"; # store hashref into bucket 'foo', key 'bar' # will serializer as 'application/json' $client->put( foo => bar => { baz => 1024 }); # store text into bucket 'foo', key 'bar' $client->put( foo => baz => "sometext", 'text/plain'); $client->put_raw( foo => baz => "sometext"); # does not encode ! # fetch hashref from bucket 'foo', key 'bar' my $hash = $client->get( foo => 'bar'); my $text = $client->get_raw( foo => 'baz'); # does not decode ! # delete hashref from bucket 'foo', key 'bar' $client->del(foo => 'bar'); # check if exists (like get but using less bytes in the response) $client->exists(foo => 'baz') or warn "ops, foo => bar does not exist"; # list keys in stream (callback only) $client->get_keys(foo => sub{ my $key = $_[0]; # you should use another client inside this callback! $another_client->del(foo => $key); });
Riak::Light is a very light (and fast) Perl client for Riak using PBC interface. Support operations like ping, get, exists, put, del, and secondary indexes (so-called 2i) setting and querying.
It is flexible to change the timeout backend for I/O operations and can suppress 'die' in case of error (autodie) using the configuration. There is no auto-reconnect option. It can be very easily wrapped up by modules like Action::Retry to manage flexible retry/reconnect strategies.
Riak ip or hostname. There is no default.
Port of the PBC interface. There is no default.
R value setting for this client. Default 2.
W value setting for this client. Default 2.
DW value setting for this client. Default 2.
Boolean, if false each operation will return undef in case of error (stored in $@). Default is true.
Timeout for connection, write and read operations. Default is 0.5 seconds.
Timeout for read operations. Default is timeout value.
Timeout for write operations. Default is timeout value.
Can change the backend for timeout. The default value is IO::Socket::INET and there is only support to connection timeout.
IMPORTANT: in case of any timeout error, the socket between this client and the Riak server will be closed. To support I/O timeout you can choose 5 options (or you can set undef to avoid IO Timeout):
Riak::Light::Timeout::Alarm
uses alarm and Time::HiRes to control the I/O timeout. Does not work on Win32. (Not Safe)
Riak::Light::Timeout::Time::Out
uses Time::Out and Time::HiRes to control the I/O timeout. Does not work on Win32. (Not Safe)
Riak::Light::Timeout::Select
uses IO::Select to control the I/O timeout
Riak::Light::Timeout::SelectOnWrite
uses IO::Select to control only Output Operations. Can block in Write Operations. Be Careful.
Riak::Light::Timeout::SetSockOpt
uses setsockopt to set SO_RCVTIMEO and SO_SNDTIMEO socket properties. Does not Work on NetBSD 6.0.
This is a Riak::Light::Driver instance, to be able to connect and perform requests to Riak over PBC interface.
$client->is_alive() or warn "ops... something is wrong: $@";
Perform a ping operation. Will return false in case of error (will store in $@).
try { $client->ping() } catch { "oops... something is wrong: $_" };
Perform a ping operation. Will die in case of error.
my $value_or_reference = $client->get(bucket => 'key');
Perform a fetch operation. Expects bucket and key names. Decode the json into a Perl structure. if the content_type is 'application/json'. If you need the raw data you can use get_raw.
my $scalar_value = $client->get_raw(bucket => 'key');
Perform a fetch operation. Expects bucket and key names. Return the raw data. If you need decode the json, you should use get instead.
$client->exists(bucket => 'key') or warn "key not found";
Perform a fetch operation but with head => 0, and the if there is something stored in the bucket/key.
$client->put('bucket', 'key', { some_values => [1,2,3] }); $client->put('bucket', 'key', { some_values => [1,2,3] }, 'application/json); $client->put('bucket', 'key', 'text', 'plain/text'); # you can set secondary indexes (2i) $client->put( 'bucket', 'key', 'text', 'plain/text', { field1_bin => 'abc', field2_int => 42 } ); $client->put( 'bucket', 'key', { some_values => [1,2,3] }, undef, { field1_bin => 'abc', field2_int => 42 } );
Perform a store operation. Expects bucket and key names, the value, the content type (optional, default is 'application/json'), and the indexes to set for this value (optional, default is none).
Will encode the structure in json string if necessary. If you need only store the raw data you can use put_raw instead.
IMPORTANT: all the index field names should end by either _int or _bin, depending if the index type is integer or binary.
_int
_bin
To query secondary indexes, see query_index.
$client->put_raw('bucket', 'key', encode_json({ some_values => [1,2,3] }), 'application/json'); $client->put_raw('bucket', 'key', 'text'); $client->put_raw('bucket', 'key', 'text', undef, {field_bin => 'foo'});
Perform a store operation. Expects bucket and key names, the value, the content type (optional, default is 'plain/text'), and the indexes to set for this value (optional, default is none).
Will encode the raw data. If you need encode the structure you can use put instead.
$client->del(bucket => key);
Perform a delete operation. Expects bucket and key names.
$client->get_keys(foo => sub{ my $key = $_[0]; # you should use another client inside this callback! $another_client->del(foo => $key); });
Perform a list keys operation. Receive a callback and will call it for each key. You can't use this callback to perform other operations!
The callback is optional, in which case an ArrayRef of all the keys are returned. But you should always provide a callback, to avoid your RAM usage to skyrocket...
Perform a secondary index query. Expects a bucket name, the index field name, and the index value you're searching on. Returns and ArrayRef of matching keys.
The index value you're searching on can be of two types. If it's a scalar, an exact match query will be performed. if the value is an ArrayRef, then a range query will be performed, the first element in the array will be the range_min, the second element the range_max. other elements will be ignored.
Based on the example in put, here is how to query it:
put
# exact match my $matching_keys = $client->query_index( 'bucket', 'field2_int', 42 ), # range match my $matching_keys = $client->query_index( 'bucket', 'field2_int', [ 40, 50] ),
Net::Riak
Data::Riak
Data::Riak::Fast
Action::Retry
Tiago Peczenyj <tiago.peczenyj@gmail.com>
Damien Krotkine <dams@cpan.org>
This software is copyright (c) 2013 by Weborama.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Riak::Light, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Riak::Light
CPAN shell
perl -MCPAN -e shell install Riak::Light
For more information on module installation, please visit the detailed CPAN module installation guide.