Cassandra::Client(3pm) | User Contributed Perl Documentation | Cassandra::Client(3pm) |
Cassandra::Client - Perl library for accessing Cassandra using its binary network protocol
version 0.21
"Cassandra::Client" is a Perl library giving its users access to the Cassandra database, through the native protocol. Both synchronous and asynchronous querying is supported, through various common calling styles.
use Cassandra::Client; my $client= Cassandra::Client->new( contact_points => [ '127.0.0.1', '192.168.0.1' ], username => "my_user", password => "my_password", keyspace => "my_keyspace", ); $client->connect; $client->each_page("SELECT id, column FROM my_table WHERE id=?", [ 5 ], undef, sub { for my $row (@{shift->rows}) { my ($id, $column)= @$row; say "$id: $column"; } });
Defaults to a logged batch, which can be overridden by passing "logged", "unlogged" or "counter" as the "batch_type" attribute.
$client->batch([ [ "INSERT INTO my_table (a, b) VALUES (?, ?)", [ $row1_a, $row1_b ] ], [ "INSERT INTO my_table (a, b) VALUES (?, ?)", [ $row2_a, $row2_b ] ], ], { batch_type => "unlogged" });
For queries that have large amounts of result rows and end up spanning multiple pages, "each_page" is the function you need. "execute" does not handle pagination, and may end up missing rows unless pagination is implemented by its user through the "page" attribute.
$client->execute( "UPDATE my_table SET column=:new_column WHERE id=:id", { new_column => 2, id => 5 }, { consistency => "quorum" }, );
The "idempotent" attribute indicates that the query is idempotent and may be retried without harm.
# Downloads the entire table from the database, even if it's terabytes in size $client->each_page( "SELECT id, column FROM my_table", undef, undef, sub { my $page= shift; for my $row (@{$page->rows}) { say $row->[0]; } });
It's up to the user to choose which calling style to use: synchronous, asynchronous with promises, or through returned coderefs.
All "Cassandra::Client" methods are available as synchronous methods by using their normal names. For example, "$client->connect();" will block until the client has connected. Similarly, "$client->execute($query)" will wait for the query response. These are arguably not the fastest variants (there's no parallelism in queries) but certainly the most convenient.
my $client= Cassandra::Client->new( ... ); $client->connect; $client->execute("INSERT INTO my_table (id, value) VALUES (?, ?) USING TTL ?", [ 1, "test", 86400 ], { consistency => "quorum" });
"Cassandra::Client" methods are also available as promises (see perldoc AnyEvent::XSPromises). This integrates well with other libraries that deal with promises or asynchronous callbacks. Note that for promises to work, "AnyEvent" is required, and needs to be enabled by passing "anyevent => 1" to "Cassandra::Client->new()".
Promise variants are available by prefixing method names with "async_", eg. "async_connect", "async_execute", etc. The usual result of the method is passed to the promise's success handler, or to the failure handler if there was an error.
# Asynchronously pages through the result set, processing data as it comes in. my $promise= $client->async_each_page("SELECT id, column FROM my_table WHERE id=?", [ 5 ], undef, sub { for my $row (@{shift->rows}) { my ($id, $column)= @$row; say "Row: $id $column"; } })->then(sub { say "We finished paging through all the rows"; }, sub { my $error= shift; });
Promises normally get resolved from event loops, so for this to work you need one. Normally you would deal with that by collecting all your promises and then waiting for that :
use AnyEvent::XSPromises qw/collect/; use AnyEvent; my @promises= ( ... ); # See other examples my $condvar= AnyEvent->condvar; collect(@promises)->then(sub { $condvar->send; }, sub { my $error= shift; warn "Unhandled error! $error"; $condvar->send; }); $condvar->recv; # Wait for the promsie to resolve or fail
How you integrate this into your infrastructure is of course up to you, and beyond the scope of the "Cassandra::Client" documentation.
These are the simplest form of asynchronous querying in "Cassandra::Client". Instead of dealing with complex callback resolution, the client simply returns a coderef that, once invoked, returns what the original method would have retruned.
The variants are available by prefixing method names with "future_", eg. "future_connect", "future_execute", etc. These methods return a coderef.
my $coderef= $client->future_execute("INSERT INTO table (id, value) VALUES (?, ?), [ $id, $value ]); # Do other things ... # Wait for the query to finish $coderef->();
Upon errors, the coderef will die, just like the synchronous methods would. Because of this, invoking the coderef immediately after getting it is equal to using the synchronous methods :
# This : $client->connect; # Is the same as : $client->future_connect->();
When used properly, coderefs can give a modest performance boost, but their real value is in the ease of use compared to promises.
Tom van der Woerdt <tvdw@cpan.org>
This software is copyright (c) 2023 by Tom van der Woerdt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2024-03-31 | perl v5.38.2 |