Sayed Jalil Hassan
Sayed Jalil Hassan

Reputation: 2595

Insertion and retrieval of timestamp into Cassandra CQL3 based table in Python

I have the following table created using CQL3 in Cassandra:

    CREATE TABLE user_log (
        user_id text,
        zone_id text,
        freq int,
        time timestamp,
        PRIMARY KEY (user_id, zone_id)
    )

I insert some values into this table using datastax Cassandra-Python driver like this :

    fmt = '%m/%d/%Y %H:%M'
    t1 = datetime.strptime("01/01/2000 22:05", fmt)
    prepared = session.prepare("""
                    INSERT INTO user_log (user_id,zone_id,freq,time) VALUES (?,?,?,?)
                    """)
    session.execute_async(prepared.bind((str(Uid),str(zone_id),int(freq),t1 )))

Insertion goes fine but when I try to retrieve the values it throws an exception while retrieving the time values. Retrieval of all other columns works fine. Select * also throws exception.

Upvotes: 0

Views: 7603

Answers (2)

Matt Goldworm
Matt Goldworm

Reputation: 65

import json
import uuid

class UUIDEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, uuid.UUID):
            return obj.hex
        return json.JSONEncoder.default(self, obj)

j = json.dumps( invar, indent=4, separators=(',', ': '), cls=UUIDEncoder )

Upvotes: 1

John
John

Reputation: 1462

If you are having trouble inserting and retrieving timestamps in cassandra, you are not alone, see this: How to insert a datetime into a Cassandra 1.2 timestamp column

The solution might be to calculate the unix timestamp (the elapsed ms since epoch) from your datetime, and insert/retrieve this instead.

In general, it would help writing a helpful answer, if you exactly stated the error message (if any)? Also, what line of code exactly is causing the error?

Upvotes: 2

Related Questions