Client & Connections¶
To connect to a Riak cluster, you must create a
RiakClient
object. The default configuration
connects to a Riak node on localhost
with the default
ports. The below instantiation statements are all equivalent:
from aioriak import RiakClient
client = RiakClient()
async def go():
client = await RiakClient.create(host='127.0.0.1', port=8087)
Note
Connections are not established until you attempt to perform an operation. If the host or port are incorrect, you will not get an error raised immediately.
Client objects¶
-
class
aioriak.client.
RiakClient
(host='localhost', port=8087, loop=None)[source]¶ The
RiakClient
object holds information necessary to connect to Riak. Requests can be made to Riak directly through the client or by using the methods on related objects.-
resolver
¶ The sibling-resolution function for this client. Defaults to
riak.resolver.default_resolver()
.
-
Client-level Operations¶
Some operations are not scoped by buckets or bucket types and can be performed on the client directly:
-
coroutine
RiakClient.
ping
()[source]¶ Check if the Riak server for this
RiakClient
instance is alive.Return type: boolean
-
coroutine
RiakClient.
get_buckets
(bucket_type=None)[source]¶ Get the list of buckets as
Bucket
instances.Warning
Do not use this in production, as it requires traversing through all keys stored in a cluster.
Parameters: bucket_type ( BucketType
) – the optional containing bucket typeReturn type: list of Bucket
instances
Accessing Bucket Types and Buckets¶
Most client operations are on bucket type objects
, the bucket objects
they contain or keys within those buckets. Use the
bucket_type
or bucket
methods for creating bucket types and buckets
that will proxy operations to the called client.
-
RiakClient.
bucket_type
(name)[source]¶ Gets the bucket-type by the specified name. Bucket-types do not always exist (unlike buckets), but this will always return a
BucketType
object.Parameters: name (str) – the bucket name Return type: BucketType
-
RiakClient.
bucket
(name, bucket_type='default')[source]¶ Get the bucket by the specified name. Since buckets always exist, this will always return a
Bucket
. If you are using a bucket that is contained in a bucket type, it is preferable to access it from the bucket type object:# Preferred: client.bucket_type("foo").bucket("bar") # Equivalent, but not preferred: client.bucket("bar", bucket_type="foo")
Parameters: - name (str) – the bucket name
- bucket_type (
BucketType
or str) – the parent bucket-type
Return type:
Bucket Type Operations¶
-
coroutine
RiakClient.
get_bucket_type_props
(bucket_type)[source]¶ Fetches properties for the given bucket-type.
Parameters: bucket_type (BucketType) – the bucket-type whose properties will be fetched Return type: dict
-
coroutine
RiakClient.
set_bucket_type_props
(bucket_type, props)[source]¶ Sets properties for the given bucket-type.
Parameters: - bucket_type (BucketType) – the bucket-type whose properties will be set
- props (dict) – the properties to set
Bucket Operations¶
-
coroutine
RiakClient.
get_bucket_props
(bucket)[source]¶ Fetches bucket properties for the given bucket.
Parameters: bucket (Bucket) – the bucket whose properties will be fetched Return type: dict
Key-level Operations¶
-
coroutine
RiakClient.
get
(robj)[source]¶ Fetches the contents of a Riak object.
Parameters: robj (RiakObject) – the object to fetch
-
coroutine
RiakClient.
put
(robj, return_body)[source]¶ Stores an object in the Riak cluster.
Parameters: - return_body (boolean) – whether to return the resulting object after the write
- robj (RiakObject) – the object to store
-
coroutine
RiakClient.
delete
(robj)[source]¶ Deletes an object from Riak.
Parameters: robj (RiakObject) – the object to delete
-
coroutine
RiakClient.
fetch_datatype
(bucket, key)[source]¶ Fetches the value of a Riak Datatype.
Parameters: - bucket (
Bucket
) – the bucket of the datatype, which must belong to aBucketType
- key (string) – the key of the datatype
Return type: - bucket (
Serialization¶
The client supports automatic transformation of Riak responses into
Python types if encoders and decoders are registered for the
media-types. Supported by default are application/json
and
text/plain
.
-
aioriak.client.
default_encoder
(obj)[source]¶ Default encoder for JSON datatypes, which returns UTF-8 encoded json instead of the default bloated backslash u XXXX escaped ASCII strings.
-
RiakClient.
get_encoder
(content_type)[source]¶ Get the encoding function for the provided content type.
Parameters: content_type (str) – the requested media type Return type: function
-
RiakClient.
set_encoder
(content_type, encoder)[source]¶ Set the encoding function for the provided content type.
Parameters: - content_type (str) – the requested media type
- encoder (function) – an encoding function, takes a single object argument and returns encoded data