Key-Value and Document Data models in NoSQL

Column family stores and Aggregate-Oriented Data Model:

NoSQL databases offer various data models to accommodate different data storage and retrieval needs.

Two common data models in NoSQL databases are the key-value model and the document model.

Here’s an explanation of each with examples:

Key-value Data model

✅Key-Value Data Model:In a key-value data model, data is stored as a collection of key-value pairs.

✅Each key is unique and is associated with a corresponding value.

✅Keys are used to retrieve values, and there is typically no inherent structure or schema enforced on the values.

✅ Key-value stores are highly performant for simple data retrieval but may lack the ability to perform complex queries.

Example of Key-Value Data Model:

Let’s consider a simple key-value store where you want to store user session information: Key: User Session ID (e.g., a session token or a UUID)
Value: User Session Data (e.g., user ID, session start time, user preferences)
Here’s a key-value pair in this example:
Key: “session123”
Value: { “userId”: 101, “startTime”: “2023-09-25T10:00:00”, “preferences”: {
“theme”: “dark”, “language”: “en” } }
In this example, you can quickly retrieve the session data for a user by providing their session ID.

Document Data Model

Document Data Model:The document data model is an extension of the key-value model, but with a more structured approach.

In this model, data is stored as documents, which are self-contained data structures typically in formats like JSON or BSON.

Each document has a unique identifier (often referred to as a primary key) and may contain nested fields and arrays, allowing for the storage of more complex and semi-structured data.

Example of Document Data Model: Consider an e-commerce platform that stores product information using a document data model:

Collection (Equivalent to a Table): “Products”

Documents (Equivalent to Rows): Each document represents a product with fields like product ID, name, description, price, and tags.

Example document in the “Products” collection: json code

{

“_id”: “product123”,

“name”: “Laptop”,

“description”: “High-performance laptop”,

“price”: 999.99,

“tags”: [“electronics”, “computers”]

}

Key value and Document Data Models

Key-Value and Document data models are two types of NoSQL database models that are used to store and retrieve data.

Column family stores

Column-family stores, also known as column-family databases or wide-column stores, are a type of NoSQL database that is designed to efficiently handle and manage large amounts of data, particularly when dealing with distributed and scalable systems.

They are especially suitable for write workloads and applications requiring high availability and horizontal scalability.

Apache Cassandra and HBase are well-known examples of column-family stores.

Example:

Suppose you are building a social media application, and you want to store user profiles, including their basic information and posts.

In a column-family store like Apache Cassandra, you might structure your data as follows:

Column Family: UserProfile

Row Key: User001

Columns:

“username” -> “user123”

“email” -> “user123@example.com”

“birthdate” -> “1990-01-15”

Row Key: User002

Columns:

“username” -> “johndoe”

“email” -> “johndoe@example.com”

“birthdate” -> “1985-05-20”

Column Family: user posts

Row Key: User001

Columns:

“post123” -> “Had a great day today!”

“post124” -> “Excited for the weekend!”

Row Key: User002

Columns:

“post125” -> “Visited a new restaurant.”

“post126” -> “Saw an amazing movie.”

In this example:

“UserProfile” and “UserPosts” are column families.

Each user is represented by a row in the respective column family.

Columns within each row store different attributes or posts.

The schema is flexible; different users can have different sets of columns.

Key Characteristics of Column-Family Stores:

Distributed and Scalable

Eventual Consistency

High Write Throughput

Schema Flexibility

Aggregate-Oriented Data Model:

Aggregate-Oriented Data Model:

In an aggregate-oriented database, data is organized and stored in a way that facilitates the efficient retrieval of precomputed aggregates.

These aggregates represent summarizations or calculations of the underlying raw data.

The design often involves denormalization to reduce the need for complex joins and calculations during query execution.

Example:

Suppose you are building an e-commerce platform, and you want to track and analyze sales data for different products. In an aggregate-oriented database, you might structure your data as follows:

Aggregate: ProductSales

ProductID: 123

TotalSales: $5,000

TotalQuantitySold: 200 units

ProductID: 456

TotalSales: $2,500

Total Quantity Sold: 100 units

In this example: “Product Sales” is an aggregate representing the summary of sales data for various products. Each product has its own record within the aggregate, containing precomputed values like total sales and total quantity sold.

Key Characteristics of Aggregate-Oriented Databases:

Precomputed Aggregates

Denormalization

Optimized for Reads

High Performance

Trade-Off with Data Freshness

One response to “Key-Value and Document Data models in NoSQL”

Leave a Reply

Your email address will not be published. Required fields are marked *

Key-Value and Document Data models in NoSQL

One thought on “Key-Value and Document Data models in NoSQL

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top