Question

I am writing some python code that will collect data over time. I need to store this in Cassandra. I have spent my entire day on this but cannot find something that works.

CREATE TABLE timearchive 
  (name_yymmddhh text, name text, ip text, time_current timestamp, data blob, 
   PRIMARY KEY (name_yymmddhh, time_current));

I can create the table, but I am having trouble inserting various data (time_current timestamp, data blob). I cannot format it correctly. I plan to have rows broken by hour (data size should be fine in my use case) and columns with each data entry (2-3/min).

Here is my code to insert. It works if I change the format of timestamp/blob to int/text.

query = """INSERT INTO timearchive
            (name_yymmddhh, name, ip, time_current, data)
            VALUES (:name_yymmddhh, :name, :ip, :time_current, :data)"""
values = {'name_yymmddhh':rowkey,
          'name': dcname,
          'ip': ip,
          'time_current': timenow,
          'data': my_blob}
cursor.execute(query, values)

Questions:

1) How do I make a cql timestamp in python: timenow?
This did not help (too complicated for my Cassandra level): Cassandra 1.2 inserting/updating a blob column type using Python and the cql library

2) My data is a dict. It will be a large dict of dicts and other data. (I found various discussions but nothing worked. It seems there was some update ~6months ago but no simple examples: https://github.com/datastax/python-driver/pull/39)
How do I put:

my_dict = {'one': 1, 'two': 2, 'three': 3}  
 ...  
my_blob = ???
Was it helpful?

Solution

Solved. Needed to use the latest Datastax drivers for blob, and the above INSERT method (not string converted), and proper pickle and bytearray.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top