使用Apache Cassandra列表进行操作

时间:2016-04-06 10:23:41

标签: cassandra

我在Cassandra有下一个表结构:

CREATE TABLE statistics (
  clientId VARCHAR,
  hits LIST<text>,
  PRIMARY KEY (clientId)
);

INSERT INTO statistics(clientId, hits) VALUES ('clientId', [{'referer': 'http://example.com/asd', 'type': 'PAGE', 'page': '{"title": "Page title"}'}, {'referer': 'http://example.com/dsa', 'type': 'EVENT', 'event': '{"title": "Click on big button"}'}, {'referer': 'http://example.com/fgd', 'type': 'PAGE', 'page': '{"title": "Page title second"}'}]);

我想选择类型=&#39; PAGE&#39;。

的点击次数

我该怎么做?

1 个答案:

答案 0 :(得分:3)

列表不是您使用案例的正确结构,请考虑以下架构

CREATE TABLE statistics(
  client_id VARCHAR,
  hit_type text,
  referer text,
  page text,
  event text,
  PRIMARY KEY ((client_id,hit_type), referer)
);

// Insert hits
INSERT INTO statistics(client_id, hit_type, referer, page)
VALUES('client1','PAGE', 'http://example.com/asd', '{"title": "Page title"}'); 

INSERT INTO statistics(client_id, hit_type, referer, event)
VALUES('client1','EVENT', 'http://example.com/dsa', '{"title": "Click on big button"}'); 

INSERT INTO statistics(client_id, hit_type, referer, page)
VALUES('client1','PAGE', 'http://example.com/fgd', '{"title": "Page title second"}');

//Select all hits for a given client and hit type:
SELECT * FROM statistics WHERE client_id='xxx' AND hit_type='PAGE';

请注意,使用上述架构,不建议为每对夫妇提供超过1亿的引用(client_id,hit_type)