Skip to content

Instantly share code, notes, and snippets.

@lukebakken
Last active December 5, 2015 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukebakken/ba14fc057d9ba36a27c5 to your computer and use it in GitHub Desktop.
Save lukebakken/ba14fc057d9ba36a27c5 to your computer and use it in GitHub Desktop.
Riak TS / Python examples
import datetime
from riak.client import RiakClient
epoch = datetime.datetime.utcfromtimestamp(0)
def unix_time_millis(dt):
td = dt - epoch
return int(td.total_seconds() * 1000.0)
tenMins = datetime.timedelta(0, 600)
now = datetime.datetime(2015, 1, 1, 12, 0, 0)
nowMS = unix_time_millis(now);
tenMinsAgo = now - tenMins
tenMinsAgoMS = unix_time_millis(tenMinsAgo);
tenMinsFromNow = now + tenMins
tenMinsFromNowMS = unix_time_millis(tenMinsFromNow);
# NB: modify 'host' and 'pb_port' to match your installation
client = RiakClient(host='myriakdb.host', pb_port=8087)
fmt = """
select * from GeoCheckin where
time > {t1} and time < {t2} and
myfamily = 'family1' and myseries = 'series1'
"""
query = fmt.format(t1=tenMinsAgoMS, t2=tenMinsFromNowMS)
ts_obj = client.ts_query('GeoCheckin', query)
print "Query result rows:", ts_obj.rows
import datetime
from riak.client import RiakClient
# NB: modify 'host' and 'pb_port' to match your installation
client = RiakClient(host='myriakdb.host', pb_port=8087)
fiveMins = datetime.timedelta(0, 300)
ts0 = datetime.datetime(2015, 1, 1, 12, 0, 0)
ts1 = ts0 + fiveMins
table = client.table('GeoCheckin')
rows = [
['family1', 'series1', ts0, 'hot', 23.5],
['family1', 'series1', ts1, 'windy', 19.8]
]
ts_obj = table.new(rows)
print "Store result:", ts_obj.store()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment