0% found this document useful (0 votes)
25 views7 pages

Physio

Uploaded by

Sakthi PS21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Physio

Uploaded by

Sakthi PS21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Cassandra Data Model Overview

explain cassandra data model with an example

Apache Cassandra's data model is based on the concepts of keyspace, tables, rows, and columns.
Here's a brief overview with an example:

1. Keyspace: The outermost container that holds tables, similar to a database in relational
databases.
2. Table: A collection of rows. Each table has a primary key to uniquely identify rows.
3. Row: A set of columns. Each row is uniquely identified by the primary key.
4. Column: The smallest unit of data. Each column has a name, value, and timestamp.

Example

Let's consider a simple example of a table for storing user information in a social media application.
Keyspace: `social_media`
Table: `users`

sql

CREATE KEYSPACE social_media


WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};

USE social_media;

CREATE TABLE users (


user_id UUID PRIMARY KEY,
username TEXT,
email TEXT,
age INT
);

Inserting Data:

sql

INSERT INTO users (user_id, username, email, age)


VALUES (uuid(), 'john_doe', '[email protected]', 30);

Querying Data:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/7
sql

SELECT * FROM users WHERE user_id = <some-uuid>;

Explanation

1. Keyspace Creation:

sql

CREATE KEYSPACE social_media

This creates a keyspace named `social_media` with a simple replication strategy.


2. Table Creation:

sql

CREATE TABLE users (user_id UUID PRIMARY KEY, username TEXT, email TEXT, age INT);

This defines a table named `users` with columns `user_id` (primary key), `username`, `email`,
and `age`.
3. Inserting Data:

sql

INSERT INTO users (user_id, username, email, age) VALUES (uuid(), 'john_doe',
'[email protected]', 30);

This inserts a new user with a unique ID, username, email, and age.
4. Querying Data:

sql

SELECT * FROM users WHERE user_id = <some-uuid>;

This retrieves the user information for a specific `user_id`.

Cassandra is designed for scalability and high availability, making it suitable for applications requiring
large amounts of data and quick access.

explain
polymorphic schema with an example

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/7
A polymorphic schema in databases allows storing different types of data in the same table, enabling
flexible and dynamic schemas. In Cassandra, this can be achieved using a combination of wide rows
and collections (like maps or sets) to handle various attributes.

Example: Polymorphic Schema for an E-commerce Application

Scenario:

We need to store information about different products such as electronics, clothing, and books, each
with its specific attributes.

Table Schema:

sql

CREATE TABLE products (


product_id UUID PRIMARY KEY,
product_type TEXT,
common_attributes MAP<TEXT, TEXT>,
electronic_attributes MAP<TEXT, TEXT>,
clothing_attributes MAP<TEXT, TEXT>,
book_attributes MAP<TEXT, TEXT>
);

Explanation:

1. product_id: Unique identifier for each product.


2. product_type: Indicates the type of product (e.g., 'electronics', 'clothing', 'book').
3. common_attributes: Stores common attributes like name, price, and brand.
4. electronic_attributes: Stores attributes specific to electronics like warranty, battery life.
5. clothing_attributes: Stores attributes specific to clothing like size, material.
6. book_attributes: Stores attributes specific to books like author, ISBN.

Inserting Data:

sql

-- Inserting an electronic item


INSERT INTO products (product_id, product_type, common_attributes, electronic_attributes)
VALUES (
uuid(),
'electronics',
{'name': 'Smartphone', 'price': '699', 'brand': 'TechBrand'},
{'warranty': '2 years', 'battery_life': '10 hours'}
);

-- Inserting a clothing item


INSERT INTO products (product_id, product_type, common_attributes, clothing_attributes)
VALUES (
uuid(),
'clothing',
{'name': 'T-Shirt', 'price': '20', 'brand': 'FashionBrand'},
{'size': 'M', 'material': 'Cotton'}
);

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/7
-- Inserting a book
INSERT INTO products (product_id, product_type, common_attributes, book_attributes)
VALUES (
uuid(),
'book',
{'name': 'The Great Novel', 'price': '15', 'brand': 'BooksPublisher'},
{'author': 'Famous Author', 'ISBN': '1234567890'}
);

Querying Data:

sql

-- Querying a product by ID
SELECT * FROM products WHERE product_id = <some-uuid>;

-- Querying all electronic products


SELECT * FROM products WHERE product_type = 'electronics';

Advantages:

1. Flexibility: Allows storing different types of products with varying attributes in a single table.
2. Scalability: Efficiently handles large amounts of diverse data.
3. Simplicity: Reduces the need for multiple tables, simplifying the schema.

This approach is particularly useful in applications where the data structure can vary significantly
between different entities, providing the flexibility needed to manage diverse data types efficiently.

polymorphic schema in mongo db

In MongoDB, a polymorphic schema allows you to store documents of different types in the same
collection. This is facilitated by MongoDB's flexible, schema-less nature, where each document can
have its unique structure.

Example: Polymorphic Schema for an E-commerce Application

Scenario:

We need to store information about different products such as electronics, clothing, and books, each
with its specific attributes.

Collection: `products`

Sample Documents:

1. Electronic Product Document:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/7
json

{
"_id": ObjectId("60c72b2f9b1d8b001c8e4c72"),
"product_type": "electronics",
"name": "Smartphone",
"price": 699,
"brand": "TechBrand",
"attributes": {
"warranty": "2 years",
"battery_life": "10 hours"
}
}

2. Clothing Product Document:

json

{
"_id": ObjectId("60c72b3f9b1d8b001c8e4c73"),
"product_type": "clothing",
"name": "T-Shirt",
"price": 20,
"brand": "FashionBrand",
"attributes": {
"size": "M",
"material": "Cotton"
}
}

3. Book Product Document:

json

{
"_id": ObjectId("60c72b4f9b1d8b001c8e4c74"),
"product_type": "book",
"name": "The Great Novel",
"price": 15,
"brand": "BooksPublisher",
"attributes": {
"author": "Famous Author",
"ISBN": "1234567890"
}
}

Explanation:

Common Fields: All documents have `_id`, `product_type`, `name`, `price`, and `brand`.
Attributes Field: A nested document storing specific attributes relevant to the product type. This
field varies depending on the type of product.

MongoDB Operations:

1. Inserting Documents:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/7
javascript

db.products.insertMany([
{
"product_type": "electronics",
"name": "Smartphone",
"price": 699,
"brand": "TechBrand",
"attributes": {
"warranty": "2 years",
"battery_life": "10 hours"
}
},
{
"product_type": "clothing",
"name": "T-Shirt",
"price": 20,
"brand": "FashionBrand",
"attributes": {
"size": "M",
"material": "Cotton"
}
},
{
"product_type": "book",
"name": "The Great Novel",
"price": 15,
"brand": "BooksPublisher",
"attributes": {
"author": "Famous Author",
"ISBN": "1234567890"
}
}
]);

2. Querying Documents:
Querying by Product Type:

javascript

db.products.find({ product_type: "electronics" });

Querying by Specific Attribute:

javascript

db.products.find({ "attributes.warranty": "2 years" });

Advantages:

1. Flexibility: Easily accommodates different product types and attributes within a single collection.
2. Scalability: Efficiently handles diverse data, making it suitable for large and varied datasets.
3. Simplicity: Reduces complexity by avoiding the need for multiple collections for different
product types.

This approach leverages MongoDB's dynamic schema capabilities to manage heterogeneous data
efficiently, making it ideal for applications where the data structure varies significantly between
different entities.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/7
ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/7

You might also like