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\":\"\"}'");
                session.execute("INSERT INTO test.student JSON '{\"userid\": 2, \"name\":\"Phil\", \"email\":\"\"}'");
                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\":\"\"}";
                String record2 = "{\"userid\": 4, \"name\":\"John\", \"email\":\"\"}";
                // 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