2016-04-06 22 views
2

操作我有一個表結構卡桑德拉:與Apache 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"}'}]); 

我想選擇與類型=「頁面」命中計數。

我該怎麼辦?

回答

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'; 

請注意,上面的架構,不建議有超過100個百萬參照網址爲每對情侶(client_id,hit_type)