【问题标题】:Issue with Updating data in Cassandra using Java使用 Java 在 Cassandra 中更新数据的问题
【发布时间】:2018-08-03 05:21:27
【问题描述】:

我在 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;
}

当我第一次运行更新方法时,它按预期存储statustype 值,但将其他durationthresholdsegment 设置为null。在插入另一个row 之后再次运行update 查询,这次更新statustype 而不影响其他字段,即durationthresholdsegment

【问题讨论】:

    标签: java cassandra


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2014-04-05
    • 2018-01-24
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    相关资源
    最近更新 更多