pig入門案例

2021-09-06 17:27:35 字數 4499 閱讀 2294

測試資料位於:/home/hadoop/luogankun/workspace/sync_data/pig

person.txt中的資料以逗號分隔

1,zhangsan,112

2,lisi,113

3,wangwu,114

4,zhaoliu,115

score.txt中的資料以製表符分隔

1202

303405

50

pig只能針對hdfs上的檔案進行操作,所以需要將檔案先上傳到hdfs中

cd /home/hadoop/luogankun/workspace/sync_data/

pighadoop 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: describe b

b:

檢視表資料

dumpa(

1,zhangsan,112)(

2,lisi,113)(

3,wangwu,114)(

4,zhaoliu,115

)dumpb(

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:#表名欄位名之間兩個冒號,欄位與字段型別之間乙個冒號

dumpc(

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: dumpd(

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

1122 lisi 30

1133 wangwu 40

1144 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/

pighadoop fs

-put excite-small.log input/pig/excite-small.log

傳遞方式一:逐個引數傳遞

pig -paraminput=input/pig/excite-small.log

-paramsize=

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_filemulit_params.txtmulit_params_demo01.pig

Pig簡單入門

pig專門用來處理來自於hdfs的資料,它提供了一套流式的資料處理語言,轉化為map reduce來處理hdfs的資料 pig包括用來描述資料分析程式的高階程式語言,以及對這些程式進行評估的基礎結構。pig突出的特點就是它的結構經得起 大量並行任務的檢驗,這使得它能夠處理大規模資料集。使用pig命令...

Pig入門 環境搭建

pig入門 環境搭建 本文介紹在linux redhat hadoop2.2.0 jdk1.7的環境下安裝pig 0.14.0.二 安裝及配置 1 解壓至安裝目錄 比如 tar zxvf pig 0.14.0.tar.gz c itcast 2 配置 編輯 bash profile檔案 新增 exp...

Hibernate入門 入門案例

4.1 資料庫建立表 create table cst customer cust id bigint 32 not null auto increment comment 客戶編號 主鍵 cust name varchar 32 not null comment 客戶名稱 公司名稱 cust so...