Pig Latin helps you write simple steps to process big data easily. It makes working with large data sets faster and clearer.
Pig Latin basics in Hadoop
alias = LOAD 'datafile' USING loader AS (field1:type, field2:type, ...); filtered = FILTER alias BY condition; grouped = GROUP alias BY field; result = FOREACH grouped GENERATE group, COUNT(alias); STORE result INTO 'output';
Each step creates a new alias (name) for the data.
Use LOAD to read data, FILTER to select rows, GROUP to collect by key, FOREACH to process groups, and STORE to save results.
data = LOAD 'users.csv' USING PigStorage(',') AS (name:chararray, age:int, city:chararray);
adults = FILTER data BY age >= 18;grouped_by_city = GROUP adults BY city;
count_by_city = FOREACH grouped_by_city GENERATE group, COUNT(adults);
This program loads user data, filters adults, groups them by city, counts adults per city, and shows the result.
users = LOAD 'users.csv' USING PigStorage(',') AS (name:chararray, age:int, city:chararray); adults = FILTER users BY age >= 18; grouped = GROUP adults BY city; count_by_city = FOREACH grouped GENERATE group AS city, COUNT(adults) AS adult_count; DUMP count_by_city;
Always define the schema (field names and types) when loading data for clarity.
Use DUMP to see results immediately during testing, STORE to save results permanently.
Pig Latin commands run in order, each step uses the previous step's output.
Pig Latin is a simple language to process big data step-by-step.
Use LOAD, FILTER, GROUP, FOREACH, and STORE to handle data easily.
It helps you clean, group, count, and save big data results quickly.
Practice
FILTER statement in Pig Latin?Solution
Step 1: Understand the role of FILTER
The FILTER statement is used to keep only the rows that satisfy a given condition.Step 2: Compare with other statements
LOAD reads data, GROUP groups data, STORE saves data, so FILTER is for selecting rows.Final Answer:
To select rows that meet a condition -> Option AQuick Check:
FILTER = select rows [OK]
- Confusing FILTER with GROUP
- Thinking FILTER saves data
- Mixing FILTER with LOAD
data.txt in Pig Latin?Solution
Step 1: Check the LOAD syntax
LOAD requires the file path as a string in quotes and the loader function with parentheses.Step 2: Validate each option
LOAD 'data.txt' USING PigStorage(); uses quotes and parentheses correctly. LOAD data.txt USING PigStorage(); misses quotes. LOAD 'data.txt' AS PigStorage(); uses AS instead of USING. LOAD 'data.txt' USING PigStorage misses parentheses.Final Answer:
LOAD 'data.txt' USING PigStorage(); -> Option AQuick Check:
LOAD 'file' USING loader(); [OK]
- Forgetting quotes around filename
- Using AS instead of USING
- Omitting parentheses after loader
A = LOAD 'numbers.txt' USING PigStorage(',');
B = FILTER A BY $0 > 10;
C = FOREACH B GENERATE $0;
DUMP C;What will be the output if
numbers.txt contains:5,abc
15,def
8,ghi
20,jkl
Solution
Step 1: Understand the FILTER condition
FILTER keeps rows where first column ($0) is greater than 10, so rows with 15 and 20 remain.Step 2: Understand FOREACH GENERATE
FOREACH selects only the first column ($0), so output shows only 15 and 20 as single values.Final Answer:
(15) (20) -> Option DQuick Check:
FILTER $0 > 10 then select $0 [OK]
- Including second column in output
- Selecting rows less than or equal to 10
- Confusing FILTER condition
data = LOAD 'input.txt' USING PigStorage(',');
filtered = FILTER data BY $1 == 'USA';
grouped = GROUP filtered BY $2;
result = FOREACH grouped GENERATE group, COUNT(filtered);
STORE result INTO 'output';Solution
Step 1: Check STORE syntax
STORE requires a USING clause to specify the storage format, e.g., PigStorage().Step 2: Verify other statements
GROUP uses filtered correctly, COUNT uses filtered correctly, FILTER condition is valid if $1 is country column.Final Answer:
STORE command missing USING clause -> Option BQuick Check:
STORE ... INTO 'path' USING loader() [OK]
- Omitting USING in STORE
- Confusing GROUP input
- Misunderstanding COUNT argument
product_id, region, amount. You want to find total sales per region but only for sales above 100. Which Pig Latin sequence correctly achieves this?Solution
Step 1: Identify correct column indexes
Columns: product_id ($0), region ($1), amount ($2). Filter by amount > 100 means FILTER BY $2 > 100.Step 2: Group by region and sum amount
Group by $1 (region), then sum B.$2 (amount) in FOREACH. STORE needs USING clause.Step 3: Check each option
A = LOAD 'sales.txt' USING PigStorage(','); B = FILTER A BY $2 > 100; C = GROUP B BY $1; D = FOREACH C GENERATE group, SUM(B.$2); STORE D INTO 'result' USING PigStorage(); matches all requirements: correct filter, group, sum, and STORE syntax.Final Answer:
Option C code sequence -> Option CQuick Check:
Filter $2>100, group $1, sum $2, STORE with USING [OK]
- Using wrong column indexes for filter or group
- Omitting USING in STORE
- Summing wrong column
Ready to prove it?
Pass the test and this pattern moves unknown
