Ruby Client API
You can develop applications and tools using Riak TS with the Riak Ruby client. This document covers the Ruby API for Riak TS.
Overview
Riak Ruby client versions 2.3.0+ have new objects in the
Riak::TimeSeries
module, including the necessary operations and data types
to make sense of these operations.
Language | Source | Documentation | Download |
---|---|---|---|
Ruby | riak-ruby-client | GitHub Pages | RubyGems |
Data Types
Scalar
- Not strictly a class itself, contains the basic Ruby core/stdlib:String
,Fixnum
,Bignum
,Float
, andTime
instances that represent a single cell in a time series collection.Row
- anArray
subclass that holds a collection of scalars.Collection
- anArray
subclass that holds a collection of rows.
Scalars in Ruby and Riak TS
- Ruby’s
nil
round-trips to Riak TS’sNULL
without issue so long as the Riak TS cell is nullable. - Ruby’s
String
and Riak TS’svarchar
are interchanged without loss. Float
instances in Ruby on platforms with 64-bitdouble
s will interchange with Riak TS’sdouble
without loss.- Ruby’s
Fixnum
turns into Riak TS’ssint64
and back without loss. - Low-magnitude Ruby
Bignum
instances that fit in asint64
are interchanged without loss. - High-magnitude Ruby
Bignum
instances raise the error:Riak::TimeSeriesError::SerializeBigIntegerError
. - Ruby
BigDecimal
numbers are converted to aFloat
and serialized as a Riak TSdouble
, which will later de-serialize as a RubyFloat
. - Ruby
Complex
numbers raise the error:Riak::TimeSeriesError::SerializeComplexNumberError
. - Ruby
Rational
numbers raise the error:Riak::TimeSeriesError::SerializeRationalNumberError
.
Operations
Riak TS supports five basic operations: single-key reads and deletes, key listing, SQL queries, and writes/submissions.
Single-key Reads
To load a single row with a given key use Riak::TimeSeries::Read
:
read_operation = Riak::TimeSeries::Read.new client, 'GeoCheckins'
read_operation.key = ['myfamily', 'myseries', Time.now]
results = read_operation.read!
Constructor
The new
class method takes two arguments: client
(the Riak::Client
to use) and table_name
(a String
of the name of the table).
Instance Accessors
key
- (read/write) this is how you tell theRead
object what key to find.client
- (read-only) theRiak::Client
thisRead
will use.table_name
- (read-only) theString
table name thisRead
will use.
Instance Method
read!
- issues the read operation to Riak and returns aRow
of data. If no data are found, returnsnil
.
Single-key Deletes
To delete a single row with a given key, use Riak::TimeSeries::Delete
:
delete_operation = Riak::TimeSeries::Deletion.new client, 'GeoCheckins'
delete_operation.key = ['myfamily', 'myseries', Time.now]
delete_operation.delete!
Constructor
The new
class method takes two arguments: client
(the Riak::Client
to use)
and table_name
(a String
of the name of the table).
Instance Accessors
key
- (read/write) this is how you tell theDeletion
object what key to find and delete.client
- (read-only) theRiak::Client
thisDeletion
will use.table_name
- (read-only) theString
table name thisDeletion
will use.
Instance Method
delete!
- issues the deletion to Riak.
Key Listing
To list keys in a table, use the Riak::TimeSeries::List
class.
WARNING: Listing keys is a very expensive operation for a Riak TS cluster.
list_operation = Riak::TimeSeries::List.new client, 'GeoCheckins'
list_operation.issue! do |key|
puts key
end
results = list_operation.issue!
List
is only available via streaming API by invoking the issue!
method. If the issue!
method is called with a block, issue!
will yield
each key (which will be a Row
object) to the provided block in the order that it receives them. When called without a block, issue!
returns and stores the listing of all keys.
Constructor
The new
class method takes two arguments: client
(the Riak::Client
to use)
and table_name
(a String
of the name of the table).
Instance Accessors
client
- (read-only) theRiak::Client
thisList
will use.table_name
- (read-only) theString
table name thisList
will use.timeout
- how many milliseconds Riak should wait for listing.results
- either theRiak::TimeSeries::Collection
of found keys ornil
if the listing wasn’t retrieved for storage.
Instance Method
The issue!
method that starts the key listing can work in two different ways.
Without a block, the streaming key listing will be buffered up into a
Riak::TimeSeries::Collection
. The Collection
will also be available from the
results
accessor.
WARNING: on a table with many rows, the
Collection
can be extremely large and will cause extra memory load, garbage collections, and other issues.
With a block, each key from the listing will be yield
ed into your block as a
Riak::TimeSeries::Row
and not otherwise saved.
SQL Queries
SQL queries are sent with a Riak::TimeSeries::Query
object:
query = Riak::TimeSeries::Query.new client, sql
query.issue!
query.results
Constructor
The new
class method takes two arguments: client
(the Riak::Client
to use)
and a String
of query_text
.
Instance Accessors
query_text
- (read/write) the SQL queryString
.client
- (read-only) theRiak::Client
thisQuery
will use.results
- (read-only) theRiak::TimeSeries::Collection
of results.
Instance Method
The issue!
method issues the query to Riak and populates the results
accessor with the results.
Writing
Data can be submitted to Riak TS with the Riak::TimeSeries::Submission
class.
Measurements are expected as an Array
which represents a collection containing
one or more Array
instances representing rows that contain scalar values for cells.
submission = Riak::TimeSeries::Submission.new client, 'GeoCheckins'
submission.measurements = [
['myfamily', 'myseries', Time.now - 5, 1, 2, '3'],
['myfamily', 'myseries', Time.now - 4, 4, 5, '6']
]
submission.write!
Constructor
The new
class method takes two arguments: client
(the Riak::Client
to use)
and a String
of query_text
.
Instance Accessors
client
- (read-only) theRiak::Client
thisWrite
will use.table_name
- (read-only) theString
table name thisWrite
will use.measurements
- (read-write) theArray<Array<scalar>>
of measurements. The innerArray
s of scalars are order-sensitive: data will be written in the order the cells are in the table’s DDL.
Instance Method
The write!
method writes the data to Riak TS.