测试数据位于:/home/hadoop/luogankun/workspace/sync_data/pig
person.txt中的数据以逗号分隔

1,zhangsan,112
2,lisi,113
3,wangwu,114
4,zhaoliu,115

score.txt中的数据以制表符分隔

1       20
2 30
3 40
5 50

pig只能针对HDFS上的文件进行操作,所以需要将文件先上传到HDFS中

cd /home/hadoop/luogankun/workspace/sync_data/pig
hadoop fs -put person.txt input/pig/person.txt
hadoop fs -put score.txt input/pig/score.txt

load文件(HDFS系统上的)

a = load 'input/pig/person.txt' using PigStorage(',') as (id:int, name:chararray, age:int);
b = load 'input/pig/score.txt' using PigStorage('\t') as (id:int, score:int);

查看表结构

describe a
a: {id: int,name: chararray,age: int} describe b
b: {id: int,score: int}

查看表数据

dump a
(1,zhangsan,112)
(2,lisi,113)
(3,wangwu,114)
(4,zhaoliu,115) dump b
(1,20)
(2,30)
(3,40)
(5,50)

dump 会跑mapreduce任务。

条件过滤

查询person中id小于4的人

aa = filter a by id < 4;

dump aa;
(1,zhangsan,112)
(2,lisi,113)
(3,wangwu,114)

pig中等号使用==, 例如:aa = filter a by id == 4;

表关联

c = join a by id left , b by id;

describe c
c: {a::id: int,a::name: chararray,a::age: int,b::id: int,b::score: int}
#表名字段名之间两个冒号,字段与字段类型之间一个冒号 dump c
(1,zhangsan,112,1,20)
(2,lisi,113,2,30)
(3,wangwu,114,3,40)
(4,zhaoliu,115,,)

由于采用的是left join,所以只有四条数据,而且第四条数据是没有分数的。

迭代数据

d =foreach c generate a::id as id, a::name as name, b::score as score, a::age as age;

describe d;
d: {id: int,name: chararray,score: int,age: int} dump d
(1,zhangsan,20,112)
(2,lisi,30,113)
(3,wangwu,40,114)
(4,zhaoliu,,115)

注意:foreach使用时只要等号前或者后有一个空格即可,如果等号两端都没有空格的话会报错。

处理结果存储到HDFS系统上

store d into 'output/pig/person_score' using PigStorage(',');   #导出到HDFS上的文件分隔符是逗号
hadoop fs -ls output/pig/person_score
hadoop fs -cat output/pig/person_score/part-r-00000
1,zhangsan,20,112
2,lisi,30,113
3,wangwu,40,114
4,zhaoliu,,115 hadoop fs -rmr output/pig/person_score
store d into 'output/pig/person_score'; #导出到HDFS上的文件分隔符是制表符
hadoop fs -ls output/pig/person_score
hadoop fs -cat output/pig/person_score/part-r-00000
1 zhangsan 20 112
2 lisi 30 113
3 wangwu 40 114
4 zhaoliu 115

pig执行文件

将上面的所有pig shell脚本放到一个sh脚本中执行
/home/hadoop/luogankun/workspace/shell/pig/person_score.pig

a = load 'input/pig/person.txt' using PigStorage(',') as (id:int, name:chararray, age:int);
b = load 'input/pig/score.txt' using PigStorage('\t') as (id:int, score:int);
c = join a by id left , b by id;
d =foreach c generate a::id as id, a::name as name, b::score as score, a::age as age;
store d into 'output/pig/person_score2' using PigStorage(',');

执行person.score.pig脚本:

/home/hadoop/luogankun/workspace/shell/pig

pig person_score.pig

pig脚本传递参数

pig脚本位置:/home/hadoop/luogankun/workspace/shell/pig/mulit_params_demo01.pig

log = LOAD '$input' AS (user:chararray, time:long, query:chararray);
lmt = LIMIT log $size;
DUMP lmt;

上传数据到hdfs文件中

