A simple data processing example of Pig

Source: Internet
Author: User

1. Pig Data Model

Bag: Table

Tuple: Row, record

Field: attribute

Pig does not require that each tuple in the same bag has the same number or type of fields.

2. Common pig lating statements

1) load: indicates the method for loading data.

2) foreach: perform some processing on a row-by-row scan.

3) filter: filters rows.

4) dump: display the result to the screen

5) store: Save the result to a file

3. Simple Example:

If there is a transcript with student ID, Chinese score, and mathematical score, use | to separate the attributes, as shown below:

 
20130001 | 80 | 9020130002 | 85 | 9620130003 | 60 | 7020130004 | 74 | 8620130005 | 65 | 98

1) upload files from the local system to hadoop

 
[Coder @ H1 hadoop-0.20.2]$ Bin/hadoop DFS-Put/home/coder/score.txt in

Check whether the upload is successful:

 
[Coder @ H1 hadoop-0.20.2]$ Bin/hadoop DFS-ls/user/coder/infound1Items-RW-r --2 coder supergroup 75/user/coder/In/score.txt

2) load the original data and use load

 
Grunt> scores = load 'hdfs: // H1: 9000/user/coder/In/Score.txt 'using pigstorage ('|') as (Num: int, Chinese: int, Math: INT );

The input file is 'hdfs: // H1: 9000/user/coder/In/Score.txt'

Table Name (BAG): scores

When reading data from an input file (tuple), it is separated by |

The read tuple contains three attributes: Student ID (Num), Chinese score (Chinese), and math. The data types of these attributes are Int.

3) view the table structure

 
Grunt>Describe scores; scores: {num: int, Chinese: int, Math: int}

4) if we need to filter out records whose student ID is 20130005

Grunt> filter_scores =FilterScores by num! = 20130005;

View filtered records

 
Grunt>Dump filter_scores ;(20130001,80, 90)(, 96)(, 60, 70)(, 74, 86)

5) calculate the total score of each person

 
Grunt> totalscore =ForeachScores generate num, Chinese + math;

View results:

 
Grunt> dump totalscore;

 

(20130001,170)(20130002,181)(20130003,130)(20130004,160)
(20130005,163)

6) output the total score of each person to the file.

 
Grunt> store totalscore into 'hdfs: // H1: 9000/user/coder/out/result 'using pigstorage (' | ');

View results:

[Coder @ H1 ~] $ Hadoop DFS-ls/user/coder/out/Resultfound2Itemsdrwxr-XR-X-coder supergroup 0 2013-04-20 :54/user/coder/out/result/_ Logs-RW-r -- 2 coder supergroup 65 2013-04-20/user/coder/out/result/part-m-00000[Coder@ H1 ~] $^ C [coder@ H1 ~] $ Hadoop DFS-CAT/user/coder/out/result/* 20130001 | 17020130002 | 18120130003 | 13020130004 | 16020130005 | 163Cat: source must be a file. [coder@ H1 ~] $

 

Let's look at another small example:

There are a batch of files in the following format:

Zhangsan #123456.# Zhangsan@qq.comlisi #434 dfdds # Lisi @126. Comwangwu # ffere233 # wangwu @163. Comzhouliu # fgrtr43 # zhouliu @139. com

Each line of record has three fields: account, password, and email address. The fields are separated by #. Now, you need to extract the mailboxes in these files.

1) upload files to hadoop

 
[Coder @ H1 hadoop-0.20.2]$ Bin/hadoop DFS-put data.txt in

 

[Coder @ H1 hadoop-0.20.2] $ bin/hadoop DFS-ls/user/coder/InFound1Items-RW-r -- 2 coder supergroup 122 2013-04-24 20:34/user/coder/In/Data.txt [coder@ H1 hadoop-0.20.2] $

2) load the original data file

 
Grunt> T_A = load'/user/coder/In/Data.txt 'using pigstorage ('#') As (username: chararray, password: chararray, email: chararray );

3) retrieve the email Field

 
Grunt> T_ B =ForeachT_A generate email;

4) output the result to the file

Grunt> store T_ B into '/user/coder/out/Email'

5) view results

 
[Coder @ H1 hadoop-0.20.2] $ bin/hadoop DFS-CAT/user/coder/out/email /*Zhangsan@QQ. comlisi126. Comwangwu163. Comzhouliu139. Comcat: source must be a file.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.