pig课件 框架组件 安装配置 使用 运算符 运行脚本 常用函数

Apache Pig架构

用于使用Pig分析Hadoop中的数据的语言称为 Pig Latin ,是一种高级数据处理语言,它提供了一组丰富的数据类型和操作符来对数据执行各种操作。

要执行特定任务时,程序员使用Pig,需要用Pig Latin语言编写Pig脚本,并使用任何执行机制(Grunt Shell,UDFs,Embedded)执行它们。执行后,这些脚本将通过应用Pig框架的一系列转换来生成所需的输出。

在内部,Apache Pig将这些脚本转换为一系列MapReduce作业,因此,它使程序员的工作变得容易。Apache Pig的架构如下所示。

Apache Pig组件

如图所示,Apache Pig框架中有各种组件。让我们来看看主要的组件。

Parser(解析器)

最初,Pig脚本由解析器处理,它检查脚本的语法,类型检查和其他杂项检查。解析器的输出将是DAG(有向无环图),它表示Pig Latin语句和逻辑运算符。在DAG中,脚本的逻辑运算符表示为节点,数据流表示为边。

Optimizer(优化器)

逻辑计划(DAG)传递到逻辑优化器,逻辑优化器执行逻辑优化,例如投影和下推。

Compiler(编译器)

编译器将优化的逻辑计划编译为一系列MapReduce作业。

Execution engine(执行引擎)

最后,MapReduce作业以排序顺序提交到Hadoop。这些MapReduce作业在Hadoop上执行,产生所需的结果。

Pig Latin数据模型

Pig Latin的数据模型是完全嵌套的,它允许复杂的非原子数据类型,例如 map  tuple 。下面给出了Pig Latin数据模型的图形表示。

Atom(原子)

Pig Latin中的任何单个值,无论其数据类型,都称为 Atom 。它存储为字符串,可以用作字符串和数字。int,long,float,double,chararray和bytearray是Pig的原子值。一条数据或一个简单的原子值被称为字段:“raja“或“30"

Tuple(元组)

由有序字段集合形成的记录称为元组,字段可以是任何类型。元组与RDBMS表中的行类似。例:(Raja,30)

Bag(包)

一个包是一组无序的元组。换句话说,元组(非唯一)的集合被称为包。每个元组可以有任意数量的字段(灵活模式)。包由“{}"表示。它类似于RDBMS中的表,但是与RDBMS中的表不同,不需要每个元组包含相同数量的字段,或者相同位置(列)中的字段具有相同类型。

:{(Raja,30),(Mohammad,45)}

包可以是关系中的字段;在这种情况下,它被称为内包(inner bag)

:{Raja,30, {9848022338,[email protected],} }

Map(映射)

映射(或数据映射)是一组key-value对。key需要是chararray类型,且应该是唯一的。value可以是任何类型,它由“[]"表示,

