Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

Duck avatar image
Duck asked Erick Ramirez commented

How do I model my data when there is no unique key?

Hi, I'm trying to make a data model for pruchase logs. Since its a log model, there is no unique key. Thus, I cannot write partition, primary key. What should I do for that?

Data Model

Timestamp timestamp;
int item;
int amount;
int price;
UUID seller;
UUID buyer;
  1. Which keys should I put into cluster section?
  2. Which keys should I put into partition section?
  3. Is there any problem to have a lot of keys in cluster section?
data modeling
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez commented

It isn't possible for there to not be a unique key. What it sounds like is you don't know what the app queries are.

When modeling data in Cassandra, you need to design one table for each app query. The query will determine how you partition your data and will dictate what is unique about the table so you can define the partition key.

For example, if your app needs to know "Which items were purchased by a buyer?", the table needs to be partitioned by the buyer since you are querying based on the buyer. The schema would look something like this:

CREATE TABLE purchases_by_buyer (
    buyer UUID,
    item int,
    ...
    PRIMARY KEY (buyer, item)
)

In this table, each buyer partition has rows of items. In this case, the buyer is the unique key. Cheers!

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks for the information!

As I understood, I should separate tables by my querying/filtering. I'll separate buyer and seller if it's the case. The problem is it's a log. There'd be same "buyer" and same "item". Therefore, they are not unique.

0 Likes 0 ·

I'm confused. How could the buyer (which is a UUID) be the same as the item (which is an integer)? I think you misunderstood my answer somewhere along the line.

0 Likes 0 ·