Hadoop Pig学习笔记(一) 各种SQL在PIG中实现

大数据 Hadoop
我这里以Mysql 5.1.x为例,Pig的版本是0.8

我这里以Mysql 5.1.x为例,Pig的版本是0.8

 

[[145952]]

同时我将数据放在了两个文件,存放在/tmp/data_file_1和/tmp/data_file_2中.文件内容如下:

tmp_file_1:

Txt代码
  1. zhangsan 23 1
  2. lisi 24 1
  3. wangmazi 30 1
  4. meinv 18 0
  5. dama 55 0
zhangsan	23	1
lisi	24	1
wangmazi	30	1
meinv	18	0
dama	55	0

tmp_file_2:

Txt代码
  1. 1 a
  2. 23 bb
  3. 50 ccc
  4. 30 dddd
  5. 66 eeeee
1	a
23	bb
50	ccc
30	dddd
66	eeeee

 

1.从文件导入数据

1)Mysql (Mysql需要先创建表).

CREATE TABLE TMP_TABLE(USER VARCHAR(32),AGE INT,IS_MALE BOOLEAN);

CREATE TABLE TMP_TABLE_2(AGE INT,OPTIONS VARCHAR(50)); -- 用于Join

LOAD DATA LOCAL INFILE '/tmp/data_file_1' INTO TABLE TMP_TABLE ;

LOAD DATA LOCAL INFILE '/tmp/data_file_2' INTO TABLE TMP_TABLE_2;

2)Pig

tmp_table = LOAD '/tmp/data_file_1' USING PigStorage('\t') AS (user:chararray, age:int,is_male:int);

tmp_table_2= LOAD '/tmp/data_file_2' USING PigStorage('\t') AS (age:int,options:chararray);

 

2.查询整张表

1)Mysql

SELECT * FROM TMP_TABLE;

2)Pig

DUMP tmp_table;

3. 查询前50行

1)Mysql

SELECT * FROM TMP_TABLE LIMIT 50;

2)Pig

tmp_table_limit = LIMIT tmp_table 50;

DUMP tmp_table_limit;

4.查询某些列

1)Mysql

SELECT USER FROM TMP_TABLE;

2)Pig

tmp_table_user = FOREACH tmp_table GENERATE user;

DUMP tmp_table_user;

 

5. 给列取别名

1)Mysql

SELECT USER AS USER_NAME,AGE AS USER_AGE FROM TMP_TABLE;

2)Pig

tmp_table_column_alias = FOREACH tmp_table GENERATE user AS user_name,age AS user_age;

DUMP tmp_table_column_alias;

 

6.排序

1)Mysql

SELECT * FROM TMP_TABLE ORDER BY AGE;

2)Pig

tmp_table_order = ORDER tmp_table BY age ASC;

DUMP tmp_table_order;

 

7.条件查询

1)Mysql

SELECT * FROM TMP_TABLE WHERE AGE>20;

2) Pig

tmp_table_where = FILTER tmp_table by age > 20;

DUMP tmp_table_where;

 

8.内连接Inner Join

1)Mysql

SELECT * FROM TMP_TABLE A JOIN TMP_TABLE_2 B ON A.AGE=B.AGE;

2)Pig

tmp_table_inner_join = JOIN tmp_table BY age,tmp_table_2 BY age;

DUMP tmp_table_inner_join;

9.左连接Left Join

1)Mysql

SELECT * FROM TMP_TABLE A LEFT JOIN TMP_TABLE_2 B ON A.AGE=B.AGE;

2)Pig

tmp_table_left_join = JOIN tmp_table BY age LEFT OUTER,tmp_table_2 BY age;

DUMP tmp_table_left_join;

10.右连接Right Join

1)Mysql

SELECT * FROM TMP_TABLE A RIGHT JOIN TMP_TABLE_2 B ON A.AGE=B.AGE;

2)Pig

tmp_table_right_join = JOIN tmp_table BY age RIGHT OUTER,tmp_table_2 BY age;

DUMP tmp_table_right_join;

11.全连接Full Join

1)Mysql

SELECT * FROM TMP_TABLE A JOIN TMP_TABLE_2 B ON A.AGE=B.AGE

UNION SELECT * FROM TMP_TABLE A LEFT JOIN TMP_TABLE_2 B ON A.AGE=B.AGE

UNION SELECT * FROM TMP_TABLE A RIGHT JOIN TMP_TABLE_2 B ON A.AGE=B.AGE;

2)Pig

tmp_table_full_join = JOIN tmp_table BY age FULL OUTER,tmp_table_2 BY age;

DUMP tmp_table_full_join;

 

12.同时对多张表交叉查询

1)Mysql

SELECT * FROM TMP_TABLE,TMP_TABLE_2;

2)Pig

tmp_table_cross = CROSS tmp_table,tmp_table_2;

DUMP tmp_table_cross;

 

13.分组GROUP BY

1)Mysql

SELECT * FROM TMP_TABLE GROUP BY IS_MALE;

2)Pig

tmp_table_group = GROUP tmp_table BY is_male;

DUMP tmp_table_group;

14.分组并统计

1)Mysql

SELECT IS_MALE,COUNT(*) FROM TMP_TABLE GROUP BY IS_MALE;

2)Pig

tmp_table_group_count = GROUP tmp_table BY is_male;

tmp_table_group_count = FOREACH tmp_table_group_count GENERATE group,COUNT($1);

DUMP tmp_table_group_count;
 

15.查询去重DISTINCT

1)MYSQL

SELECT DISTINCT IS_MALE FROM TMP_TABLE;

2)Pig

tmp_table_distinct = FOREACH tmp_table GENERATE is_male;

tmp_table_distinct = DISTINCT tmp_table_distinct;

DUMP tmp_table_distinct;

 
 
责任编辑:李英杰 来源: guoyunsky
相关推荐

2016-10-12 18:58:15

大数据PIGHive

2014-04-01 15:25:18

2019-04-18 10:30:50

大数据Hadoop数据分析

2022-08-05 10:03:17

分布式微服务

2011-08-30 16:26:34

Hadoop

2010-06-03 11:34:42

Hadoop

2010-06-03 11:12:55

Hadoop

2010-06-04 18:17:50

Hadoop集群

2017-02-17 09:14:14

Hadoop

2010-08-06 13:22:48

FlexCSS

2019-04-16 15:18:28

SQLJOIN数据库

2011-08-16 09:48:27

SQLPLUS学习笔记SQL Buffer

2010-03-05 10:50:43

2013-12-10 09:39:00

2023-10-08 07:37:44

SQL连接

2009-05-12 10:55:06

SQLFOR XML模式

2009-08-26 10:48:44

C# SQL命令

2009-09-17 16:20:43

Linq to sql

2009-08-05 15:17:02

NTILE函数

2021-01-23 23:21:46

数据分析编程语言Java
点赞
收藏

51CTO技术栈公众号