Key-Value Store
Seaplane comes with a built-in low latency key-value (KV) store to build powerful data science and machine learning applications. This document describes the usage of the KV store and its methods.
The Seaplane SDK hides all the complexity of setting up and connecting to the KV
store and hands you a single kv_store
instance to interact with.
Authentication is handled automatically by your API key set in your .env
file.
You can read more about authentication here.
To use the KV-store import it in your project as follows.
from seaplane.kv import kv_store
Store Operations​
The KV store requires you to define a store before setting any key-value pairs. The following section covers all the store operations.
Creating a Store​
To create a Store call the create_store()
method on the kv_store
and supply
it with the required arguments.
- The store name
type:string
You can provide an optional object containing any of the following elements to override the default KV store behavior.
max_value_size
,type:int
- The maximum allowed size (in bytes) of any value in this KV store. The default max value size is 2MB and the maximum value size should not exceed 8MB (8,000,000 bytes).ttl
,type:int
- The time in seconds at which key-value pair in this store expires.replicas
,type:int
- The number of replicas of this store.allow_locations
,type:list
with elements oftype:string
. The regions where this store can be deployed. For a complete list of locations, see application regions.deny_locatons
,type:list
with elements oftype:string
. The regions where this store can not be deployed. For a complete list of locations, see application regions.
{
"max_value_size": 2000,
"ttl": 500,
"replicas": 3,
"allow_locations": ["region/xn"],
"deny_locations": ["country/xe"]
}
We recommend that most users create stores without altering the default configuration object. This allows Seaplane to figure out the best location, replication and other factors of your store according to your application ruleset.
- Python Code
- Plane Command
from seaplane.kv import kv_store
def my_kv_store_task():
object_store.create_store("my-kv-store", config_object) # config_object is optional
Expected Output:
- If successful, None
- If unsuccessful,
Error
plane kv delete-store my-kv-store
Expected Output:
Created my-kv-store
Deleting a Store​
To delete a store call the delete_store()
method on the kv_store
and supply
the required arguments.
- The store name
type:string
- Python Code
- Plane Command
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.delete_store("my-kv-store")
Expected Output:
- If successful, None
- If unsuccessful,
Error
plane kv delete-store my-kv-store
Expected Output:
The plane CLI tool will request confirmation before it deletes a store. Type
y
to continue.
Are you sure you want to delete store? [y/N]: Y
Deleted my-kv-store
List Stores​
To list all stores in your current tenant call the list_stores()
method on the
kv_store
.
- Python Code
- Plane Command
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.list_stores()
Expected Output:
['my-kv-store-1', 'my-kv-store-2']
plane kv list-stores
Expected Output:
name
------------------
_SEAPLANE_INTERNAL
my-kv-store-1
my-kv-store-2
Key Operations​
The following section describes all available key operations.
Set a Key​
To set a key-value pair call the set_key()
method on the kv_store
and supply
it with the required arguments.
- The store name,
type:string
- The key name,
type:string
- The value,
type:bytes
- Python Code
- Plane Command
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.set_key("my-kv-store", "my-key", b"my-value")
Expected Output:
- If successful, None
- If unsuccessful,
Error
plane kv set my-kv-store my-key my-value
Expected Output:
- If successful, None
- If unsuccessful,
Error
Get a Key​
To get a key-value pair call the get_key()
method on the kv_store
and supply
it with the required arguments.
- The store name,
type:string
- The key name,
type:string
- Python Code
- Plane Command
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.get_key("my-kv-store", "my-key")
Expected Output:
b'my-key'
plane kv get my-kv-store my-key
Expected Output:
plane kv get my-kv-store my-key
b'my-value'
revision: <INT>
Delete a Key​
To delete a key-value pair call the del_key()
method on the kv_store
and
supply it with the required arguments.
- The store name,
type:string
- The key name,
type:string
- Python Code
- Plane Command
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.delete_key("my-kv-store", "my-key")
Expected Output:
- If successful, None
- If unsuccessful,
Error
plane kv del my-kv-store my-key
Expected Output:
Deleted my-key
List Keys​
To list all keys in a store call the list_keys()
method on the kv_store
and
supply it with the required arguments.
- The store name,
type:string
- Python Code
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.list_keys("my-kv-store")
Expected Output:
['my-key-1', 'my-key-2', 'my-key-3']
Key Exists​
To check if a key exists call the exists()
method on the kv_store
and supply
it with the required arguments.
- The store name,
type:string
- The key name,
type:string
- Python Code
from seaplane.kv import kv_store
def my_kv_store_task(msg):
kv_store.exists("my-kv-store", "my-key")
Expected Output:
- If exists, True,
type:bool
- If not exist, False,
type:bool