:[name#Raja,age#30]

Relation(关系)

一个关系是一个元组的包。Pig Latin中的关系是无序的(不能保证按任何特定顺序处理元组)。

 

Apache Pig 安装

先决条件

在你运行Apache Pig之前,必须在系统上安装好Hadoop和Java。

下载ApachePig

首先,从以下网站下载最新版本的Apache Pig:https://pig.apache.org/

pig-0.16.0.tar.gz

 

安装ApachePig

下载Apache Pig软件后,按照以下步骤将其安装在Linux环境中。

步骤1

在安装了 Hadoop,Java和其他软件的安装目录的同一目录中创建一个名为Pig的目录。(在我们的教程中,我们在名为Hadoop的用户中创建了Pig目录)。

$ mkdir Pig

第2步

提取下载的tar文件,如下所示。

$ cd Downloads/ 
$ tar zxvf pig-0.15.0-src.tar.gz 
$ tar zxvf pig-0.15.0.tar.gz 

步骤3

 pig-0.16.0-src.tar.gz 文件的内容移动到之前创建的 Pig 目录,如下所示。

$ mv pig-0.16.0-src.tar.gz/* /home/Hadoop/Pig/

配置ApachePig

安装Apache Pig后,我们必须配置它。要配置,我们需要编辑两个文件 - bashrcpig.properties 

.bashrc文件

 .bashrc 文件中,设置以下变量

·     

PIG_HOME 文件夹复制到Apache Pig的安装文件夹

PATH 环境变量复制到bin文件夹

PIG_CLASSPATH 环境变量复制到安装Hadoop的etc(配置)文件夹(包含core-site.xml,hdfs-site.xml和mapred-site.xml文件的目录)。

·     

export PIG_HOME = /home/Hadoop/Pig
export PATH  = PATH:/home/Hadoop/pig/bin
export PIG_CLASSPATH = $HADOOP_HOME/conf

pig.properties文件

在Pig的 conf 文件夹中,我们有一个名为 pig.properties 的文件。在pig.properties文件中,可以设置如下所示的各种参数。

pig -h properties 

支持以下属性:

Logging: verbose = true|false; default is false. This property is the same as -v
       switch brief=true|false; default is false. This property is the same 
       as -b switch debug=OFF|ERROR|WARN|INFO|DEBUG; default is INFO.             
       This property is the same as -d switch aggregate.warning = true|false; default is true. 
       If true, prints count of warnings of each type rather than logging each warning.           
                   
Performance tuning: pig.cachedbag.memusage=; default is 0.2 (20% of all memory).
       Note that this memory is shared across all large bags used by the application.         
       pig.skewedjoin.reduce.memusagea=; default is 0.3 (30% of all memory).
       Specifies the fraction of heap available for the reducer to perform the join.
       pig.exec.nocombiner = true|false; default is false.
           Only disable combiner as a temporary workaround for problems.         
       opt.multiquery = true|false; multiquery is on by default.
           Only disable multiquery as a temporary workaround for problems.
       opt.fetch=true|false; fetch is on by default.
           Scripts containing Filter, Foreach, Limit, Stream, and Union can be dumped without MR jobs.         
       pig.tmpfilecompression = true|false; compression is off by default.             
           Determines whether output of intermediate jobs is compressed.         
       pig.tmpfilecompression.codec = lzo|gzip; default is gzip.
           Used in conjunction with pig.tmpfilecompression. Defines compression type.         
       pig.noSplitCombination = true|false. Split combination is on by default.
           Determines if multiple small files are combined into a single map.         
                                
       pig.exec.mapPartAgg = true|false. Default is false.             
           Determines if partial aggregation is done within map phase, before records are sent to combiner.         
       pig.exec.mapPartAgg.minReduction=. Default is 10.             
           If the in-map partial aggregation does not reduce the output num records by this factor, it gets disabled.
                                
Miscellaneous: exectype = mapreduce|tez|local; default is mapreduce. This property is the same as -x switch
       pig.additional.jars.uris=. Used in place of register command.
       udf.import.list=. Used to avoid package names in UDF.
       stop.on.failure = true|false; default is false. Set to true to terminate on the first error.         
       pig.datetime.default.tz=. e.g. +08:00. Default is the default timezone of the host.
           Determines the timezone used to handle datetime datatype and UDFs.
Additionally, any Hadoop property can be specified.

验证安装

通过键入version命令验证Apache Pig的安装。如果安装成功,你将获得Apache Pig的正式版本,如下所示。

$ pig –version 
 
Apache Pig version 0.16.0 (r1682971)  
compiled Jun 01 2015, 11:44:35

 

Apache Pig 执行

Apache Pig执行模式

你可以以两种模式运行Apache Pig,即Local(本地)模式HDFS模式

Local模式

在此模式下,所有文件都从本地主机和本地文件系统安装和运行,不需要Hadoop或HDFS。此模式通常用于测试目的。

MapReduce模式

MapReduce模式是我们使用Apache Pig加载或处理Hadoop文件系统(HDFS)中存在的数据的地方。在这种模式下,每当我们执行Pig Latin语句来处理数据时,会在后端调用一个MapReduce作业,以对HDFS中存在的数据执行特定的操作。

Apache Pig执行机制

Apache Pig脚本可以通过三种方式执行,即交互模式,批处理模式和嵌入式模式。

交互模式(Gruntshell) - 你可以使用Grunt shell以交互模式运行Apache Pig。在此shell中,你可以输入Pig Latin语句并获取输出(使用Dump运算符)。

批处理模式(脚本) -你可以通过将Pig Latin脚本写入具有 .pig 扩展名的单个文件中,以批处理模式运行Apache Pig。

嵌入式模式(UDF) - Apache Pig允许在Java等编程语言中定义我们自己的函数(UDF用户定义函数),并在我们的脚本中使用它们。

·     

调用GruntShell

你可以使用“-x"选项以所需的模式(local/MapReduce)调用Grunt shell,如下所示。

Local模式

MapReduce模式

Command(命令) -

$ ./pig -x local

Command(命令)-

$ ./pig -x mapreduce

Output(输出) -

Output(输出)-

这两个命令都给出了Grunt shell提示符,如下所示。

grunt>

你可以使用“ctrl+d"退出Grunt shell。

在调用Grunt shell之后,可以通过直接输入Pig中的Pig Latin语句来执行Pig脚本。

grunt> customers = LOAD 'customers.txt' USING PigStorage(',');

在批处理模式下执行Apache Pig

你可以在文件中编写整个Pig Latin脚本,并使用 -xcommand 执行它。我们假设在一个名为 sample_script.pig 的文件中有一个Pig脚本,如下所示。

Sample_script.pig

student = LOAD 'hdfs://localhost:9000/pig_data/student.txt' USING
   PigStorage(',') as (id:int,name:chararray,city:chararray);
  
Dump student;

现在,你可以在上面的文件中执行脚本,如下所示。

Local模式

MapReduce模式

$ pig -x local Sample_script.pig

$ pig -x mapreduce Sample_script.pig

注意:我们将详细讨论如何在批处理模式嵌入模式中运行Pig脚本。

 

Apache Pig Grunt Shell

调用Grunt shell后,可以在shell中运行Pig脚本。除此之外,还有由Grunt shell提供的一些有用的shell和实用程序命令。本章讲解的是Grunt shell提供的shell和实用程序命令。

注意:在本章的某些部分中,使用了LoadStore等命令。请参阅相应章节以获取有关它们的详细信息。

Shell 命令

Apache Pig的Grunt shell主要用于编写PigLatin脚本。在此之前,我们可以使用 sh  fs 来调用任何shell命令。

sh 命令

使用 sh 命令,我们可以从Gruntshell调用任何shell命令,但无法执行作为shell环境( ex - cd)一部分的命令。

语法

下面给出了 sh 命令的语法。

grunt> sh shell command parameters

示例

我们可以使用 sh 选项从Gruntshell中调用Linux shell的 ls 命令,如下所示。在此示例中,它列出了 /pig/bin/ 目录中的文件。

grunt> sh ls
   
pig 
pig_1444799121955.log 
pig.cmd 
pig.py

fs命令

使用 fs 命令,我们可以从Gruntshell调用任何FsShell命令。

语法

下面给出了 fs 命令的语法。

grunt> sh File System command parameters

示例

我们可以使用fs命令从Grunt shell调用HDFS的ls命令。在以下示例中,它列出了HDFS根目录中的文件。

grunt> fs –ls
  
Found 3 items
drwxrwxrwx   - Hadoop supergroup          0 2015-09-08 14:13 Hbase
drwxr-xr-x   - Hadoop supergroup          0 2015-09-09 14:52 seqgen_data
drwxr-xr-x   - Hadoop supergroup          0 2015-09-08 11:30 twitter_data

以同样的方式,我们可以使用 fs 命令从Grunt shell中调用所有其他文件系统的shell命令。

实用程序命令

Grunt shell提供了一组实用程序命令。这些包括诸如clear,help,history,quitset等实用程序命令;以及Grunt shell中诸如 exec,killrun等命令来控制Pig。下面给出了Grunt shell提供的实用命令的描述。

clear命令

clear 命令用于清除Grunt shell的屏幕。

语法

你可以使用 clear 命令清除gruntshell的屏幕,如下所示。

grunt> clear

help命令

help 命令提供了Pig命令或Pig属性的列表。

使用

你可以使用 help 命令获取Pig命令列表,如下所示。

grunt> help
 
Commands: ; - See the PigLatin manual for details:
http://hadoop.apache.org/pig
  
File system commands:fs  - Equivalent to Hadoop dfs  command:
http://hadoop.apache.org/common/docs/current/hdfs_shell.html
       
Diagnostic Commands:describe [:: 
   
Inner aliases can be described as A::B.
    explain [-script ] [-out ] [-brief] [-dot|-xml] 
       [-param =]
       [-param_file ] [] - 
       Show the execution plan to compute the alias or for entire script.
       -script - Explain the entire script.
       -out - Store the output into directory rather than print to stdout.
       -brief - Don't expand nested plans (presenting a smaller graph for overview).
       -dot - Generate the output in .dot format. Default is text format.
       -xml - Generate the output in .xml format. Default is text format.
       -param  
   
       -param_file  - See parameter substitution for details.
       alias - Alias to explain.
       dump  - Compute the alias and writes the results to stdout.
 
Utility Commands: exec [-param =param_value] [-param_file ] 
                    
                    

你可能感兴趣的:(大数据)