Cassandra:将复合主键与batch_mutate java一起使用

时间:2015-02-15 11:58:19

标签: java cassandra

我是cassandra的新手,我尝试使用batch_mutate将数据输入cassandra。 因此,如果我有一个主键的表,所有工作

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'};

但是当我使用复合主键时,它会失败

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'};

作为代码引用,我使用了来自Apache Cassandra代码的压力。 这是一个简单的代码示例:

    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());

所以我明白我错过了什么,但我找不到任何好的例子,所以请帮助我。

1 个答案:

答案 0 :(得分:1)

请您将表格创建语句更正为

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

我能够使用CQL Batch和Datastax java驱动程序编写

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>