Error : "Mismatched position did not write cluster key" seen with Azure Cosmos. Same code works on local Cassandra cluster. RRS feed

  • Question

  • I'm trying to insert multiple records into a table using JSON format.

    	userid INT
    	,name TEXT
    	,email TEXT
    	,PRIMARY KEY (userid)

    It works well using a direct query instead of a prepared and bound statement. Both records get inserted using following code.

                // Test 1 - without prepared statement
                session.execute("INSERT INTO test.student JSON '{\"userid\": 1, \"name\":\"Mark\", \"email\":\"mark@test.com\"}'");
                session.execute("INSERT INTO test.student JSON '{\"userid\": 2, \"name\":\"Phil\", \"email\":\"phil@test.com\"}'");
                System.out.println("## Test WITHOUT PreparedStatement passed.");

    But if I try doing the same using prepared and bound statement, first record gets inserted, but second one fails.

                // Test 2 - with prepared statement
                String record1 = "{\"userid\": 3, \"name\":\"Chelsea\", \"email\":\"chelsea@test.com\"}";
                String record2 = "{\"userid\": 4, \"name\":\"John\", \"email\":\"john@test.com\"}";
                // Insert 1st record
                PreparedStatement prepared = session.prepare("INSERT INTO test.student JSON ?");
                BoundStatement bound = prepared.bind(record1);
                System.out.println("## First record inserted.");
                // Insert 2nd record
                bound = prepared.bind(record2);
                System.out.println("## Second record inserted.");
                System.out.println("## Test USING PreparedStatement passed.");
    Expected output :
    ## First record inserted.
    ## Second record inserted.
    ## Test USING PreparedStatement passed.

    Result :
    ## First record inserted.
    (Mismatched position did not write cluster key at position 1 but found cluster key at position 0), defuncting connection.

    Note : The same code works without errors in my local Cassandra cluster, but fails in Azure Cosmos.

    Friday, December 6, 2019 3:36 PM

All replies

  • Additional note. I'm using v3.8.0 of the Cassandra Java driver.


    As per MSFT documentation v3.5+ drivers are supported.

    Friday, December 6, 2019 4:58 PM
  • Hi Nikhil,

    Per the following from the Frequently Asked Questions about different APIs in Azure Cosmos DB (link).

    Can an on-premises Apache Cassandra cluster be paired with Azure Cosmos DB's Cassandra API?

    At present Azure Cosmos DB has an optimized experience for cloud environment without overhead of operations. If you require pairing, send mail to askcosmosdbcassandra@microsoft.com with a description of your scenario. We are working on offering to help pair the on-premises/different cloud Cassandra cluster to Cosomos DB's Cassandra API.

    Please provide your instance name and Azure subscription ID via email and they will be able to respond to you more specifically with regard to cluster key support. If you are not receiving any feedback, please feel free to reach me directly at AzCommunity.



    Monday, December 9, 2019 10:38 PM