继续扫盲,今天尝试pig的安装使用
Apache Pig 是一个高级过程语言,适合于使用 Hadoop 和 MapReduce 平台来查询大型半结构化数据集。通过允许对分布式数据集进行类似 SQL 的查询,Pig 可以简化 Hadoop 的使用。
使用pig,可以不用编写java程序,用几句简单的命令就处理复杂的java编程才能完成的MapReduce程序
1.下载
http://pig.apache.org/releases.html#19+June%2C+2017%3A+release+0.17.0+available
我下载的是最新版本 pig-0.17.0.tar.gz
2.解压
tar -zxvf pig-0.17.0.tar.gz
3.配置
无需配置,按默认设置就可运行,为了方便使用,可以修改 /etc/profile文件,加入:
export PIG_HOME=/home/linbin/software/pig-0.17.0
export PIG_CLASSPATH = $HADOOP_HOME/etc/hadoop
export PATH=$PATH:$PIG_HOME/bin
让设置生效:
source /etc/profile
另外,根据需要修改配置文件 pig-0.17.0/conf/pig.properties 都按默认值也能测试运行
4.测试
[root@centos7 bin]# pig -version
Apache Pig version 0.17.0 (r1797386)
compiled Jun 02 2017, 15:41:58
5. 执行
Apache Pig脚本可以通过三种方式执行,即交互模式,批处理模式和嵌入式模式。
交互模式是shell模式,批处理是脚本提交模式,嵌入式是定义函数模式。
另外 在使用Grunt shell时,通过“-x"选项以所需的模式(local/MapReduce)(即本地模式,hadoop模式)调用
local本地模式
[root@centos7 bin]# pig -x local
...
grunt>
MapReduce模式
先启动hadoop,然后:
[root@centos7 bin]# pig -x mapreduce
...
grunt>
6.第一个例子helloworld
(1)先准备数据
[root@centos7 bin]# hadoop fs -text /user/root/pig/pig_data/student_data.txt
001,Rajiv,Reddy,9848022337,Hyderabad
002,siddarth,Battacharya,9848022338,Kolkata
003,Rajesh,Khanna,9848022339,Delhi
004,Preethi,Agarwal,9848022330,Pune
005,Trupthi,Mohanthy,9848022336,Bhuwaneshwar
006,Archana,Mishra,9848022335,Chennai
(2) 测试: 载入数据,显示数据,存储数据
bin/pig -x mapreduce
grunt> student = LOAD 'hdfs://centos7:8020/user/root/pig/pig_data/student_data.txt'
USING PigStorage(',')
as ( id:int, firstname:chararray, lastname:chararray, phone:chararray,
city:chararray );
grunt>dump student
grunt> STORE student INTO 'hdfs://centos7:8020/user/root/pig/pig_Output/' USING PigStorage (',');
通过显示的执行过程可见,每次执行都转化为map/reduce程序提交运行
7.使用教程
教程网址: https://www.w3cschool.cn/apache_pig/apache_pig_overview.html