2
votes

I'm trying to select timestamps columns from Cassandra 2.0 using cqlengine or cql(python), and i'm getting wrong results.

This is what i get from cqlsh ( or thrift ): "2013-09-23 00:00:00-0700"

This is what i get from cqlengine and cql itself: "\x00\x00\x01AG\x0b\xd5\xe0"

If you wanna reproduce the error, try this:

  • open cqlsh
  • create table test (name varchar primary key, dt timestamp)
  • insert into table test ('Test', '2013-09-23 12:00') <<< Yes, i have tried to add by another ways....
  • select * from test ( Here it's everything fine )
  • Now go on cqlengine or cql itself and select that table and you will get a broken hexadecimal.

Thanks !

2
The insert statement should be insert into test (name, dt) VALUES ('Test', '2013-09-23 12:00'); - Richard

2 Answers

3
votes

Unfortunately, cqlengine is not currently compatible with cassandra 2.0

There were some new types introduced with Cassandra 2.0, and we haven't had a chance to make cqlengine compatible with them. I'm also aware of a problem with blob columns.

This particular issue is caused by the cql driver returning the timestamp as a raw string of bytes, as opposed to an integer.

Since cqlengine does not support Cassandra 2.0 yet, your best bet is to use Cassandra 1.2.x until we can get it updated, cqlengine doesn't support any of the new 2.0 features anyway. If you really need to use 2.0, you can work around this problem by subclassing the DateTime column like so:

class NewDateTime(DateTime):
    def to_python(self, val):
        if isinstance(val, basestring):
            val = struct.unpack('!Q', val)[0] / 1000.0
        return super(NewDateTime, self).to_python(val)
2
votes

The timestamp datatype stores values as the number of milliseconds since the epoch, in a long. It seems that however you are printing it is interpreting it as a string. This works for me using cql-dbapi2 after creating and inserting as in the question:

>>> import cql
>>> con = cql.connect('localhost', keyspace='ks', cql_version='3.0.0')
>>> cursor = con.cursor()
>>> cursor.execute('select * from test;')
True
>>> cursor.fetchone()
[u'Test', 1379934000.0]