0

Problem Statement:-

I am trying to insert data into Cassandra database. I am using Netflix/Astyanax client for this.

Below is the code which will upsert data into Cassandra database using Astyanax client.

In my below method, it accepts two parameter. One is the userId which I will be using as the RowKey and attributes map which will contain, column name as the key and it's column value as the value in the map.

Now how can I use Astyanax client to upsert data into Cassandra database using the below method which accepts those two parameters?

I am mainly concern about attributes Map. How should I use that map to upsert data into Cassandra database?

/**
 * Performs an upsert of the specified attributes for the specified id.
 */
public void upsertAttributes(final String userId, final Map<String, String> attributes) {

    MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();

    m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), userId)
    .putColumn(attributeName from attributesMap, attributeValue from attributesMap, null)
    ;

    try {
        m.execute();
    } catch (ConnectionException e) {
        StringBuilder message = new StringBuilder();
        message.append("Failed to read from C* = '").append(e).append("'. ");

        LOG.error("Failed to write data to C*", e);

        throw new RuntimeException("failed to write data to C*", e);
    }
}
arsenal
  • 23,366
  • 85
  • 225
  • 331

1 Answers1

2

You have almost given the solution, but i think its not clear to you

 MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
 ColumnListMutation<String> clm = m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), userId);
 for(String key: attributeMap.keySet()){
     clm.putColumn(key, attributeMap.get(key), null);
 }

try {
    m.execute();
} catch (ConnectionException e) {
    e.printStackTrace();
}

Basically putColumn has different overloaded variations, so it will support all the datatypes supported by cassandra.

abhi
  • 4,762
  • 4
  • 29
  • 49
  • Thanks abhi. That thing, I already know whatever code you posted out above. My main challenge is to get the `col1` from the `attributes Map` and `col1 value` from the same `attributesMap`. If you go through my question again, you will see what I am trying to say. – arsenal Apr 19 '13 at 04:19
  • ,Basically in my attributes Map, I will have column name and corresponding column value. So I need to get the column names and column values from those map. – arsenal Apr 19 '13 at 04:25
  • Thanks. It worked. I guess, you are the only Cassandra folks here out. :). I have two more questions related to Cassandra open. And nobody has answered that yet. These two are related to Datastax API (new Binary protocol) [post](http://stackoverflow.com/questions/16096009/creating-column-family-or-table-in-cassandra-while-working-datastax-apiwhich-us) and [post](http://stackoverflow.com/questions/16096207/upsert-read-into-from-cassandra-database-using-datastax-api-using-new-binary-pr). If you have some time, take a look into that as well. Thanks a lot for the help. – arsenal Apr 19 '13 at 05:50