Question

I know we can insert single record to Cassandra with something like this (the example below taken from here):

final String INSERT_STATEMENT = "INSERT INTO employees (empID, deptID, first_name, last_name) VALUES (?, ?, ?, ?);";

result = keyspace
    .prepareQuery(CQL3_CF)
    .withCql(INSERT_STATEMENT)
    .asPreparedStatement()
    .withIntegerValue(222)
    .withIntegerValue(333)
    .withStringValue("Eric")
    .withStringValue("Cartman")
    .execute();

Is it possible to do a batch insert (for multiple records) with Astyanax's cql3 API (like JDBC's executeBatch)?

NOTE: Using Astyanax's MutationBatch (which is based on Thrift, not CQL) doesn't seem to be an option to me, since I'm running in to a problem same as this one.

Was it helpful?

Solution

To acheive the following using Astyanax:

With cqlsh:

cqlsh:TEST_KS> INSERT INTO "MESSAGE_CF" (KEY, "DELETED_RECEIVER", "DELETED_SENDER", "SENDER") 
               VALUES ('user-1241324', 'Yes', 'No', 'user@mail.com');
cqlsh:TEST_KS> SELECT * FROM "MESSAGE_CF";                                                                   
 key          | DELETED_RECEIVER | DELETED_SENDER | RECEIVER | SENDER
--------------+------------------+----------------+----------+---------------
 user-1241324 |              Yes |             No |     null | user@mail.com

With Astyanax:

    Keyspace keyspace = Astyanax.getKeyspaceContext();
    ColumnFamily<String, String> mail = new ColumnFamily<String, String>(
        keyspace.getKeyspaceName(), // CF Name
        StringSerializer.get(),   // Key Serializer
        StringSerializer.get());  // Column Serializer

    // You could start looping here to alter what data is being inserted  
    // or make the method take in parameters and call it multiple times.         
    String  cqlStatement = 
            "INSERT INTO MESSAGE_CF (KEY, DELETED_RECEIVER, DELETED_SENDER, SENDER) "
            + "VALUES ('user-1281324', 'Yes', 'No', 'user@mail.com');";

    // execute the insertion
    OperationResult<CqlResult<String, String>> result = 
        keyspace.prepareQuery(mail).withCql(cqlStatement).execute();

   // stop looping

As a note: I couldn't achieve this with prepared statements, Astyanax have shown in their wiki (under prepared CQL) that prepared statements are supported, but I'm using astyanax-1.56.21 and the asPreparedStatement() function is missing.

Also, for this to work dont forget to set your AstyanaxContext to use CQL3.

 new com.netflix.astyanax.impl.AstyanaxConfigurationImpl()      
     .setCqlVersion("3.0.0")) //using CQL3 

UPDATE

Lookinto the batch keyword. The main factor of batch's speeding up abilities is that it saves round trips. It will be harder to manage the CQL statements but it does improve update speeds. It can do CUD operations (insert, update and delete) but it cannot do SELECTs. Also I'd advise you to read through the CQL3 documentation to learn what cql can do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top