我创建了一个像这样的cassandra数据库:
cqlsh:timeseries> describe keyspace timeseries;
CREATE KEYSPACE timeseries WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': '1'
};
USE timeseries;
CREATE TABLE option_data (
ts timestamp,
ask decimal,
bid decimal,
expiry timestamp,
id text,
strike decimal,
symbol text,
PRIMARY KEY ((ts))
) 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 underlying_data (
symbol text,
ask decimal,
bid decimal,
ts bigint,
PRIMARY KEY ((symbol))
) 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 INDEX underlying_data_ts_idx ON underlying_data (ts);
cqlsh:timeseries>
我有一个C#功能:
public void InsertUnderlying(long timestamp, string symbol, decimal bid, decimal ask)
{
var batchStmt = new BatchStatement();
var v2Insert = new SimpleStatement("insert into underlying_data " +
"(ts, symbol, bid, ask) values(?, ?, ?, ?);");
batchStmt.Add(v2Insert.Bind(timestamp, symbol, bid, ask));
session.Execute(batchStmt);
}
我实时调用此函数来添加数据。但是,当我从CQL执行查询时,
cqlsh:timeseries> select * from underlying_data;
我只看到一行,即使我多次调用此函数。不确定我如何附加数据而不是覆盖它?
答案 0 :(得分:3)
在Cassandra中,主键是唯一的。您的表格underlying_data
仅限于symbol
列:
PRIMARY KEY ((symbol))
这意味着特定符号的所有插入都将相互覆盖:
INSERT INTO underlying_data (symbol, ts, ask, bid) VALUES ('SPX',1412102636,3.1,4.0);
INSERT INTO underlying_data (symbol, ts, ask, bid) VALUES ('SPX',1412102708,3.0,4.4);
INSERT INTO underlying_data (symbol, ts, ask, bid) VALUES ('SPX',1412102731,2.1,5.0);
SELECT * FROM underlying_data;
symbol | ts | ask | bid
--------+------------+-----+-----
SPX | 1412102731 | 2.1 | 5.0
要存储每个INSERT
,请将ts
添加到主键定义中:
PRIMARY KEY (symbol, ts)
此外,Cassandra不区分INSERT
和UPDATE
(基本上是" UPSERT")。虽然语法不同,但它们都完成了同样的事情:存储特定键的列值。这意味着您可以使用UPDATE
插入新记录,并使用INSERT
更新现有记录。 Ike Walker有一篇很好的博客文章描述了这一点:How to do an Upsert in Cassandra。