Are prepared statements supported with Azure Cosmos Cassandra API?

Question:

Are prepared statements supported with Azure Cosmos Cassandra API with Python?

It appears not – when I execute

stmt = session.prepare("SELECT provider FROM providers WHERE country_code=?")

I get the following exception:

Traceback (most recent call last):
  File "cosmos-cql.py", line 42, in <module>
    select_provider_stmt = session.prepare("SELECT provider FROM providers WHERE country_code=?")
  File "cassandracluster.py", line 3072, in cassandra.cluster.Session.prepare
  File "cassandracluster.py", line 3069, in cassandra.cluster.Session.prepare
  File "cassandracluster.py", line 4901, in cassandra.cluster.ResponseFuture.result
  File "cassandraconnection.py", line 1229, in cassandra.connection.Connection.process_msg
  File "cassandraprotocol.py", line 1196, in cassandra.protocol._ProtocolHandler.decode_message
  File "cassandraprotocol.py", line 744, in cassandra.protocol.ResultMessage.recv_body
  File "cassandraprotocol.py", line 734, in cassandra.protocol.ResultMessage.recv
  File "cassandraprotocol.py", line 775, in cassandra.protocol.ResultMessage.recv_results_prepared
  File "cassandraprotocol.py", line 819, in cassandra.protocol.ResultMessage.recv_prepared_metadata
  File "cassandraprotocol.py", line 1321, in cassandra.protocol.read_short
  File "C:UsersIan.condaenvsenerlyticslibsite-packagescassandramarshal.py", line 22, in <lambda>
    unpack = lambda s: packer.unpack(s)[0]
struct.error: unpack requires a buffer of 2 bytes

I’d normally expect to resolve this in a few minutes by googling, but I can find absolutely nothing about prepared statements and Cosmos Cassandra API. It’s as if either the problem doesn’t exist (I’m making some really silly mistake) or no one else has even thought to try it.

I am using version 3.25.1 of Datastax’s Cassandra driver.

Asked By: Ian Goldby

||

Answers:

Prepared statements are supported. However, there is a Python driver specific issue with prepared statements in versions higher than 3.20.2 when used with Cosmos DB API for Cassandra. If you downgrade to cassandra-driver==3.20.2, the prepared statement will work.

Answered By: Theo van Kraay

** This is not the prepared statement.

But, the below solution solves the purpose another way around.
I got it working by removing "?" with "%s".

session.execute("""INSERT INTO test.profile (id, depth, create_ts, create_user)  VALUES (%s, %s, %s, %s)""", (id, depth, create_ts, create_user))

with the above, you can use batch as well.

batch.add("""INSERT INTO test.profile (id, depth, create_ts, create_user)  VALUES (%s, %s, %s, %s)""", (id, depth, create_ts, create_user))

Hope it’s helpful for someone.

Answered By: Anant