Skip to content
mtrencseni edited this page Aug 22, 2011 · 2 revisions

ScalienDB Wiki Documentation

A short tutorial introduction for working with ScalienDB from Python.

First make the Python client library:

$ make pythonlib
$ cp bin/python/* where_you_want_it/

Let's connect to the cluster assuming the controller is running on localhost:

import scaliendb
client = scaliendb.Client(["localhost:7080"]) # the endpoint of the controller(s)

Create a database:

db = client.create_database("test")

Or if the database already exists:

db = client.get_database("test") # returns None if the database does not exist

List all databases:

dbs = client.get_databases()
for db in dbs:
    print(db.name)

Delete a database:

db.delete_database()

Create a table:

table = db.create_table("test") 

Or if the table already exists:

table = db.get_table("test") # returns None if the table does not exist

List tables in a database:

tables = db.get_tables()
for table in tables:
    print(table.name)

Truncate a table:

table.truncate_table()

Delete a table:

table.delete_table()

Write and read back some data:

table.set("foo", "bar")
print(table.get("foo"))
table.delete("foo")
print(table.get("foo")) # prints None

Save objects:

# retrieve a unique id for comments
comment_ids = db.get_table("sequences").get_sequence("comment_ids")
# create dummy object
comment = {}
comment.comment_id = comment_ids.get()
comment.text = "Hello world"
comment.from = "user123"
# save it using the standard pickle module
db.get_table("comments").set(comment.comment_id, pickle.dumps(comment))

Read the object back later:

comment = pickle.loads(db.get_table("comments").get(comment_id))
print("%s: %s" % (comment.from, comment.text))

Create an index for the "from" field when saving object:

# ...
comment_ids = pickle.loads(db.get_table("comments_index_from").get(comment.from)) # assume it's a list
comment_ids.append(comment.comment_id)
db.get_table("comments_index_from").set(comment.from, pickle.dumps(comment_ids))

Load all comments from some user:

comment_ids = pickle.loads(db.get_table("comments_index_from").get(comment.from))
for comment_id in comment_ids:
    comment = db.get_table("comments").get(comment_id)
    print("%s: %s" % (comment.from, comment.text))

List all comments:

for key, value in db.get_table("comments").get_key_value_iterator():
    comment = pickle.loads(value)
    print("%s: %s" % (comment.from, comment.text))

For more, see: Advanced Python

Happy hacking!

Clone this wiki locally