python-3.xcassandrapaginationpython-bytearray

How to store PagingState in Cassandra Python driver?


I'm implementing a REST API which behind the scenes queries Cassandra (via Python driver) and returns the result. Now the items to be queried will be huge so I want to have pagination capability.

The ResultSet returned by execute() method has a property called paging_state which seems to be byte literal. I would like to return this in the response body with something like startKey=<PAGING_STATE_VALUE>. Currently, I see the paging state looking something like b'\x0cFY5D70822742\x00\xf0\x7f\xff\xff\xe6\x00'.

How should I decode it so that I can append it as a proper string in my API response body. I tried decoding with UTF-8, ASCII but they are failing.


Solution

  • The paging state by definition is binary, and won't be represented as a string.

    Just encode it as base64, quoted printable, uuencode, or hex string - for example by using built-in binascii module. There are plenty of functions there, like, hexlify to convert from bytes to hex string.