这听起来可能会变得很大,所以我将采用第一个并逐步介绍如何处理它。你没有必须这样做,这只是一种方法。请注意,您可能必须为上述 4 个场景中的每一个创建查询表。此表仅适用于第一种情况。
首先,我将为发布者地址创建一个类型。
CREATE TYPE address (
street text,
city text,
state text,
postalCode text
);
接下来我将创建一个名为booksByPublisher 的表。我将使用我的address 类型作为publisherAddress。我将使用publisherid 作为分区键构建我的主键,并在bookYear 和isbn 上进行聚类。
由于您希望能够查询特定出版商的所有书籍,因此将其指定为分区键是有意义的。将结果按年份排序可能会很有帮助,或者至少能够查看特定出版商的特定年份,因此我将 bookYear 作为第一个聚类键。当然,要为出版商中的每本书创建一个唯一的 CQL 行,我将添加 isbn 以保持唯一性。
CREATE TABLE booksByPublisher (
publisherid UUID,
publisherName text,
publisherAddress frozen<address>,
publisherPhoneNo text,
bookName text,
isbn text,
bookYear bigint,
bookCost bigint,
bookAuthor text,
PRIMARY KEY (publisherid, bookYear, isbn)
);
INSERT INTO booksByPublisher (publisherid, publishername, publisheraddress, publisherphoneno, bookname, isbn, bookyear, bookcost, bookauthor)
VALUES (b7b99ee9-f495-444b-b849-6cea82683d0b,'Crown Publishing',{ street: '1745 Broadway', city: 'New York', state:'NY', postalcode: '10019'},'212-782-9000','Ready Player One','978-0307887443',2005,812,'Ernest Cline');
INSERT INTO booksByPublisher (publisherid, publishername, publisheraddress, publisherphoneno, bookname, isbn, bookyear, bookcost, bookauthor)
VALUES (b7b99ee9-f495-444b-b849-6cea82683d0b,'Crown Publishing',{ street: '1745 Broadway', city: 'New York', state:'NY', postalcode: '10019'},'212-782-9000','Armada','978-0804137256',2015,1560,'Ernest Cline');
INSERT INTO booksByPublisher (publisherid, publishername, publisheraddress, publisherphoneno, bookname, isbn, bookyear, bookcost, bookauthor)
VALUES (uuid(),'The Berkley Publishing Group',{ street: '375 Hudson Street', city: 'New York', state:'NY', postalcode: '10014'},'212-333-2354','Rainbox Six','978-0425170342',1999,867,'Tom Clancy');
现在我可以像这样查询 Crown Publishing (publisherid=b7b99ee9-f495-444b-b849-6cea82683d0b) 出版的所有书籍(在我的 3 行中):
aploetz@cqlsh:stackoverflow2> SELECT * FROM booksbypublisher
WHERE publisherid=b7b99ee9-f495-444b-b849-6cea82683d0b;
publisherid | bookyear | isbn | bookauthor | bookcost | bookname | publisheraddress | publishername | publisherphoneno
--------------------------------------+----------+----------------+--------------+----------+------------------+-------------------------------------------------------------------------------+------------------+------------------
b7b99ee9-f495-444b-b849-6cea82683d0b | 2005 | 978-0307887443 | Ernest Cline | 812 | Ready Player One | {street: '1745 Broadway', city: 'New York', state: 'NY', postalcode: '10019'} | Crown Publishing | 212-782-9000
b7b99ee9-f495-444b-b849-6cea82683d0b | 2015 | 978-0804137256 | Ernest Cline | 1560 | Armada | {street: '1745 Broadway', city: 'New York', state: 'NY', postalcode: '10019'} | Crown Publishing | 212-782-9000
(2 rows)
如果我愿意,我也可以查询皇冠出版社在 2015 年的所有书籍:
aploetz@cqlsh:stackoverflow2> SELECT * FROM booksbypublisher
WHERE publisherid=b7b99ee9-f495-444b-b849-6cea82683d0b AND bookyear=2015;
publisherid | bookyear | isbn | bookauthor | bookcost | bookname | publisheraddress | publishername | publisherphoneno
--------------------------------------+----------+----------------+--------------+----------+----------+-------------------------------------------------------------------------------+------------------+------------------
b7b99ee9-f495-444b-b849-6cea82683d0b | 2015 | 978-0804137256 | Ernest Cline | 1560 | Armada | {street: '1745 Broadway', city: 'New York', state: 'NY', postalcode: '10019'} | Crown Publishing | 212-782-9000
(1 rows)
但我不能仅通过bookyear查询:
aploetz@cqlsh:stackoverflow2> SELECT * FROM booksbypublisher WHERE bookyear=2015;
InvalidRequest: code=2200 [Invalid query] message="Cannot execute this query as it might
involve data filtering and thus may have unpredictable performance. If you want to execute
this query despite the performance unpredictability, use ALLOW FILTERING"
并且不要听错误信息并添加ALLOW FILTERING。对于 3 行(甚至 300 行)的表,这可能工作得很好。但它不适用于具有 300 万行的表(您会超时)。当您通过完整的分区键查询时,Cassandra 效果最佳。因为publisherid 是我们的分区键,所以这个查询会执行得很好。但是如果你需要通过bookYear查询,那么你应该创建一个使用bookYear作为其分区键的表。