Python, Cassandra and LongType

jbohman edited this page Sep 14, 2010 · 1 revision

Cassandra requires data stored as LongType to be exactly 8 bytes and in big endian byte order (see Cassandra FAQ). To achieve this in Python you can use the struct module, see example below:

    >>> import struct
    >>> a = struct.pack('>q', 1000)
    >>> a
    '\x00\x00\x00\x00\x00\x00\x03\xe8'
    >>> struct.unpack('>q', a)
    (1000,)

To clarify, ‘>’ is the modifier for big endian byte order, and ‘q’ is the format character for 8 bytes long.