Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2016 01:21
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 anonymous/a4a4ccb8617a00d38fb47a6b11571d81 to your computer and use it in GitHub Desktop.
Save anonymous/a4a4ccb8617a00d38fb47a6b11571d81 to your computer and use it in GitHub Desktop.
PRECISION = 1000.0
def toUnixTime(dt):
dt1 = datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S.%fZ')
td = dt1 - datetime.fromtimestamp(0)
return int(td.total_seconds() * PRECISION)
def fromUnixTime(dt):
return datetime.fromtimestamp(dt/PRECISION).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
tables = [("testA","QUANTUM(time, 6, 'h')"), ("testB","QUANTUM(time, 12, 'd')")]
for eachTable in tables:
query = "CREATE TABLE {0} (id VARCHAR NOT NULL, time TIMESTAMP NOT NULL, data VARCHAR NOT NULL, PRIMARY KEY ((id, {1}),id, time))".format(eachTable[0], eachTable[1])
try:
client.ts_query(eachTable[0], query)
print("Table '{0}' created successfully".format(eachTable[0]))
except Exception as e:
print(e)
for eachTable in tables:
try:
description = client.table(eachTable[0]).describe()
print('-------')
for column_desc in description.rows:
print(column_desc)
except Exception as e:
print(e)
pass
>Table 'testA' created successfully
>Table 'testB' created successfully
-------
['assetid', 'varchar', False, 1, 1, None, None]
['time', 'timestamp', False, 2, 2, 6, 'h']
['data', 'varchar', False, None, None, None, None]
-------
['assetid', 'varchar', False, 1, 1, None, None]
['time', 'timestamp', False, 2, 2, 120, 'd']
['data', 'varchar', False, None, None, None, None]
# create an exact interval of 5 * quantum
# 6hrs * 5 = 30 hours
startTime = toUnixTime('2016-01-01T00:00:00.000Z')
endTime = startTime + (1000*60*60*6*5)
print(fromUnixTime(endTime))
> 2016-01-02T06:00:00.000000Z
# 12 days * 5 = 60 days
startTime2 = toUnixTime('2016-01-01T00:00:00.000Z')
endTime2 = startTime2 + (1000*60*60*24*12*5)
print(fromUnixTime(endTime2))
> 2016-03-01T00:00:00.000000Z
fmt = "Select data from testA where id = 'junk' and time >= {0} and time < {1}".format(startTime, endTime)
fmt2 = "Select data from testB where id = 'junk' and time >= {0} and time < {1}".format(startTime2, endTime2)
ts_obj = client.ts_query('testA', fmt)
print(ts_obj.rows)
> []
ts_obj2 = client.ts_query('testB', fmt2)
print(ts_obj2.rows)
> riak.riak_error.RiakError: 'Too many subqueries (6)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment