什么是Pig?
Pig是一种数据流语言和运行环境,常用于检索和分析数据量较大的数据集。Pig包括两部分:一是用于描述数据流的语言,称为Pig Latin;二是用于运行Pig Latin程序的执行环境。
Pig与Hive的区别?
Pig与Hive作为一种高级数据语言,均运行于HDFS之上,是hadoop上层的衍生架构,用于简化hadoop任务,并对MapReduce进行一个更高层次的封装。Pig与Hive的区别如下:
- Pig是一种面向过程的数据流语言;Hive是一种数据仓库语言,并提供了完整的sql查询功能。
- Pig更轻量级,执行效率更快,适用于实时分析;Hive适用于离线数据分析。
- Hive查询语言为Hql,支持分区;Pig查询语言为Pig Latin,不支持分区。
- Hive支持JDBC/ODBC;Pig不支持JDBC/ODBC。
- Pig适用于半结构化数据(如:日志文件);Hive适用于结构化数据。
Pig Latin语法介绍
下面,将通过与Mysql对比,介绍Pig Latin的常用语法。
1. 导入文件数据
- Mysql
create table tmp_table(name varchar(32), age int, is_child boolean);
create table tmp_table2(age int, user varchar(50), is_child boolean);
load data local infile'/tmp/data_file1' into table tmp_table;
load data local infile'/tmp/data_file2' into table tmp_table2;
- Pig
tmp_table = load'/tmp/data_file1' using PigStorage('\t') as(name:chararray, age:int, is_child:int);
tmp_table2 = load'/tmp/data_file2' using PigStorage('\t') as(age:int, user:chararray, is_child:int);
2. 查询固定行数据
- Mysql
select * from tmp_table limit 50;
- Pig
tmp_table_limit = limit tmp_table 50;
dump tmp_table_limit;
3. 查询指定列数据
- Mysql
select name from tmp_table;
- Pig
tmp_table_name = foreach tmp_table generate name;
dump tmp_table_name;
4. 给列取别名
- Mysql
select name as username, age as userage from tmp_table;
- Pig
tmp_table_column_alias = foreach tmp_table generate name as username, age as userage;
dump tmp_table_column_alias;
5. 按某列排序
- Mysql
select * from tmp_table order by age;
- Pig
tmp_table_order = order tmp_table by age asc;
dump tmp_table_order;
6. 条件查询
- Mysql
select * from tmp_table where age > 18;
- Pig
tmp_table_where = filter tmp_table by age > 18;
dump tmp_table_where;
7. 内连接inner join
- Mysql
select * from tmp_table A join tmp_table2 B on A.age = B.age;
- Pig
tmp_table_inner_join = join tmp_table by age, tmp_table2 by age;
dump tmp_table_inner_join;
8. 左连接left join
- Mysql
select * from tmp_table A left join tmp_table2 B on A.age = B.age;
- Pig
tmp_table_left_join = join tmp_table by age left outer, tmp_table2 by age;
dump tmp_table_left_join;
9. 右链接right join
- Mysql
select * from tmp_table A right join tmp_table2 B on A.age = B.age;
- Pig
tmp_table_right_join = join tmp_table by age right outer,tmp_table2 by age;
dump tmp_table_right_join;
10. 全连接full join
- Mysql
select * from tmp_table A join tmp_table2 B on A.age = B.age Union select * from tmp_table A left join tmp_table2 B on A.age = B.age;
- Pig
tmp_table_full_join = join tmp_table by age full outer,tmp_table2 by age;
dump tmp_table_full_join;
11. 交叉查询多张表
- Mysql
select * from tmp_table,tmp_table2;
- Pig
tmp_table_cross = cross tmp_table,tmp_table2;
dump tmp_table_cross ;
12. 分组group by
- Mysql
select * from tmp_table group by is_child;
- Pig
tmp_table_group = group tmp_table by is_child;
dump tmp_table_group;
13. 分组并统计
- Mysql
select is_male, count(*) from tmp_table group by is_child;
- Pig
tmp_table_group_count = group tmp_table by is_child;
tmp_table_group_count = foreach tmp_table_group_count generate group,count($1);
dump tmp_table_group_count;
14. 查询去重
- Mysql
select distinct is_child from tmp_table;
- Pig
tmp_table_distinct = foreach tmp_table generate is_child;
tmp_table_distinct = distinct tmp_table_distinct;
dump tmp_table_distinct;