Question

I have a Cassandra CQL3 column family with the following structure

CREATE TABLE mytable(
  A text,
  B text,
  C text,
  mymap map<text,text>,
  D text,
  PRIMARY KEY (A,B,C)
);

I am trying to insert a bunch of data into it using Astyanax.

The version of Cassandra that I am working with is 1.2, so I can't use BATCH insert.

I know that I can run CQL3 commands in a for loop using Prepared Statements.

I wanted to know if it's possible to use Astyanax mutation batch to insert the data into the above column family? I realize that this will make use of the Astyanax Thrift interface to insert into a CQL3 column family but for the sake of performant writes, is this a viable option?

I took a look at the structure of the column family in cassandra-cli and it looks something like this

ColumnFamily: mytable
  Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type
  Default column value validator: org.apache.cassandra.db.marshal.BytesType
  Cells sorted by: org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.ColumnToCollectionType(6974656d73:org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.UTF8Type)))

While I can insert data into the other columns (i.e A, B, C, D) by creating a POJO with @Component on the various fields, I'm not sure how to go about dealing with the map insert i.e. inserting into the mymap column.

A sample POJO that I created is

public class TestColumn {

  @Component(ordinal = 0)
  String bComponent;

  @Component(ordinal = 1)
  String cComponent;

  @Component(ordinal = 2)
  String columnName;

  public TestColumn() {
  }
}

The insertion code is as follows

AnnotatedCompositeSerializer columnSerializer = new  AnnotatedCompositeSerializer(TestColumn.class);
ColumnFamily columnFamily = new ColumnFamily("mytable", StringSerializer.get(), columnSerializer);

final MutationBatch m = keyspace.prepareMutationBatch();

ColumnListMutation columnListMutation = m.withRow(columnFamily, "AVal");

columnListMutation.putColumn(new TestColumn("BVal", "CVal", null),  ByteBufferUtil.EMPTY_BYTE_BUFFER,
                timeToLive);
columnListMutation.putColumn(new ApiAvColumn("BVal", "CVal", "D"), "DVal",
                timeToLive);
m.execute;

How exactly should I modify the above code so that I can insert the map value as well?

Was it helpful?

Solution

We solved this by using the DataStax Java Driver instead of Astyanax.

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