LOAD, FILTER, and STORE operations in Hadoop - Time & Space Complexity
We want to understand how the time needed changes when we load, filter, and store data in Hadoop.
How does the size of data affect the work done by these operations?
Analyze the time complexity of the following code snippet.
data = LOAD 'input_data';
filtered = FILTER data BY some_condition;
STORE filtered INTO 'output_data';
This code loads data, filters rows based on a condition, then stores the filtered data.
Look at what repeats as data size grows.
- Primary operation: Filtering each row to check the condition.
- How many times: Once for every row in the loaded data.
As the number of rows increases, the filtering work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 filter checks |
| 100 | About 100 filter checks |
| 1000 | About 1000 filter checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time grows in a straight line with the number of rows processed.
[X] Wrong: "Filtering only takes constant time no matter how much data there is."
[OK] Correct: Each row must be checked, so more rows mean more work.
Understanding how data size affects filtering helps you explain how Hadoop handles big data efficiently.
"What if we added a nested loop inside the filter condition? How would the time complexity change?"
Practice
LOAD operation do in Hadoop data processing?Solution
Step 1: Understand the purpose of LOAD
The LOAD operation is used to bring data from external files into the Hadoop processing environment.Step 2: Differentiate from FILTER and STORE
FILTER selects data rows, and STORE saves data back, so LOAD must be the reading step.Final Answer:
Reads data from files into the system -> Option AQuick Check:
LOAD = Read data [OK]
- Confusing LOAD with STORE operation
- Thinking LOAD filters data
- Assuming LOAD deletes data
Solution
Step 1: Recall FILTER syntax in Pig Latin
The correct syntax uses FILTER ... BY to select rows based on a condition.Step 2: Check each option
A mixes LOAD and FILTER incorrectly; B uses WHERE which is invalid in Pig; C is SQL syntax; D uses FILTER ... BY correctly.Final Answer:
filtered_data = FILTER data BY age > 30; -> Option BQuick Check:
FILTER ... BY is correct syntax [OK]
- Using WHERE instead of BY in FILTER
- Confusing Pig syntax with SQL
- Trying to filter during LOAD
data = LOAD 'users.txt' USING PigStorage(',') AS (name:chararray, age:int);
adults = FILTER data BY age >= 18;
STORE adults INTO 'adult_users';What will be the content of the 'adult_users' folder?
Solution
Step 1: Understand the FILTER condition
The FILTER selects rows where age is 18 or more, so only adults remain.Step 2: STORE saves filtered data
The STORE command saves the filtered adults data into 'adult_users' folder.Final Answer:
Only users with age 18 or older -> Option DQuick Check:
FILTER age >= 18 then STORE = adults only [OK]
- Assuming STORE saves original data
- Thinking STORE needs format specified always
- Confusing FILTER condition direction
data = LOAD 'data.txt' USING PigStorage(',');
filtered = FILTER data WHERE score > 50;
STORE filtered INTO 'high_scores';Solution
Step 1: Check FILTER syntax
FILTER in Pig Latin requires BY keyword, not WHERE.Step 2: Verify other parts
LOAD can omit AS but then schema is unknown; STORE syntax is correct; PigStorage accepts parameters.Final Answer:
FILTER uses WHERE instead of BY -> Option CQuick Check:
FILTER ... BY is correct, not WHERE [OK]
- Using WHERE in FILTER
- Thinking AS is mandatory in LOAD
- Misunderstanding PigStorage parameters
Solution
Step 1: Load with schema and correct syntax
sales = LOAD 'sales.csv' USING PigStorage(',') AS (product:chararray, quantity:int, price:int); filtered = FILTER sales BY quantity > 10 AND price < 100; STORE filtered INTO 'filtered_sales'; loads with schema and uses PigStorage correctly.Step 2: Filter with correct condition and operator
We want quantity > 10 AND price < 100, so AND is correct, not OR.Step 3: STORE command is correct
sales = LOAD 'sales.csv' USING PigStorage(',') AS (product:chararray, quantity:int, price:int); filtered = FILTER sales BY quantity > 10 AND price < 100; STORE filtered INTO 'filtered_sales'; uses STORE correctly to save filtered data.Final Answer:
Correct load, filter with AND, and store commands -> Option AQuick Check:
LOAD with schema + FILTER with AND + STORE = sales = LOAD 'sales.csv' USING PigStorage(',') AS (product:chararray, quantity:int, price:int); filtered = FILTER sales BY quantity > 10 AND price < 100; STORE filtered INTO 'filtered_sales'; [OK]
- Using WHERE instead of BY in FILTER
- Using OR instead of AND for filtering
- Omitting schema in LOAD causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
