在C#中查询Cassandra

时间:2014-09-30 13:28:55

标签: c# cassandra cql

我在时间序列密钥空间中有以下表格:

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#查询这个表:

static long ninetySeventyTicks = new DateTime(1970, 1, 1).Ticks;

Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
ISession session = cluster.Connect("timeseries");

RowSet result  = session.Execute("SELECT symbol, ts FROM underlying_data WHERE ts > " 
                + ninetySeventyTicks.ToString() 
                + " and symbol = 'SPX'"
                //+ "AllowFiltering"
                + ";");

            rows = result.GetRows();

但我得到一个例外:

Message = "No indexed columns present in by-columns clause with Equal operator"

我从一本关于查询cassandra的书中复制了这个例子。唯一的区别是本书使用CQL而不是C#驱动程序。

这是否有效?若然,建议的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

老实说,二级索引的最佳策略是避免使用它们。

您可以通过将ts添加到PRIMARY KEY来轻松获得此查询。

PRIMARY KEY (symbol,ts)

此处,symbol是分区键,ts是群集键。这样您就可以指定symbol并根据ts的值过滤结果。