繁体   English   中英

使用Java在Cassandra中更新数据的问题

[英]Issue with Updating data in Cassandra using Java

我在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首先记录更新成功,但是它是也为未更新的字段存储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;
}

当我第一次运行update方法时,它将按预期存储statustype值,但将其他durationthresholdsegmentnull 在插入另一row然后再次运行update查询之后,这次是在不影响其他字段(例如durationthresholdsegment情况下更新statustype

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM