Cassandra开发入门文档第三部分(非规范化关系结构、批处理)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Cassandra开发入门文档第三部分(非规范化关系结构、批处理),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8992字,纯文字阅读大概需要13分钟。
内容图文
![Cassandra开发入门文档第三部分(非规范化关系结构、批处理)](/upload/InfoBanner/zyjiaocheng/1327/caef68de730e4fcaa3c988292a342cdf.jpg)
非规范化关系结构
第二部分我们讲了复合主键,这可以灵活的解决主从关系,也即是一对多关系,那么多对多关系呢?
多对多关系的数据模型应该回答两个问题:
- 我跟着谁?
- 谁跟着我?
-- 建表,我们发现这里有个不寻常的事情,表都有2个列,都是主键。2个表,一个出站,一个入站 CREATE TABLE "user_outbound_follows" ( "follower_username" text , "followed_username" text , PRIMARY KEY ("follower_username", "followed_username") ); CREATE TABLE "user_inbound_follows" ( "followed_username" text , "follower_username" text , PRIMARY KEY ("followed_username", "follower_username") ); -- 新建数据 INSERT INTO "user_outbound_follows" ("follower_username", "followed_username") VALUES (‘alice‘, ‘bob‘); INSERTINTO "user_inbound_follows" ("followed_username", "follower_username") VALUES (‘bob‘, ‘alice‘); INSERTINTO "user_outbound_follows" ("follower_username", "followed_username") VALUES (‘alice‘, ‘carol‘); INSERTINTO "user_inbound_follows" ("followed_username", "follower_username") VALUES (‘carol‘, ‘alice‘); SELECT "followed_username" FROM "user_outbound_follows" WHERE "follower_username" =‘alice‘; -- 将会报错,followed_username不是分区列,簇列精确查询将报错SELECT "follower_username" FROM "user_outbound_follows" WHERE "followed_username" =‘bob‘; -- 正常,在入站表中followed_username是分区列SELECT "follower_username" FROM "user_inbound_follows" WHERE "followed_username" =‘bob‘; DELETEFROM "user_outbound_follows" WHERE "follower_username" =‘alice‘AND "followed_username" =‘bob‘; DELETEFROM "user_inbound_follows" WHERE "followed_username" =‘bob‘AND "follower_username" =‘alice‘; SELECT "followed_username" FROM "user_outbound_follows" WHERE "follower_username" =‘alice‘;
非规范化数据结构
上面我们看到的第一个非规范化的例子,这是在多个地方存储相同数据的实践。在关系数据库模式中,非规范化通常是不受欢迎的,尽管从实际角度来看,即使在这种情况下,非规范化也常常是一种有用的优化。在非关系数据库中,非规范化通常是查询驱动设计中的一个关键工具。
非规范化的缺点体现在前面的插入模式上:我们必须做两个插入语句才能完全表示一个基本事实(多对多跟随)。从性能的角度来看,这是可以接受的:Cassandra针对高效的写操作进行了优化,因此我们很高兴进行详细的写操作,以便允许高效的读取。当然,这会在应用程序级别上增加更多的复杂度
CREATE TABLE "user_follows" ( "followed_username" text , "follower_username" text , PRIMARY KEY ("followed_username", "follower_username") ); -- 将会报错 SELECT * FROM "user_follows" WHERE "follower_username" =‘alice‘; -- 建立索引CREATEINDEXON "user_follows" ("follower_username"); INSERTINTO "user_follows" ("followed_username", "follower_username") VALUES (‘alice‘, ‘bob‘); INSERTINTO "user_follows" ("followed_username", "follower_username") VALUES (‘alice‘, ‘carol‘); INSERTINTO "user_follows" ("followed_username", "follower_username") VALUES (‘carol‘, ‘bob‘); INSERTINTO "user_follows" ("followed_username", "follower_username") VALUES (‘dave‘, ‘bob‘); SELECT*FROM "user_follows" WHERE "follower_username" =‘bob‘; -- 将会报错CREATEINDEXON "users" ("email", "encrypted_password"); SELECT*FROM "user_follows" WHERE "follower_username" >‘alice‘;
视图
在建模关系时,我们在其中一列上创建了辅助索引。
但是,在非分区键列上添加辅助索引会对读取产生性能影响。如果需要高性能,需要在查询中精准指定分区键,这样查询就不会跨多个分区扩展。
为了避免客户端非规范化对数列使用二级索引,Cassandra 3.0引入了视图。视图处理服务器端的非规范化,确保基本数据和视图数据之间的最终一致性。
CREATE TABLE "user_follows_base" ( "followed_username" text , "follower_username" text , PRIMARY KEY ("followed_username", "follower_username") ); CREATE MATERIALIZED VIEW "user_follows_view" ASSELECT followed_username, follower_username FROM "user_follows_base" WHERE follower_username ISNOTNULLAND followed_username ISNOTNULLPRIMARYKEY (follower_username, followed_username); INSERTINTO "user_follows_base" ("followed_username", "follower_username") VALUES (‘alice‘, ‘bob‘); INSERTINTO "user_follows_base" ("followed_username", "follower_username") VALUES (‘alice‘, ‘carol‘); INSERTINTO "user_follows_base" ("followed_username", "follower_username") VALUES (‘carol‘, ‘bob‘); INSERTINTO "user_follows_base" ("followed_username", "follower_username") VALUES (‘dave‘, ‘bob‘); SELECT*FROM "user_follows_base" WHERE "followed_username" =‘alice‘; SELECT*FROM "user_follows_base" WHERE "follower_username" =‘bob‘; SELECT*FROM "user_follows_view" WHERE "follower_username" =‘bob‘; DELETEFROM "user_follows_base" WHERE "followed_username" =‘dave‘AND "follower_username" =‘bob‘; INSERTINTO "user_outbound_follows" ("follower_username", "followed_username") VALUES (‘alice‘, ‘dave‘); INSERTINTO "user_inbound_follows" ("followed_username", "follower_username") VALUES (‘dave‘, ‘alice‘); INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘dave‘, NOW(), ‘dave update one‘); INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘carol‘, NOW(), ‘carol update one‘); INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘dave‘, NOW(), ‘dave update two‘); INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘carol‘, NOW(), ‘carol update two‘); SELECT "followed_username" FROM "user_outbound_follows" WHERE "follower_username" =‘alice‘; SELECT "id", UNIXTIMESTAMPOF("id"), "body" FROM "user_status_updates" WHERE "username" IN (‘carol‘, ‘dave‘); -- 将会报错SELECT "username", "id", UNIXTIMESTAMPOF("id"), "body" FROM "user_status_updates" WHERE "username" IN (‘carol‘, ‘dave‘) ORDERBY "id" DESC LIMIT 2; SELECT "username", "id", UNIXTIMESTAMPOF("id"), "body" FROM "user_status_updates" WHERE "username" IN (‘carol‘, ‘dave‘) AND "id" < 3a59c320-28cf-11e4-8069-5f98e903bf02 ORDERBY "id" DESC LIMIT 2; CREATETABLE "home_status_update_ids" ( "timeline_username" text, "status_update_id" timeuuid, "status_update_username" text, PRIMARYKEY ("timeline_username", "status_update_id") ) WITH CLUSTERING ORDERBY ("status_update_id" DESC); SELECT "follower_username" FROM "user_inbound_follows" WHERE "followed_username" =‘carol‘; INSERTINTO "user_status_updates" ("username", "id", "body") VALUES( ‘carol‘, 65cd8320-2ad7-11e4-8069-5f98e903bf02, ‘carol update 3‘); INSERTINTO "home_status_update_ids" ("timeline_username", "status_update_id", "status_update_username") VALUES (‘alice‘, 65cd8320-2ad7-11e4-8069-5f98e903bf02, ‘carol‘); SELECT NOW() FROM "user_status_updates" LIMIT 1; INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘dave‘, a05b90b0-2ada-11e4-8069-5f98e903bf02, ‘dave update 3‘); SELECT*FROM "user_status_updates" WHERE "username" IN (‘dave‘, ‘carol‘) AND "id" IN ( a05b90b0-2ada-11e4-8069-5f98e903bf02, 65cd8320-2ad7-11e4-8069-5f98e903bf02 ); DELETEFROM "user_status_updates" WHERE username =‘dave‘AND id = a05b90b0-2ada-11e4-8069-5f98e903bf02; DELETEFROM "home_status_update_ids" WHERE timeline_username IN (‘alice‘) AND status_update_id = a05b90b0-2ada-11e4-8069-5f98e903bf02; CREATETABLE "home_status_updates" ( "timeline_username" text, "status_update_id" timeuuid, "status_update_username" text, "body" text, PRIMARYKEY ("timeline_username", "status_update_id") ) WITH CLUSTERING ORDERBY ("status_update_id" DESC); SELECT "followed_username" FROM "user_inbound_follows" WHERE "followed_username" =‘carol‘; INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘carol‘, cacc7de0-2af9-11e4-8069-5f98e903bf02, ‘carol update 4‘ ); INSERTINTO "home_status_updates" ("timeline_username", "status_update_id", ? "status_update_username", "body") VALUES ( ‘alice‘, cacc7de0-2af9-11e4-8069-5f98e903bf02, ‘carol‘, ‘carol update 4‘ ); SELECT "followed_username" FROM "user_inbound_follows" WHERE "followed_username" =‘dave‘; INSERTINTO "user_status_updates" ("username", "id", "body") VALUES (‘dave‘, 16e2f240-2afa-11e4-8069-5f98e903bf02, ‘dave update 4‘); INSERTINTO "home_status_updates" ("timeline_username", "status_update_id", ? "status_update_username", "body") VALUES ( ‘alice‘, 16e2f240-2afa-11e4-8069-5f98e903bf02, ‘dave‘, ‘dave update 4‘ ); SELECT "status_update_username", "status_update_id", "body" FROM "home_status_updates" WHERE "timeline_username" =‘alice‘;
批处理
-- 批处理 BEGIN BATCH INSERT INTO "user_status_updates" ("username", "id", "body") VALUES ( ‘ dave ‘ , 16e2f240 -2afa-11e4-8069-5f98e903bf02, ‘dave update 4‘ ); INSERTINTO "home_status_updates" ( "timeline_username", "status_update_id", "status_update_username", "body") VALUES ( ‘alice‘, 16e2f240-2afa-11e4-8069-5f98e903bf02, ‘dave‘, ‘dave update 4‘ ); APPLY BATCH; CREATETABLE batchlog (? id uuid PRIMARYKEY,? written_at timestamp,? data blob? ); BEGIN UNLOGGED BATCH INSERTINTO "user_status_updates" ("username", "id", "body") VALUES( ‘dave‘, 16e2f240-2afa-11e4-8069-5f98e903bf02, ‘dave update 4‘ ); INSERTINTO "home_status_updates" ( "timeline_username", "status_update_id", "status_update_username", "body") VALUES ( ‘alice‘, 16e2f240-2afa-11e4-8069-5f98e903bf02, ‘dave‘, ‘dave update 4‘ ); APPLY BATCH; BEGIN UNLOGGED BATCH INSERTINTO "status_update_replies" ("status_update_username", "status_update_id", "id", "author_username", "body") VALUES (‘alice‘, 76e7a4d0-e796-11e3-90ce-5f98e903bf02, NOW(), ‘bob‘, ‘Bob Reply1‘); INSERTINTO "status_update_replies" ("status_update_username", "status_update_id", "id", "author_username", "body") VALUES (‘alice‘, 76e7a4d0-e796-11e3-90ce-5f98e903bf02, NOW(), ‘carol‘, ‘Carol Reply1‘); APPLY BATCH; BEGIN UNLOGGED BATCH INSERTINTO "user_status_updates" ("username", "id", "body") VALUES( ‘dave‘, NOW(), ‘dave update 5‘ ); INSERTINTO "user_status_updates" ("username", "id", "body") VALUES( ‘ellen‘, NOW(), ‘ellen update 6‘ ); APPLY BATCH;
原文:https://www.cnblogs.com/starcrm/p/11928459.html
内容总结
以上是互联网集市为您收集整理的Cassandra开发入门文档第三部分(非规范化关系结构、批处理)全部内容,希望文章能够帮你解决Cassandra开发入门文档第三部分(非规范化关系结构、批处理)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。