2013-04-04 77 views
2

我是cassandra的新手。我不知道多次添加行中的一组列,例如,我想添加与调用有关的信息列(像timestamp_calling_no,timestamp_tower_id,timestamp_start_time,timestamp_end_time,timestamp_duration,timestamp_call_type等)在一行中,每当相同的手机号码使用hector/astyanax/java/CQL拨打电话。在cassandra中添加一列的行

请给出您的建议。提前Thanx。

回答

1

向行中添加列是一项廉价操作,而且还有一件事情是cassandra将列名作为排序存储,所以通过使用timestamp作为列名将解决切片问题.cassandra可以拥有20亿CDR CF中的一行中的列,因此我们可以轻松地添加列。 你试图运行一个查询需要cassandra扫描所有行然后是的,它会表现不佳。

+0

感谢您的回答.... – user2243225 2013-04-12 09:03:11

1

很高兴您认识到有很多API可用。我建议使用CQL3,主要是因为节俭apis现在只能用于向后兼容,并且CQL可以用于大多数语言,而Astyanaxhector是java专用的。

我想用compound key(在“集群,复合键等等”下面)。

//An example keyspace using CQL2 
cqlsh> 
CREATE KEYSPACE calls WITH strategy_class = 'SimpleStrategy' 
AND strategy_options:replication_factor = 1; 

//And next create a CQL3 table with a compound key 
//Compound key is formed from the number and call's start time 
cqlsh> 
CREATE TABLE calls.calldata (
     number text, 
     timestamp_start_time timestamp, 
     timestamp_end_time timestamp, 
     PRIMARY KEY (number, timestamp_start_time) 
    ) WITH COMPACT STORAGE 

上面的架构将允许你插入含有相同数量的键多次,但由于呼叫开始是关键的组成部分,这将确保该组合创建一个唯一的密钥每一个行时间。

下一页插入使用CQL3一些测试数据(对于课程的例子的目的)

cqlsh> //This example data below uses 2 diffrent numbers 
insert into calls.calldata (number, timestamp_start_time, timestamp_end_time) 
values ('+441234567890', 1335361733545850, 1335361773545850); 
insert into calls.calldata (number, timestamp_start_time, timestamp_end_time) 
values ('+440987654321', 1335361734678700, 1335361737678700); 
insert into calls.calldata (number, timestamp_start_time, timestamp_end_time) 
values ('+441234567890', 1335361738208700, 1335361738900032); 
insert into calls.calldata (number, timestamp_start_time, timestamp_end_time) 
values ('+441234567890', 1335361740100277, 1335361740131251); 
insert into calls.calldata (number, timestamp_start_time, timestamp_end_time) 
values ('+440987654321', 1335361740176666, 1335361740213000); 

现在我们可以retreive所有的数据(再次使用CQL3):

cqlsh> SELECT * FROM calls.calldata; 

number  | timestamp_start_time  | timestamp_end_time 
---------------+---------------------------+--------------------------- 
+440987654321 | 44285-12-05 15:11:18+0000 | 44285-12-05 16:01:18+0000 
+440987654321 | 44285-12-05 16:42:56+0000 | 44285-12-05 16:43:33+0000 
+441234567890 | 44285-12-05 14:52:25+0000 | 44285-12-06 01:59:05+0000 
+441234567890 | 44285-12-05 16:10:08+0000 | 44285-12-05 16:21:40+0000 
+441234567890 | 44285-12-05 16:41:40+0000 | 44285-12-05 16:42:11+0000 

或者是数据的一部分。由于复合键的使用,你可以使用CQL3检索特定号码的所有行:

cqlsh> SELECT * FROM calls.calldata WHERE number='+441234567890';  

number  | timestamp_start_time  | timestamp_end_time 
---------------+---------------------------+--------------------------- 
+441234567890 | 44285-12-05 14:52:25+0000 | 44285-12-06 01:59:05+0000 
+441234567890 | 44285-12-05 16:10:08+0000 | 44285-12-05 16:21:40+0000 
+441234567890 | 44285-12-05 16:41:40+0000 | 44285-12-05 16:42:11+0000 

如果你想成为真正具体的,您可以检索在呼叫开始在特定时间特定号码(再次感谢复合键)

cqlsh> SELECT * FROM calls.calldata WHERE number='+441234567890' 
      and timestamp_start_time=1335361733545850; 

number  | timestamp_start_time  | timestamp_end_time 
---------------+---------------------------+--------------------------- 
+441234567890 | 44285-12-05 14:52:25+0000 | 44285-12-06 01:59:05+0000 
+0

感谢您的详细解决方案,但我想存储所有通过同一个人在同一行中的呼叫数据,因为Cassandra是一个水平高效的数据库。我们应该更喜欢添加列而不是添加行。我的解决方案中的主要挑战是切割色谱柱。 – user2243225 2013-04-12 07:29:06