使用Java

时间:2018-02-23 07:18:27

标签: java cassandra

我在Cassandra的表结构:

CREATE TABLE test(
   id text,
   location text,
   status text,
   type text,
   duration double,
   threshold double,
   timestamp timestamp,
   segment text,
   PRIMARY KEY (id, location, timestamp)
) WITH CLUSTERING ORDER BY (location ASC, timestamp ASC)
AND bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';

我使用Java将数据插入此表,然后在尝试更新同一行的某些计算之后。这个过程是插入更新然后插入更新.........这里问题是当我尝试使用java进行更新,即com.datastax.driver.core.querybuilder.Update第一条记录更新成功,但它正在存储{ {1}}非更新字段的值也是,但是当我更新第二次插入记录时,它只更新必须更新的字段而不影响未更新的字段。

这是我的插入和更新代码

null

更新方法如下

String names[] = {
        "id ",
        "location",
        "status",
        "type",
        "duration",
        "threshold",
        "timestamp",
        "segment"
};

Object values[] = {
        obj.getId(),
        obj.getLocation(),
        obj.getStatus(),
        obj.gettype(),
        obj.getDuration(),
        obj.getThreshold(),
        obj.getTimestamp(),
        obj.getSegment(),
};

try{
    Insert insert = QueryBuilder.insertInto("test");
    insert.values( names, values );
    return session.execute( insert ).wasApplied();
} catch( Exception ex ) {
    logger.error( "Exception while inserting ", ex );
    return false;
}

当我第一次运行更新方法时,它会按预期存储try { Update update = QueryBuilder.update("test"); update.with( QueryBuilder.set("status", obj.getStatus() ) ); update.with( QueryBuilder.set("type", obj.getType() ) ); update.where( QueryBuilder.eq("id", obj.getId()) ) .and( QueryBuilder.eq("location", obj.getLocation() ) ) .and( QueryBuilder.eq("timestamp", obj.getTimestamp() ) ); return session.execute( update ).wasApplied(); } catch( Exception ex ) { logger.error( "Exception while updating", ex ); return false; } status值,但将其他typedurationthreshold作为{ {1}}。插入另一个segment后再次运行null查询,这次更新rowupdate而不会影响其他字段,即statustypeduration

1 个答案:

答案 0 :(得分:2)

session.execute( update.setForceNoValues(false) ).wasApplied()会有所帮助