Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I insert a map into Cassandra CQL3 column family by using Astyanax mutation batch? #494

Open
Glavoran-Nicaragua opened this issue Mar 12, 2014 · 0 comments

Comments

@Glavoran-Nicaragua
Copy link

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 TestColumn("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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant