Introduction
We use Pig and Hive to work with big data easily. They help us write simple commands instead of complex code.
We use Pig and Hive to work with big data easily. They help us write simple commands instead of complex code.
Pig Latin example: A = LOAD 'data' AS (name:chararray, age:int); B = FILTER A BY age > 30; STORE B INTO 'output'; HiveQL example: SELECT name, age FROM data WHERE age > 30;
Pig uses a scripting language called Pig Latin.
Hive uses HiveQL, which looks like SQL.
Pig Latin: A = LOAD 'users' AS (id:int, name:chararray, salary:int); B = FILTER A BY salary > 50000; DUMP B;
HiveQL:
SELECT id, name FROM users WHERE salary > 50000;This example shows how to filter employees older than 25 and count them by department using Pig and Hive.
/* Pig Latin script example */ A = LOAD 'employee_data' AS (name:chararray, age:int, department:chararray); B = FILTER A BY age > 25; C = GROUP B BY department; D = FOREACH C GENERATE group, COUNT(B); DUMP D; /* HiveQL equivalent */ -- CREATE TABLE employee_data(name STRING, age INT, department STRING); -- SELECT department, COUNT(*) FROM employee_data WHERE age > 25 GROUP BY department;
Pig is better for data transformation and procedural tasks.
Hive is better for data summarization and ad-hoc queries.
Both run on Hadoop and convert scripts/queries into MapReduce jobs.
Pig uses a scripting language; Hive uses SQL-like queries.
Pig is procedural; Hive is declarative.
Choose Pig for complex data flows, Hive for easy querying.
users?SELECT * FROM table_name;.data = LOAD 'input' AS (name:chararray, age:int);
filtered = FILTER data BY age > 25;
DUMP filtered;
('Alice', 30)
('Bob', 22)
('Carol', 27)SELECT name age FROM users WHERE age > 20;
Pass the test and this pattern moves unknown