Updated Python GridDB API

Earlier this year, an updated Python Client for GridDB was released (0.7.0) which provides an improved API. This blog post covers the basics of connecting, querying, writing records, and other common operations.

Installing

The Quickstart Blog has Python installation instructions.

Connecting

Connecting to GridDB is a single call that takes a list of parameters. If you’re using GridDB in fixed list mode, specify notification_member instead of host and port.

self.gridstore = factory.get_store(
    host="239.0.0.1",
    port="31999",
    cluster_name="defaultCluster",
    username="admin",
    password="admin"
)

Querying

To query GridDB, you first open the container you wish to query, specify and execute the query, and then fetch and iterate through the results. Specifying timestamp_output_with_float will make GridDB not convert TIMESTAMP fields into Python datetime objects improving performance significantly.

try:
    cn = gridstore.get_container("DEVICE_READS");
    query = cn.query("select *")
    rs = query.fetch(False)
    rs.timestamp_output_with_float = True
except:
    return []
retval= []
while rs.has_next():
    data = rs.next()
    retval.append(data)

Writing

To write to GridDB, you specify the container schema as an array of fields and their type and then open the container with put_container. Data is written using put or multi_put calls.

conInfo = griddb.ContainerInfo("DEVICE_READS",
    [["ts", griddb.Type.TIMESTAMP],
    ["attr", griddb.Type.STRING],
    ["value", griddb.Type.LONG]],
    griddb.ContainerType.TIME_SERIES, True)
ts = gridstore.put_container(conInfo)
ts.set_auto_commit(False)
ts.put([int(datetime.datetime.now().timestamp()*1000), "foo", 42.0])
ts.commit()

Updating

Updates combine queries and writes. Open a container using a ContainerInfo object, query and iterate through the results which will update and commit the updated rows. The parameter of query_fetch should be True, this allows GridDB to update the result set.

conInfo = griddb.ContainerInfo("DEVICE_READS",
    [["ts", griddb.Type.TIMESTAMP],
    ["attr", griddb.Type.STRING],
    ["value", griddb.Type.LONG]],
    griddb.ContainerType.TIME_SERIES, True)
ts = gridstore.put_container(conInfo)
ts.set_auto_commit(False)
query = ts.query("select * where ts < TO_TIMESTAMP_MS("+str(epochTs)+")")
rs = query.fetch(True)
if rs.has_next():
    data = idx_rs.next()
    rs.update([data[0], "old_"+data[1], data[2]])
    rs.commit()

Deleting

Deleting a row in the container queried is very similar to the update process; the main difference being calling remove with the row key as a parameter instead.

conInfo = griddb.ContainerInfo("DEVICE_READS",
    [["ts", griddb.Type.TIMESTAMP],
    ["attr", griddb.Type.STRING],
    ["value", griddb.Type.LONG]],
    griddb.ContainerType.TIME_SERIES, True)
ts = sgridstore.put_container(conInfo)
ts.set_auto_commit(False)
query = ts.query("select * where attr = '"+specifiedAttr+"'")
rs = query.fetch(True)
if rs.has_next():
    data = idx_rs.next()
    rs.remove(data[0])
    rs.commit()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.