📖 Scenario: You work at a small online store. You have two data files: one with customer orders and another with customer details. You want to learn how to group orders by customer and then join customer details with their orders.
🎯 Goal: Build a Hadoop MapReduce job that groups orders by customer ID and then joins customer details with their orders to produce a combined output showing customer name and their orders.
📋 What You'll Learn
Create a dataset of orders with customer IDs and order amounts
Create a dataset of customers with customer IDs and names
Write a MapReduce job to group orders by customer ID
Write a MapReduce job to join customer details with their orders
Print the final joined output showing customer names and their orders
💡 Why This Matters
🌍 Real World
Grouping and joining data is common in sales analysis, customer segmentation, and reporting in many businesses.
💼 Career
Data scientists and engineers often need to group and join large datasets to prepare data for analysis or machine learning.
Progress0 / 4 steps
1
Create the orders dataset
Create a list called orders with these exact entries: ("C1", 100), ("C2", 150), ("C1", 200), ("C3", 300).
Hadoop
Hint
Use a Python list of tuples to store the orders exactly as shown.
2
Create the customers dataset
Create a dictionary called customers with these exact entries: 'C1': 'Alice', 'C2': 'Bob', 'C3': 'Charlie'.
Hadoop
Hint
Use a Python dictionary to map customer IDs to names exactly as shown.
3
Group orders by customer ID
Create a dictionary called grouped_orders that groups order amounts by customer ID from the orders list. Use a for loop with variables cust_id and amount to iterate over orders. Append amounts to the list for each customer ID.
Hadoop
Hint
Use a dictionary to collect lists of amounts for each customer ID.
4
Join customers with their grouped orders and print
Create a list called joined_data that contains tuples of customer name and their list of order amounts by joining customers and grouped_orders. Use a for loop with variable cust_id to iterate over grouped_orders. Then print joined_data.
Hadoop
Hint
Use a loop to create a list of tuples with customer names and their orders, then print it.
Practice
(1/5)
1. What does the GROUP BY operation do in Hadoop data processing?
easy
A. It organizes data by keys to summarize or analyze groups.
B. It merges two datasets based on matching keys.
C. It deletes duplicate records from a dataset.
D. It sorts data in ascending order.
Solution
Step 1: Understand the purpose of GROUP BY
The GROUP BY operation groups rows that have the same values in specified columns into summary rows.
Step 2: Differentiate from JOIN
JOIN merges datasets based on keys, but GROUP BY organizes data within one dataset by keys.
Final Answer:
It organizes data by keys to summarize or analyze groups. -> Option A
Quick Check:
GROUP BY = Organize by keys [OK]
Hint: GROUP BY groups data by keys for summaries [OK]
Common Mistakes:
Confusing GROUP BY with JOIN
Thinking GROUP BY deletes duplicates
Assuming GROUP BY sorts data
2. Which of the following is the correct syntax to join two datasets table1 and table2 on the key id in HiveQL (Hadoop SQL)?
easy
A. SELECT * FROM table1 GROUP BY table2.id;
B. SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
C. SELECT * FROM table1 WHERE table1.id = table2.id;
D. SELECT * FROM table1, table2 ON table1.id = table2.id;
Solution
Step 1: Identify correct JOIN syntax
The standard JOIN syntax in HiveQL uses ON to specify the join condition: JOIN table2 ON table1.id = table2.id.
Step 2: Eliminate incorrect options
SELECT * FROM table1 WHERE table1.id = table2.id; uses WHERE incorrectly for join; SELECT * FROM table1 GROUP BY table2.id; uses GROUP BY wrongly; SELECT * FROM table1, table2 ON table1.id = table2.id; incorrectly uses ON with comma-separated tables.
Final Answer:
SELECT * FROM table1 JOIN table2 ON table1.id = table2.id; -> Option B
Quick Check:
JOIN with ON condition = SELECT * FROM table1 JOIN table2 ON table1.id = table2.id; [OK]
Hint: Use JOIN ... ON for correct Hadoop SQL joins [OK]
Common Mistakes:
Using WHERE instead of ON for JOIN
Confusing GROUP BY with JOIN
Using ON clause with old-style comma join
3. Given two datasets in Hive: table1(id INT, value INT) table2(id INT, category STRING) What is the output of this query?
SELECT t2.category, SUM(t1.value) FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id GROUP BY t2.category;
medium
A. Sum of values grouped by id from table1.
B. List of categories with no aggregation.
C. Sum of values grouped by category from table2.
D. Error due to missing GROUP BY column.
Solution
Step 1: Understand JOIN and GROUP BY
The JOIN merges rows from table1 and table2 where ids match. Then GROUP BY groups results by category.
Step 2: Analyze aggregation
SUM(t1.value) calculates total value per category from joined data.
Final Answer:
Sum of values grouped by category from table2. -> Option C
Quick Check:
JOIN + GROUP BY category = sum per category [OK]
Hint: JOIN then GROUP BY aggregates data by joined keys [OK]
Common Mistakes:
Ignoring GROUP BY column in SELECT
Confusing grouping by id vs category
Expecting list without aggregation
4. You wrote this Hive query:
SELECT t1.id, COUNT(*) FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id;
But it gives an error. What is the likely cause?
medium
A. Missing GROUP BY clause for aggregation.
B. Incorrect JOIN syntax.
C. COUNT(*) cannot be used with JOIN.
D. Table aliases are not allowed.
Solution
Step 1: Identify aggregation usage
COUNT(*) is an aggregate function and requires GROUP BY for non-aggregated columns in SELECT.
Step 2: Check query structure
Since t1.id is selected without aggregation, GROUP BY t1.id is needed.
Final Answer:
Missing GROUP BY clause for aggregation. -> Option A
Quick Check:
Aggregate needs GROUP BY [OK]
Hint: Use GROUP BY when selecting non-aggregated columns with COUNT [OK]
Common Mistakes:
Forgetting GROUP BY with aggregate functions
Thinking JOIN syntax is wrong
Assuming COUNT(*) disallowed with JOIN
5. You have two large datasets: sales(order_id, product_id, amount) products(product_id, category) You want to find total sales amount per category but only for categories with total sales above 1000. Which Hive query correctly achieves this?
hard
A. SELECT p.category, SUM(s.amount) FROM sales s JOIN products p ON s.product_id = p.product_id HAVING SUM(s.amount) > 1000;
B. SELECT p.category, SUM(s.amount) FROM sales s JOIN products p ON s.product_id = p.product_id WHERE SUM(s.amount) > 1000 GROUP BY p.category;
C. SELECT p.category, SUM(s.amount) FROM sales s JOIN products p ON s.product_id = p.product_id GROUP BY p.category WHERE SUM(s.amount) > 1000;
D. SELECT p.category, SUM(s.amount) FROM sales s JOIN products p ON s.product_id = p.product_id GROUP BY p.category HAVING SUM(s.amount) > 1000;
Solution
Step 1: Join sales and products on product_id
This merges sales with product categories.
Step 2: Group by category and sum amounts
GROUP BY p.category with SUM(s.amount) calculates total sales per category.
Step 3: Filter groups with HAVING
HAVING filters groups after aggregation; WHERE cannot filter aggregated sums.
Final Answer:
SELECT p.category, SUM(s.amount) FROM sales s JOIN products p ON s.product_id = p.product_id GROUP BY p.category HAVING SUM(s.amount) > 1000; -> Option D
Quick Check:
Use HAVING to filter aggregated groups [OK]
Hint: Use HAVING to filter after GROUP BY aggregation [OK]
Common Mistakes:
Using WHERE instead of HAVING for aggregation filter
Omitting GROUP BY when aggregating
Incorrect JOIN condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.