Q.
NoSQL
stands for "Not Only SQL," and it's a type of database that doesn't
use the tradi onal rela onal model (like MySQL or PostgreSQL) for
storing data. NoSQL databases are designed to handle large amounts
of data that may not fit neatly into tables or rows. They are flexible,
scalable, and can store different types of data, such as text,
documents, key-value pairs, graphs, or wide-column stores.
1. Document Stores (e.g., MongoDB) – Store data in JSON-like
documents.
2. Key-Value Stores (e.g., Redis) – Store data as key-value pairs.
3. Column-Family Stores (e.g., Cassandra) – Store data in columns
instead of rows.
4. Graph Databases (e.g., Neo4j) – Store data as nodes and
rela onships.
Advantages: Scalability, flexibility, high availability.
Use Cases: Big data, real- me analy cs, systems with unstructured
data.
Q. MongoDB: Overview
MongoDB is a NoSQL database that stores data in flexible documents
instead of tables. It uses a format called BSON (Binary JSON), which is
similar to JSON but includes addi onal data types. MongoDB is
known for its ability to scale easily and handle unstructured or semi-
structured data.
Data Types in MongoDB
MongoDB supports a variety of data types, including:
String: Text data.
Integer: Numeric values.
Boolean: True or false values.
Array: Lists of values.
Object: Nested documents (like rows in rela onal databases).
Date: Dates and mes.
Null: Represents missing or empty values.
ObjectId: A unique iden fier for each document.
Creating Documents
In MongoDB, data is stored in documents. These documents are similar to records in
relational databases but can have varying fields. You don’t need to define a schema in
advance, which makes MongoDB flexible for handling different data structures.
Updating Documents
You can update documents in MongoDB. You can either update one document or
multiple documents at a time. Updates can include modifying existing values or
adding new fields to the documents.
Deleting Documents
MongoDB allows you to delete documents. You can delete a single document or
multiple documents that match specific criteria.
Querying Documents in MongoDB
In MongoDB, querying documents means searching for data that
matches certain condi ons. You can use queries to find specific
documents based on the values of their fields.
Find all documents: If you want to get all documents in a
collec on, you simply ask MongoDB to return everything.
Find specific documents: You can search for documents that
match certain condi ons, like finding all documents where a
person's age is greater than 25.
Queries in MongoDB are flexible and allow you to find data easily
based on your needs.
Q. Indexing in MongoDB
Indexing in MongoDB helps speed up searches by crea ng shortcuts
to quickly find documents. Without indexes, MongoDB would have to
look through all the data every me you search, which can be slow.
Index: A special structure that helps MongoDB find data faster.
Default Index: MongoDB automa cally creates an index on the
_id field for each document.
Advantages of Indexing in MongoDB
1. Faster Searches: Indexes make searching for documents much
faster, especially in large collec ons.
2. Improved Query Performance: With indexes, MongoDB can
quickly find data based on specific fields, reducing the me
needed to process queries.
3. Efficient Sor ng: Indexes help MongoDB sort data more
efficiently.
4. Reduced Resource Usage: By using indexes, MongoDB can
avoid scanning the en re collec on, saving me and system
resources.
Q. Capped Collec ons in MongoDB
Capped collec ons are a special type of collec on in MongoDB that
have a fixed size. When the collec on reaches its size limit, the oldest
documents are automa cally removed to make space for new ones
This type of collec on is useful for applica ons that need to store a
set amount of data, like logs or me-series data, where only the most
recent entries are important.
Advantages of Capped Collec ons in MongoDB
1. Automa c Overwri ng: Old data is automa cally deleted when
the collec on reaches its size limit, so you don’t have to worry
about removing old records manually.
2. Faster Inser on: Since capped collec ons have a fixed size, they
are op mized for fast data inser on.
3. Preserved Order: Data is stored in the order it was added,
which is useful for applica ons like logs or me-series data.
4. Efficient Storage: They use less storage and maintain a constant
size, making them ideal for cases where only the most recent
data is important.
Q. CRUD Operations in MongoDB
CRUD stands for Create, Read, Update, and Delete operations,
which are the basic actions you can perform on data in MongoDB.
1. Create (Insert Data)
To insert data into a MongoDB collection, use the insertOne() or
insertMany() methods.
insertOne(): Inserts a single document.
insertMany(): Inserts multiple documents at once.
2. Read (Query Data)
To retrieve data from MongoDB, you use the find() method. You can
filter results by passing a query to find().
find(): Retrieves documents that match the given condition.
3. Update (Modify Data)
To update existing documents, you use the updateOne(),
updateMany(), or replaceOne() methods.
updateOne(): Updates a single document.
updateMany(): Updates multiple documents.
replaceOne(): Replaces a document with a new one.
4. Delete (Remove Data)
To remove documents, you use deleteOne() or deleteMany().
deleteOne(): Deletes a single document.
deleteMany(): Deletes multiple documents.
Q. HBase: Concepts
HBase is a NoSQL database that is modeled a er Google's Bigtable. It
is designed to handle large amounts of data across many machines in
a distributed fashion. HBase is o en used in big data applica ons,
especially with Hadoop, for storing and processing massive datasets.
Advantages
Scalable: Can store massive amounts of data across many
machines.
Fast: Provides quick read and write opera ons.
Distributed: Works across mul ple servers for be er
performance.
Flexible: Can store data with varying structures.
HBase: Concepts
1. Table: A collec on of rows and columns where data is stored.
2. Row: Each row has a unique Row Key and stores data in
columns.
3. Column Family: Groups related columns together in a table.
4. Column: A field within a column family, can have mul ple
versions.
5. Cell: The combina on of row, column family, and column,
holding data.
6. HRegion: A part of a table, stored on a server called
RegionServer.
7. RegionServer: A server that handles data reading and wri ng.
8. Write-Ahead Log (WAL): A log that ensures data is safely
wri en.
9. Master: Manages the HBase cluster and assigns regions to
RegionServers.
10. Scan: A method to read data from a table.
Q. Summary Comparison: HBase vs RDBMS
1. Crea ng Records:
o HBase: Records are created with a Row Key and Column
Families. You don’t need to define a schema in advance,
allowing flexibility.
o RDBMS: Records are inserted into tables with predefined
columns and data types.
2. Accessing Data:
o HBase: Data is accessed by Row Key and is designed for
fast reads and writes over large datasets.
o RDBMS: Data is accessed using SQL queries with
condi ons like SELECT.
3. Upda ng and Dele ng Data:
o HBase: Updates add new versions of data, and dele ons
are done by specifying the Row Key.
o RDBMS: Data is updated or deleted using SQL commands
like UPDATE and DELETE.
4. Modeling Data:
o HBase: Data is stored in a flexible format, with Column
Families. It works well for semi-structured data.
o RDBMS: Data is stored in fixed tables with defined
columns and rela onships (like foreign keys).
5. Expor ng and Impor ng Data:
o HBase: Data can be exported/imported using tools like
HBase’s Export command or Apache Spark.
o RDBMS: Data is exported/imported using tools like
mysqldump or pg_dump.
Conclusion:
HBase is best for handling large, flexible, and unstructured data
with quick read/write needs.
RDBMS is ideal for structured data with predefined schemas
and complex rela onships.
Use Cases of HBase
1. Call Detail Log (CDR) Analysis:
o Use Case: Telecommunica on companies store large
volumes of Call Detail Records (CDRs) that track every call
made by users, including mestamps, call dura on, origin,
des na on, etc.
o Why HBase: HBase is suitable for storing and processing
large datasets with high write throughput, as it can handle
millions of calls per second. It allows for quick access to
CDR data by Row Key, which could be the phone number
or call mestamp, making it ideal for analyzing vast
amounts of call data for trends, pa erns, or anomalies
(e.g., tracking user behavior or network performance).
o Example: A telecom company can use HBase to analyze
CDRs in real- me, detect abnormal calling pa erns, or
generate billing reports efficiently.
Credit Fraud Alert:
Use Case: Banks and financial ins tu ons need to detect
fraudulent transac ons by analyzing pa erns in user behavior
(e.g., transac ons that deviate from usual spending habits).
Why HBase: HBase’s ability to scale horizontally and handle
real- me data makes it an excellent choice for storing
transac on logs and performing fraud detec on analysis. It can
store large amounts of transac onal data and help quickly flag
unusual transac ons.
Example: A credit card company can use HBase to monitor real-
me transac ons, detect poten ally fraudulent ac vi es by
comparing transac on behaviors, and send alerts to the user
instantly.
Weather Forecas ng:
Use Case: Weather organiza ons collect vast amounts of data,
including temperature, humidity, wind speed, and precipita on
from different sensors across the globe, to generate forecasts.
Why HBase: HBase is ideal for weather data because it can
store large volumes of me-series data. It can handle frequent
updates, and quick reads/writes for data such as hourly
weather records, satellite imagery, or sensor data.
Example: A weather forecas ng system could use HBase to
store historical weather data and generate forecasts. It can be
scaled easily to include new sensor data from various loca ons
and provide insights for future weather predic ons.