How do I insert Date/Time/Datetime type of data in Cassandra through python ORM?

215 Views Asked by At

I have a table with primary keys all set, but I want to run query for data within a time range, so I add column date_query that store the same data as pk date

from cassandra.cqlengine.columns import Text,Date,DateTime
from cassandra.cqlengine.models import Model

class MyTable(Model):
    my_partition_key = Text(primary_key=True, partition_key=True)
    date = Text(primary_key=True)  # '2023-02-06'
    date_query = Date()            # '2023-02-06'
    time_query = DateTime()        # '2023-02-06 21:45:00'

I tried to transform the data in column date_query into datetime using datetime.strptime(), but still it would raise errors that said TypeError: Object of type Timestamp is not JSON serializable

I wish there is a good way to prepare data using python pandas, though I know simply typing strings in CQL would insert the data, but let's pass that.

1

There are 1 best solutions below

0
On

The date type is a 32-bit unsigned integer representing the number of days since Unix epoch (1 Jan 1970). You can either specify the date as an unsigned integer, or as a string of the format YYYY-MM-DD.

The time type is a 64-bit signed integer representing the number of nanoseconds since midnight. You can either specify the time as a long integer, or as a string of the formats hh:mm:ss, hh:mm:ss.123, hh:mm:ss.123456, hh:mm:ss.123456789.

I'm not really sure what you're trying to achieve and a sample minimal code would have helped in this instance but instead of strptime(), try converting it with:

datetime.strftime('%Y-%m-%d %H:%M:%S')