【问题标题】:How to properly model tables in Cassandra (from a SQL mind set to Cassandra)?如何在 Cassandra 中正确建模表(从 SQL 思维模式到 Cassandra)?
【发布时间】:2018-10-16 17:22:25
【问题描述】:

我正在尝试 Cassandra,但我仍在尝试将我的大脑切换到这种基于列的逻辑。

我看过几个视频,并试图模仿他们谈论的内容...视频存储、天气信息等,但我不断收到错误。

  • 来自服务器的错误:code=2200 [Invalid query] message="Unknown PRIMARY KEY 中引用的定义 postid"

  • 来自服务器的错误:代码 = 2200 [无效查询] 消息 =“缺少列的 CLUSTERING ORDER postid”

我只尝试了用户、标签和帖子。我可以很容易地想象出我不明白的地方。

CREATE TABLE users( 
  userId uuid, 
  login map<int, text>, 
  email text, 
  phoneNumber text, 
  emailVerified boolean, 
  phoneNumberVerified boolean, 
  firstName text, 
  lastName text, 
  gender text, 
  country text, 
  region text, 
  city text, 
  cityId int, 
  zipcode text, 
  password text, 
  passwordSetDate timestamp, 
  createdAt timestamp, 
  PRIMARY KEY ((email, phoneNumber, userId), lastName, createdAt)
);
CREATE TABLE tags( 
  tag text, 
  itemId uuid, 
  itemType text, 
  createdAt timestamp, 
  PRIMARY KEY ((tag), itemId)
);
CREATE TABLE posts( 
  postId uuid, 
  authorId uuid, 
  authorName text, 
  content text, 
  tags set<text>, 
  createdAt timestamp, 
  PRIMARY KEY ((authorId, postId), createdAt)
) WITH CLUSTERING ORDER BY (createdAt DESC);
CREATE TABLE comments( 
  commentId uuid, 
  postId uuid, 
  authorName text, 
  authorId uuid, 
  content text, 
  createdAt timestamp, 
  PRIMARY KEY ((authorId), postId)
) WITH CLUSTERING ORDER BY (createdAt DESC);
CREATE TABLE messages( 
  channelId uuid, 
  messageId uuid, 
  authorId uuid, 
  authorName text, 
  content text,
  tags set<text>, 
  time timeuuid, 
  createdAt Timestamp, 
  PRIMARY KEY ((channelId, authorId), messageId)
) WITH CLUSTERING ORDER BY (message_id DESC);
CREATE TABLE channels( 
  channelId uuid, 
  content text, 
  createdAt Timestamp, 
  PRIMARY KEY ((channelId, authorId), messageId)
) WITH CLUSTERING ORDER BY (message_id DESC);
CREATE TABLE media( 
  mediaId uuid, 
  userId uuid, 
  userEmail text, 
  type text, 
  description text, 
  size text, 
  mime text, 
  tags set<text>, 
  createdAt Timestamp, 
  PRIMARY KEY ((channel_id), message_id)
) WITH CLUSTERING ORDER BY (message_id DESC);
CREATE TABLE loginLog( 
  eventId timeuuid, 
  login text, 
  createdAt timestamp, 
  status text, 
  ip inet, 
  PRIMARY KEY ((eventId, ip), message_id)
);
CREATE TABLE weatherByCities( 
  cityId int, 
  date text, 
  city text, 
  createdAt timestamp, 
  PRIMARY KEY ((cityId, date), createdAt)
) WITH CLUSTERING ORDER BY (createdAt DESC);

谁能解释一下我不明白的地方?缺陷在哪里?

【问题讨论】:

    标签: sql database-design cassandra


    【解决方案1】:

    您的查询中存在几个严重的拼写错误、无效的列定义:以下是其中的一些:

    CREATE TABLE comments( commentId uuid, postId uuid, authorName text, authorId uuid, content text, createdAt timestamp, PRIMARY KEY ((authorId), postId) ) WITH CLUSTERING ORDER BY (createdAt DESC);

    这里 WITH CLUSTERING ORDER BY (createdAt DESC); createdAt 列不是集群键。它应该是 postId

    CREATE TABLE messages( channelId uuid, messageId uuid, authorId uuid, authorName text, content text, tags set<text>, time timeuuid, createdAt Timestamp, PRIMARY KEY ((channelId, authorId), messageId) ) WITH CLUSTERING ORDER BY (message_id DESC);

    这里message_id不存在,应该是messageId

    同样的事情也发生在媒体上:

    CREATE TABLE media( mediaId uuid, userId uuid, userEmail text, type text, description text, size text, mime text, tags set<text>, createdAt Timestamp, PRIMARY KEY ((channel_id), message_id) ) WITH CLUSTERING ORDER BY (message_id DESC);

    channel_id, message_id 不存在。

    我猜你已经知道你做错了什么。现在仔细找到其他错别字并修复它们。

    【讨论】:

    • 哦,是的,关于类型的问题 - 我注意到了,但我虽然改变了它们。现在我明白了, WITH CLUSTERING ORDER 应该指一个键。呃。我会得到它的窍门。感谢您的投入。
    猜你喜欢
    • 1970-01-01
    • 2015-11-16
    • 2023-03-30
    • 2013-11-15
    • 1970-01-01
    • 2018-04-02
    • 2015-01-29
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多