如何计算多行中的列

时间:2013-06-09 12:04:20

标签: cassandra hector

如何计算列族不同行中的列数?

我是卡桑德拉的新手。我不知道一个起点。我唯一的选择是让应用程序一次获取每行的数据。这对我来说听起来不对。我正在使用Hector连接到Cassandra。

2 个答案:

答案 0 :(得分:1)

这就是如何获得特定rowkey的总列数

sliceQuery.setColumnFamily("**your column family**");
sliceQuery.setKey("**your row key**");
sliceQuery.setRange(null, null, false, Integer.MAX_VALUE);

QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
ColumnSlice<String, String> cs = result.get();

long noOfColumnInRowKey=result.get().getColumns().size();

答案 1 :(得分:0)

假设您有宽行(让我们使用CLI创建它)

create column family cf3 
 with column_type = 'Standard' and 
 comparator = 'TimeUUIDType' and 
 key_validation_class = 'UTF8Type' and 
 default_validation_class = 'UTF8Type';

这是我在CQL3中看到的:

cqlsh:ks> desc table cf3;

CREATE TABLE cf3 (
  key text,
  column1 timeuuid,
  value text,
  PRIMARY KEY (key, column1)
) WITH COMPACT STORAGE AND
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.000000 AND
  gc_grace_seconds=864000 AND
  read_repair_chance=0.100000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'SnappyCompressor'};

我从CQL3中插入了一些值,这让你觉得自己很棒'MySQL

cqlsh:ks> insert into cf3 (key, column1, value) values ('user1', now(), 'time5');
cqlsh:ks> select * from cf3;

 key   | column1                              | value
-------+--------------------------------------+-------
 user1 | f0c687b0-d114-11e2-8002-2f4261da0d90 | time1
 user1 | fb9fa130-d114-11e2-8002-2f4261da0d90 | time2
 user1 | 09512f10-d115-11e2-8002-2f4261da0d90 | time3
 user1 | 0f5c93e0-d115-11e2-8002-2f4261da0d90 | time4
 user1 | 21155220-d115-11e2-8002-2f4261da0d90 | time5

但这是你的广泛行(从CLI看到)

[default@ks] list cf3;
Using default limit of 100
Using default column limit of 100
-------------------
RowKey: user1
=> (column=f0c687b0-d114-11e2-8002-2f4261da0d90, value=time1, timestamp=1370789864363000)
=> (column=fb9fa130-d114-11e2-8002-2f4261da0d90, value=time2, timestamp=1370789882563000)
=> (column=09512f10-d115-11e2-8002-2f4261da0d90, value=time3, timestamp=1370789905537000)
=> (column=0f5c93e0-d115-11e2-8002-2f4261da0d90, value=time4, timestamp=1370789915678000)
=> (column=21155220-d115-11e2-8002-2f4261da0d90, value=time5, timestamp=1370789945410000)

1 Row Returned.
Elapsed time: 105 msec(s).

现在,您想要计算从给定时间开始的列数。对?这是CQL3。

cqlsh:ks> select count(*) from cf3 where key = 'user1' and column1 >= 09512f10-d115-11e2-8002-2f4261da0d90 ;

 count
-------
     3

现在,我有点怀疑是什么。但是,我的直觉说实际上所有列都在协调节点获取并在内存中计数。这可能与您计划在客户端计算机上手动进行的操作有些类似。

另外,我不知道cassandra-cli是否提供了这样的功能,但是你提到你正在使用Hector。因此,您可以使用get_countCountQuery之类提及的here,但null除了范围完成和大计数值。像这样:

CountQuery<String, String> cq = HFactory.createCountQuery(keyspace, StringSerializer.get(), TimeUUIDSerializer.get());
cq.setColumnFamily(cf).setKey("user1");
cq.setRange(timestamp, null, Integer.MAX_VALUE);
QueryResult<Integer> r = cq.execute();

(上面未编译的代码)

HTH


旧答案:

请参阅Hector documentation

CQL:

CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le);
cqlQuery.setQuery("SELECT COUNT(*) FROM StandardLong1 WHERE KEY = 'cqlQueryTest_key1'");
QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute();
assertEquals(2, result.get().getAsCount());

您可能只是错过 WHERE条件并使用LIMIT来解决您的目的。