cd /home/hadoop/luogankun/workspace/shell/pig
hadoop fs -put excite-small.log input/pig/excite-small.log

传递方式一:逐个参数传递

pig -param input=input/pig/excite-small.log -param size=4 mulit_params_demo01.pig

传递方式二:将参数保存在txt文件中

/home/hadoop/luogankun/workspace/shell/pig/mulit_params.txt

input=input/pig/excite-small.log
size=5
pig -param_file mulit_params.txt mulit_params_demo01.pig

pig入门案例的更多相关文章

  1. SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)

    SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...

  2. SpringMvc核心流程以及入门案例的搭建

    1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...

  3. Struts2第一个入门案例

      一.如何获取Struts2,以及Struts2资源包的目录结构的了解    Struts的官方地址为http://struts.apache.org 在他的主页当中,我们可以通过左侧的Apache ...

  4. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  5. Hibernate入门案例及增删改查

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  6. Quartz应用实践入门案例二(基于java工程)

    在web应用程序中添加定时任务,Quartz的简单介绍可以参看博文<Quartz应用实践入门案例一(基于Web应用)> .其实一旦学会了如何应用开源框架就应该很容易将这中框架应用与自己的任 ...

  7. Quartz应用实践入门案例一(基于Web环境)

    Quartz是一个完全由java编写的开源作业调度框架,正是因为这个框架整合了许多额外的功能,所以在使用上就显得相当容易.只是需要简单的配置一下就能轻松的使用任务调度了.在Quartz中,真正执行的j ...

  8. MyBatis入门案例 增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  9. Hibernate入门案例 增删改

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

随机推荐

  1. python3:xlrd、xlwt、xlutils处理excel文件

    1.xlrd读取excel 请参考上篇博客https://www.cnblogs.com/shapeL/p/9075843.html 2.xlwt生成excel 安装下载:pip install xl ...

  2. Oracle Statistic 统计信息 小结

    oraclestatisticstabledatabasesqldictionary   目录(?)[-] 直方图上列的信息说明 直方图类型说明   一.  Statistic 说明 Oracle 官 ...

  3. QT 5.4.1 for Android Ubuntu QtWebView Demo

    QT 5.4.1 for Android Ubuntu QtWebView Demo 2015-5-15 目录 一.说明: 二.参考文章: 三.QtWebView Demo在哪里? 四.Qt Crea ...

  4. Markdown的写法

    这里只介绍Markdown的书写格式.在github中显示出来. 当你看到下面两张图片时,你会发现区别还是蛮大的. 标题: 首先>就是最左边的那条竖线,但是=(最高阶标题)和-(第二阶标题). ...

  5. sql serve 创建序列

    Oracle中有sequence的功能,SQL Server类似的功能使用Identity列实现,但是有很大的局限性. 在2012中,微软终于增加了 sequence 对象,功能和性能都有了很大的提高 ...

  6. FastAdmin 自己做的插件 SQL 有一个表没有生成成功

    群里有群友问: 给插件建的install.sql 里有三个表,为啥会出现安装成功后没有错误提示,只生成了两个表的情况..这可能会是什么...原因 第一感觉和 FastAdmin 没有关系. 没生成表, ...

  7. Mfs+drbd+keepalived实现mfs系统高可用

    http://blog.sina.com.cn/s/blog_53c654720102wo1k.html Moosefs分布式文件系统是一个易用的系统,但其只有在Pro版中提供了master的高可用方 ...

  8. [boost] : test库

    最小化的测试套件minimal_test test库提供一个最小化的测试套件minimal_test, 类似lightweight_test适合入门级测试. 需要包含文件文#include <b ...

  9. Input输入控制

    1.Input只能输入正整数 <html> <head> <title>只能输入正整数</title> </head> <body&g ...

  10. 在Windows下使用svn命令行教程及svn命令行的解释

    本文转载自:https://blog.csdn.net/yangxiao2shi/article/details/50719286/ 以前在公司一直使用git,现在的公司改用svn,一时间还真的不知道 ...