shura_7_2
shura_7_2

Reputation: 1

Cassandra: Using composite primary key with batch_mutate java

I am new in cassandra and I try to enter the data into the cassandra using batch_mutate. So if I have table with one primary key, all worked

CREATE TABLE l4_temp1 (
   id int,
   secidd int,
  val int,
  PRIMARY KEY ((id))
) WITH
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.100000 AND
  gc_grace_seconds=864000 AND
  index_interval=128 AND
  read_repair_chance=0.000000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  default_time_to_live=0 AND
  speculative_retry='99.0PERCENTILE' AND
  memtable_flush_period_in_ms=0 AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'LZ4Compressor'};

But when I use composite primary key, it fails

CREATE TABLE l4_temp1 (
  id int,
  secidd int,
  val int,
  PRIMARY KEY ((id),secid)
) WITH
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.100000 AND
  gc_grace_seconds=864000 AND
  index_interval=128 AND
  read_repair_chance=0.000000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  default_time_to_live=0 AND
  speculative_retry='99.0PERCENTILE' AND
  memtable_flush_period_in_ms=0 AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'LZ4Compressor'};

As the code reference I used the stress from Apache Cassandra code. This is a simple code example :

    List<Column> columns = new ArrayList<Column>(session.getColumnsPerKey());
    String [] arr = {"id","secid"};
    for (int i = 0; i < 2; i++)
    {
        columns.add(new Column(ByteBufferUtil.bytes(arr[i]))
                        .setValue(ByteBufferUtil.bytes(i))
                        .setTimestamp(FBUtilities.timestampMicros()));
    }


    Map<String, List<Mutation>> row =  getColumnsMutationMap(columns);
   Map<ByteBuffer, Map<String, List<Mutation>>> record = Collections.singletonMap(ByteBufferUtil.bytes(i), row);
            client.batch_mutate(record, session.getConsistencyLevel());

So I understand that I miss something, but I cannot find any good example, so please help me with this.

Upvotes: 0

Views: 765

Answers (1)

akshat thakar
akshat thakar

Reputation: 1526

Could you please correct Table create statement to

CREATE TABLE l4_temp1 (
id int,
secidd int,
val int,
PRIMARY KEY ((id),**secidd**)) 

I was able to write using CQL Batch and Datastax java driver

String query = "INSERT INTO test.l4_temp1 (id, secidd, val) VALUES (?, ?, ?)";

    Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1")
            .build();
    Session session = cluster.newSession();

    PreparedStatement stmt = session.prepare(query);

    Integer[][] columns = { { 1, 10, 200 }, { 2, 20, 2500 }, { 3, 20, 2567 },
            { 4, 30, 256 }, { 5, 40, 2432 } };

    for (int i = 0; i < columns.length; i++) {
        session.execute(stmt.bind(new Integer[] { columns[i][0], columns[i][1],
                columns[i][0] }));

    }
    session.close();

Maven Dependency
<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>2.1.0</version>
</dependency>

Upvotes: 1

Related Questions