• 大数据Hadoop之——数据分析引擎Apache Pig


    目录

    一、Apache Pig概述

    Apache PIG提供一套高级语言平台,用于对结构化与非结构化数据集进行操作与分析。这种语言被称为Pig Latin,其属于一种脚本形式,可直接立足于PIG shell执行或者通过Pig Server进行触发。用户所创建的脚本会在初始阶段由Pig Latin处理引擎进行语义有效性解析,而后被转换为包含整体执行初始逻辑的定向非循环图(简称DAG)。

    官网:https://pig.apache.org/
    官方文档:https://pig.apache.org/docs/r0.17.0/

    Apache Pig具有以下特点:

    • 丰富的运算符集 - 它提供了许多运算符来执行诸如join,sort,filer等操作。

    • 易于编程 - Pig Latin与SQL类似,如果你善于使用SQL,则很容易编写Pig脚本。

    • 优化机会 - Apache Pig中的任务自动优化其执行,因此程序员只需要关注语言的语义。

    • 可扩展性 - 使用现有的操作符,用户可以开发自己的功能来读取、处理和写入数据。

    • 用户定义函数 - Pig提供了在其他编程语言(如Java)中创建用户定义函数的功能,并且可以调用或嵌入到Pig脚本中。

    • 处理各种数据 - Apache Pig分析各种数据,无论是结构化还是非结构化,它将结果存储在HDFS中。

    Pig包括两部分:

    • 用于描述数据流的语言,称为 Pig Latin,Pig Latin是类似SQL的语言。
    • 用于运行PigLatin程序的 执行环境 。一个是 本地 的单JVM执行环境,一个就是在 hadoop集群上的分布式执行环境

    二、Apache Pig架构

    1)架构图

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

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

    2)Apache Pig组件

    1、Parser(解析器)

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

    2、Optimizer(优化器)

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

    3、Compiler(编译器)

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

    4、Execution engine(执行引擎)

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

    三、Apache Pig安装

    如果使用的不是本地模式,就是必须安装好Haood基础环境,可以参考我之前的文章:
    大数据Hadoop原理介绍+安装+实战操作(HDFS+YARN+MapReduce)

    1)下载Apache Pig

    下载地址:https://pig.apache.org/releases.html
    下载最新版本:

    $ mkdir -p /opt/bigdata/hadoop/software/pig ; cd /opt/bigdata/hadoop/software/pig
    $ wget https://dlcdn.apache.org/pig/pig-0.17.0/pig-0.17.0.tar.gz
    $ tar -xf pig-0.17.0.tar.gz -C /opt/bigdata/hadoop/server/
    $ cd /opt/bigdata/hadoop/server/pig-0.17.0
    

    2)配置环境变量

    $ vi /etc/profile
    export PIG_HOME=/opt/bigdata/hadoop/server/pig-0.17.0
    export PATH=$PATH:${PIG_HOME}/bin
    export PIG_CONF_DIR=${PIG_HOME}/conf
    # 将 PIG_CLASSPATH 环境变量设置为hadoop集群配置目录的位置(包含 core-site.xml、hdfs-site.xml 和 mapred-site.xml 文件的目录):
    export PIG_CLASSPATH=$HADOOP_HOME/etc/hadoop
    # 如果您使用的是 Tez,您还需要将 Tez 配置目录(包含 tez-site.xml 的目录),我这里不加:
    # export PIG_CLASSPATH=$HADOOP_HOME/etc/hadoop:/tez/conf
    # 如果您使用的是 Spark,您还需要指定 SPARK_HOME 并指定 SPARK_JAR,这是您上传 $SPARK_HOME/lib/spark-assembly*.jar 的 hdfs 位置:
    # export SPARK_HOME=/mysparkhome/; export SPARK_JAR=hdfs://example.com:8020/spark-assembly*.jar
    

    加载生效

    $ source /etc/profile
    $ pig -version
    

    3)修改配置

    $ cd $PIG_CONF_DIR
    # 在Pig的 conf 文件夹中,我们有一个名为 pig.properties 的文件。在pig.properties文件中,可以设置如下所示的各种参数。
    # 查看配置帮助
    $ pig -h properties
    

    四、Apache Pig执行模式

    1)本地模式

    要在本地模式下运行 Pig,您需要访问单台机器;所有文件都使用本地主机和文件系统安装和运行。使用 -x 标志(pig -x local)指定本地模式。

    2)Tez 本地模式

    在 Tez 本地模式下运行 Pig。它类似于本地模式,除了内部 Pig 将调用 Tez 运行时引擎。使用 -x 标志 (pig -x tez_local) 指定 Tez 本地模式。

    【温馨提示】 Tez是基于Hadoop YARN之上的DAG(有向⽆环图,Directed Acyclic Graph)计算引擎。核⼼思想是将Map和Reduce两个操作进⼀步拆分,即Map被拆分成Input、Processor、Sort、Merge和Output, Reduce被拆分成Input、Shuffle、Sort、Merge、Processor和Output
    等。

    3)Spark 本地模式

    在 spark 本地模式下运行 Pig。它类似于本地模式,除了内部 Pig 将调用 spark 运行时引擎。使用 -x 标志 (pig -x spark_local) 指定 Spark 本地模式。


    上面三个模式是本地模式,下面三个模式是基于Hadoop集群,Hadoop环境部署可以参考我之前的文章:大数据Hadoop原理介绍+安装+实战操作(HDFS+YARN+MapReduce)

    4)MapReduce模式(默认模式)

    要在 MapReduce 模式下运行 Pig,依赖Hadoop集群。Mapreduce 模式是默认模式;您可以但不需要使用 -x 标志(pig 或者 pig -x mapreduce)指定它。

    5)Tez 模式

    要在 Tez 模式下运行 Pig,依赖Hadoop集群。使用 -x 标志 (-x tez) 指定 Tez 模式。

    6)Spark 模式

    要在 Spark 模式下运行 Pig,依赖Hadoop集群。使用 -x 标志 (-x spark) 指定 Spark 模式。在 Spark 执行模式下,需要将 env::SPARK_MASTER 设置为适当的值(local - local 模式,yarn-client - yarn-client 模式,mesos://host:port - mesos 上的 spark 或 spark://host :port - spark 集群。在 Spark 上运行的 Pig 脚本可以利用动态分配功能。只需启用spark.dynamicAllocation.enabled即可启用该功能。关于spark,可以参考我之前的文章:大数据Hadoop之——计算引擎Spark

    五、Apache Pig执行机制

    1)交互模式 (Grunt shell)

    您可以使用Grunt shell以交互模式运行Apache Pig。在这个shell中,您可以输入Pig Latin语句并获取输出(使用Dump操作符)。

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

    【local模式示例】示例一

    # 执行完的文件会被删掉,也就是/tmp/passwd执行完会删除
    $ cp /etc/passwd /tmp/passwd
    $ pig -x local
    # 分隔字符串
    A = load 'passwd' using PigStorage(':'); 
    # 遍历
    B = foreach A generate $0,$2,$6 as id; 
    # 输出屏幕
    dump B;
    # 输出到本地文件
    store B into 'passwd.out';
    


    【local模式示例】示例二
    先准备好数据

    $ vi data.txt
    001,stu01,18,55
    002,stu02,20,50
    003,stu03,25,60
    

    运行pig

    $ pig -x local
    # Tuple(元祖)数据格式
    student = LOAD './data.txt' USING PigStorage(',') as (id:int,name:chararray,age:int,height:int);
    

    【MapReduce模式示例】

    $ pig
    ls
    fs -ls
    fs -touchz test001
    fs -mkdir test
    help
    

    2)批处理模式 (脚本)

    您可以在批处理模式下运行Apache Pig,方法是将Pig Latin脚本以 .pig 扩展名写入单个文件。

    pig脚本

    $ cp /etc/passwd /tmp/passwd
    $ vi test01.pig
    ### pig脚本注释有以下两种:
    # 对于多行注释,请使用 /* ...。*/
    # 对于单行注释,使用 --
    A = load '/tmp/passwd' using PigStorage(':');  -- load the passwd file
    B = foreach A generate $0,$2,$6 as p;  -- extract the user IDs
    store B into 'test01.out';  -- write the results to a file name id.out
    

    执行

    # 以各种模式执行
    $ pig -x local test01.pig
    $ pig -x tez_local test01.pig
    $ pig -x spark_local test01.pig
    $ pig -x spark test01.pig
    $ pig -x tez test01.pig
    # MapReduce模式,这里演示一下这个模式,这个模式会提交yarn mr任务
    # 先启动hadoop和historyserver服务
    $ start-all.sh
    $ mr-jobhistory-daemon.sh start historyserver
    # 使用这个模式,文件就是HDFS上的
    $ hadoop fs -put /tmp/passwd /tmp/
    $ pig test01.pig
    $ pig -x mapreduce test01.pig
    

    yarn任务

    HDFS查看输出结果

    3)嵌入式模式(UDF)

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

    六、Pig Latin基础知识

    Pig Latin是用于使用Apache Pig在Hadoop中分析数据的语言。

    1)数据模型

    Pig Latin的数据模型是完全嵌套的,它允许复杂的非原子数据类型,例如 maptuple 。下面给出了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,raja@gmail.com,} }

    • Map(映射)

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

    • Relation(关系)

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

    2)数据类型

    空值

    • 所有上述数据类型的值都可以为NULL。Apache Pig以与SQL相似的方式处理空值。
    • null可以是未知值或不存在的值。它用作可选值的占位符。这些空值可以自然发生,也可以是操作的结果。

    3)算术操作符

    下表描述了Pig Latin的算术运算符。假设a = 10和b = 20。

    4)比较运算符

    5)类型结构运算符

    6)关系操作

    关于更多Pig Latin基础知识,可参考官方文档:https://pig.apache.org/docs/r0.17.0/start.html

    七、简单使用

    1)从文件系统(HDFS / Local)将数据加载到Apache Pig中

    语法:

    如果是MapReduce模式需要提前准备好文件传到HDFS上,上面已经演示过了,这里就不重复了,很简单

    relation_name = LOAD 'Input file path' USING function as schema;

    • relation_name——存储数据的关系变量。

    • 输入文件路径(Input file path)——我们必须提到存储文件的HDFS目录。 (在MapReduce模式下)

    • 函数(function )——我们必须从Apache Pig提供的一组加载函数( BinStorage,JsonLoader,PigStorage,TextLoader )中选择一个函数。

    • 模式(schema)——我们必须定义数据的模式。 我们可以按照以下方式定义所需的模式,例如:(column1 : data type, column2 : data type, column3 : data type);

    【温馨提示】我们加载数据而不指定模式。 在这种情况下,这些列将被作为$0、$2、$2依次类推,代表第几列数据。

    2)Pig存储数据

    语法:

    STORE relation_name INTO ' required_directory_path ' [USING function];

    【示例】

    $ pig
    # 不指定绝对路径就存储当前用户目录下
    STORE student INTO 'hdfs://hadoop-node1:8082/pig_Output/ ' USING PigStorage (',');
    

    3)将结果显示打印在屏幕

    $ vi data.txt
    001,stu01,18,55
    002,stu02,20,50
    003,stu03,25,60
    $ hadoop fs -put data.txt /tmp/
    $ student = LOAD '/tmp/data.txt' USING PigStorage(',') as (id:int,name:chararray,age:int,height:int);
    # dump不区分大小写
    Dump student 
    dump student
    

    4)描述操作符

    Describe Relation_name
    Describe student
    

    5)解释运算符

    explain Relation_name;
    explain student;
    

    6)图解运算符(表结构)

    illustrate Relation_name;
    illustrate student;
    

    以下操作是类sql操作,如果小伙伴对传统关系型数据了解的话,很容易理解。


    7)分组操作(GROUP)

    group_data = GROUP Relation_name BY age;
    group_data = GROUP student BY age;
    dump group_data;
    

    8)协同组操作

    协同组 操作符的工作或多或少以同样的方式为group运算。两个操作符之间的唯一区别是, 组 操作符通常与一个关系一起使用,而 cogroup 操作符用于涉及两个或更多个关系的语句中。

    student_details.txt

    001,Rajiv,Reddy,21,9848022337,Hyderabad
    002,siddarth,Battacharya,22,9848022338,Kolkata
    003,Rajesh,Khanna,22,9848022339,Delhi
    004,Preethi,Agarwal,21,9848022330,Pune
    005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar
    006,Archana,Mishra,23,9848022335,Chennai
    007,Komal,Nayak,24,9848022334,trivendram
    008,Bharathi,Nambiayar,24,9848022333,Chennai
    

    employee_details.txt

    001,Robin,22,newyork
    002,BOB,23,Kolkata
    003,Maya,23,Tokyo
    004,Sara,25,London
    005,David,23,Bhuwaneshwar
    006,Maggy,22,Chennai
    

    将文件上传到HDFS

    $ hadoop fs -put student_details.txt employee_details.txt /tmp/
    

    pig执行

    $ pig
    student_details = LOAD '/tmp/student_details.txt' USING PigStorage(',')
       as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray);
    
    employee_details = LOAD '/tmp/employee_details.txt' USING PigStorage(',')
       as (id:int, name:chararray, age:int, city:chararray);
    
    cogroup_data = COGROUP student_details by age, employee_details by age;
    
    Dump cogroup_data;
    

    9)JOIN连接操作

    在 JOIN 操作符是用来记录从两个或两个以上的关系结合起来。在执行连接操作时,我们声明每个关系中的一个(或一组)元组作为关键字。当这些键匹配时,两个特定的元组匹配,否则记录被丢弃。联接可以是以下类型(跟传统数据库很相似,但是本质是不一样的):

    • 自连接
    • 内部联接
    • 外连接 - 左连接,右连接和完全连接

    准备好数据
    customers.txt

    1,Ramesh,32,Ahmedabad,2000.00
    2,Khilan,25,Delhi,1500.00
    3,kaushik,23,Kota,2000.00
    4,Chaitali,25,Mumbai,6500.00
    5,Hardik,27,Bhopal,8500.00
    6,Komal,22,MP,4500.00
    7,Muffy,24,Indore,10000.00
    

    orders.txt

    102,2009-10-08 00:00:00,3,3000
    100,2009-10-08 00:00:00,3,1500
    101,2009-11-20 00:00:00,2,1560
    103,2008-05-20 00:00:00,4,2060
    

    1、自连接

    自连接 用于将表与自身连接在一起,就像该表是两个关系一样,临时重命名至少一个关系。

    $ pig -x local
    customers1 = LOAD './customers.txt' USING PigStorage(',')
       as (id:int, name:chararray, age:int, address:chararray, salary:int);
    
    customers2 = LOAD './customers.txt' USING PigStorage(',')
       as (id:int, name:chararray, age:int, address:chararray, salary:int);
    
    result = JOIN customers1 BY id, customers2 BY id;
    dump result ;
    

    发现就是把两个表连接在一起显示了。

    2、内部联接

    它通过基于连接谓词组合两个关系(称为A和B)的列值来创建新的关系。查询将A的每一行与B的每一行进行比较,以查找满足连接谓词的所有行对。当满足连接谓词时,A和B的每对匹配行的列值合并到一个结果行中,去掉重复列。如果A和B没有匹配的行就会丢弃。

    # 使用本地模式
    $ pig -x local
    customers = LOAD './customers.txt' USING PigStorage(',')
       as (id:int, name:chararray, age:int, address:chararray, salary:int);
    
    orders = LOAD './orders.txt' USING PigStorage(',')
       as (oid:int, date:chararray, customer_id:int, amount:int);
    
    # 通过customer id进行关联
    result = JOIN customers BY id, orders BY customer_id;
    dump result;
    

    3、左外连接

    在 LEFT OUTER JOIN 操作左表返回所有的行,即使是在正确的关系不匹配,如果不匹配右表列会被置空。

    # 使用本地模式
    $ pig -x local
    customers = LOAD './customers.txt' USING PigStorage(',')
       as (id:int, name:chararray, age:int, address:chararray, salary:int);
    
    orders = LOAD './orders.txt' USING PigStorage(',')
       as (oid:int, date:chararray, customer_id:int, amount:int);
    
    # 通过customer id进行关联
    result = JOIN customers BY id LEFT OUTER, orders BY customer_id;
    dump result;
    

    4、右外连接

    跟左外连接正好相反,把关键词换成RIGHT执行就行。很简单,这里就不演示了。

    5、全外联接

    当一个关系匹配时, 完整的外部联接 操作返回行。不匹配的都保留,缺失的补空。

    result = JOIN customers BY id FULL OUTER, orders BY customer_id;
    dump result
    

    6、多个键分组操作

    employee.txt

    001,Rajiv,Reddy,21,programmer,003
    002,siddarth,Battacharya,22,programmer,003
    003,Rajesh,Khanna,22,programmer,003
    004,Preethi,Agarwal,21,programmer,003
    005,Trupthi,Mohanthy,23,programmer,003
    006,Archana,Mishra,23,programmer,003
    007,Komal,Nayak,24,teamlead,002
    008,Bharathi,Nambiayar,24,manager,001
    

    employee_contact.txt

    001,9848022337,Rajiv@gmail.com,Hyderabad,003
    002,9848022338,siddarth@gmail.com,Kolkata,003
    003,9848022339,Rajesh@gmail.com,Delhi,003
    004,9848022330,Preethi@gmail.com,Pune,003
    005,9848022336,Trupthi@gmail.com,Bhuwaneshwar,003
    006,9848022335,Archana@gmail.com,Chennai,003
    007,9848022334,Komal@gmail.com,trivendram,002
    008,9848022333,Bharathi@gmail.com,Chennai,001
    

    pig执行

    $ pig -x local
    
    employee = LOAD './employee.txt' USING PigStorage(',')
       as (id:int, firstname:chararray, lastname:chararray, age:int, designation:chararray, jobid:int);
    
    employee_contact = LOAD './employee_contact.txt' USING PigStorage(',')
       as (id:int, phone:chararray, email:chararray, city:chararray, jobid:int);
    
    result = JOIN employee BY (id,jobid), employee_contact BY (id,jobid);
    

    10)交叉操作

    该 CROSS 操作符计算两个或两个以上关系的跨产品,其实也是求笛卡尔积。

    先准备好数据

    customers.txt

    1,Ramesh,32,Ahmedabad,2000.00
    2,Khilan,25,Delhi,1500.00
    3,kaushik,23,Kota,2000.00
    4,Chaitali,25,Mumbai,6500.00
    5,Hardik,27,Bhopal,8500.00
    6,Komal,22,MP,4500.00
    7,Muffy,24,Indore,10000.00
    

    orders.txt

    102,2009-10-08 00:00:00,3,3000
    100,2009-10-08 00:00:00,3,1500
    101,2009-11-20 00:00:00,2,1560
    103,2008-05-20 00:00:00,4,2060
    

    pig执行

    # 使用local模式,但是最好使用MapReduce模式,但是这里为了方便演示,就选择local模式了
    $ pig -x local
    
    customers = LOAD './customers.txt' USING PigStorage(',')
       as (id:int, name:chararray, age:int, address:chararray, salary:int);
    
    orders = LOAD './orders.txt' USING PigStorage(',')
       as (oid:int, date:chararray, customer_id:int, amount:int);
    
    result = CROSS customers, orders;
    

    11)联合操作

    Pig Latin 的 UNION 操作符用于合并两个关系的内容。要对两个关系执行UNION操作,其列和域必须相同。

    先准备好数据

    student_data1.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
    

    student_data2.txt

    7,Komal,Nayak,9848022334,trivendram
    8,Bharathi,Nambiayar,9848022333,Chennai
    

    pig执行

    # 使用local模式,但是最好使用MapReduce模式,但是这里为了方便演示,就选择local模式了
    $ pig -x local
    
    student1 = LOAD './student_data1.txt' USING PigStorage(',')
       as (id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray);
    
    student2 = LOAD './student_data2.txt' USING PigStorage(',') as (id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray);
    
    result = UNION student1, student2;
    dump restult
    

    其实像JOIN操作、分组操作,交叉操作、联合操作等跟传统数据库操作是一样的,但是本质是不一样,但是可以用传统的数据库的思想去理解语句,如有不清楚的小伙伴,可以给我留言。


    12)split

    该 SPLIT 运算符用于关系分成两个或更多的关系。

    以示例驱动理解,先准备好数据

    student_details.txt

    001,Rajiv,Reddy,21,9848022337,Hyderabad
    002,siddarth,Battacharya,22,9848022338,Kolkata
    003,Rajesh,Khanna,22,9848022339,Delhi
    004,Preethi,Agarwal,21,9848022330,Pune
    005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar
    006,Archana,Mishra,23,9848022335,Chennai
    007,Komal,Nayak,24,9848022334,trivendram
    008,Bharathi,Nambiayar,24,9848022333,Chennai
    

    pig执行

    $ pig -x local
    
    student_details = LOAD './student_details.txt' USING PigStorage(',')
       as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray);
    
    # 现在让我们将关系分为两部分,一部分列出年龄小于23岁的员工,另一部分列出年龄在22至25岁之间的员工。
    SPLIT student_details into student_details1 if age<23, student_details2 if (22<age and age>25);
    
    # 打印输出
    Dump student_details1;
    Dump student_details2;
    

    13)条件过滤操作

    连接【12)split】的操作

    filter_data = FILTER student_details BY city == 'Chennai';
    Dump filter_data;
    

    14)去重操作

    该 DISTINCT 运算符用于从关系去除冗余(一式两份)的元组。

    连接【12)split】的操作

    distinct_data = DISTINCT student_details;
    Dump distinct_data;
    

    15)Foreach遍历操作

    连接【12)split】的操作

    foreach_data = FOREACH student_details GENERATE id,age,city;
    Dump foreach_data;
    

    16)Order排序操作

    连接【12)split】的操作

    order_by_data = ORDER student_details BY age DESC;
    Dump order_by_data;
    

    17)Limit限制操作

    连接【12)split】的操作

    limit_data = LIMIT student_details 4;
    Dump limit_data;
    

    18)函数操作

    Apache Pig提供了各种内置函数,即 eval,load,store,math,string,bag 和 tuple等 函数。

    1、评估函数

    2、加载和存储函数

    3、Bag和Tuple函数

    4、字符串函数

    5、日期时间函数

    6、数学函数

    7、用户定义函数

    除了内置的功能,Apache的pig提供了广泛的支持 User Defined Functions(UDF的)。 使用这些UDF,我们可以定义我们自己的功能并使用它们。UDF支持以六种编程语言提供,即Java,Jython,Python,JavaScript,Ruby和Groovy。

    在使用Java编写UDF时,我们可以创建和使用以下三种类型的函数:

    • 过滤器功能 - 过滤器功能用作过滤器语句中的条件。 这些函数接受一个Pig值作为输入并返回一个布尔值。

    • 评估函数 - Eval函数用于FOREACH-GENERATE语句。 这些函数接受Pig值作为输入并返回Pig结果。

    • 代数函数 - 代数函数在FOREACHGENERATE语句中作用于内袋。 这些功能用于在内袋上执行完整的MapReduce操作。

    使用Java编写UDF

    1、在pom.xml文件添加配置依赖

    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.pig</groupId>
        <artifactId>pig</artifactId>
        <version>0.17.0</version>
    </dependency>
    

    代码如下:

    /**
     * 自定义行数
     * 在编写UDF时,必须继承EvalFunc类并为exec() 函数提供实现。在这个函数中,写入UDF所需的代码。
     *
     */
    
    import java.io.IOException;
    import org.apache.pig.EvalFunc;
    import org.apache.pig.data.Tuple;
    
    import java.io.IOException;
    import org.apache.pig.EvalFunc;
    import org.apache.pig.data.Tuple;
    
    public class UDFTest001 extends EvalFunc<String>{
        public String exec(Tuple input) throws IOException {
            if (input == null || input.size() == 0)
                return null;
            String str = (String)input.get(0);
            return str.toUpperCase();
        }
    }
    

    在没有错误编译类之后,右键单击UDFTest001.java文件。它给你一个菜单。IDEA导出jar包





    使用UDF,在编写UDF并生成Jar文件之后,请按照以下步骤进行操作:

    第1步:注册Jar文件

    $ pig -x local
    REGISTER ./pig.jar
    

    第2步:定义别名

    DEFINE alias {function | [command [input] [output] [ship] [cache] [stderr] ] };

    【示例】

    DEFINE UDFTest001 UDFTest001()
    

    第3步:使用UDF
    准备数据

    udfdata.txt

    001,Robin,22,newyork
    002,BOB,23,Kolkata
    003,Maya,23,Tokyo
    004,Sara,25,London
    005,David,23,Bhuwaneshwar
    006,Maggy,22,Chennai
    007,Robert,22,newyork
    008,Syam,23,Kolkata
    009,Mary,25,Tokyo
    010,Saran,25,London
    011,Stacy,25,Bhuwaneshwar
    012,Kelly,22,Chennai
    

    pig执行

    $ pig -x local
    udfdata = LOAD './udfdata.txt' USING PigStorage(',') as (id:int, name:chararray, age:int, city:chararray);
    
    Upper_case = FOREACH udfdata GENERATE UDFTest001(name);
    dump Upper_case
    

    全部把名字转换成大写了

    19)执行脚本

    # 交互式执行脚本
    $ pig -x mapreduce
    exec xxx.pig
    
    # 非交互式执行脚本
    $ pig -x mapreduce xxx.pig
    

    八、Apache Pig与其它组件对比

    1)Apache Pig与MapReduce对比

    2)Apache Pig与SQL对比

    除了上述差异之外,Apache Pig Latin 还有以下几个优势:

    • 允许在管道中分割。
    • 允许开发人员在管道中的任何地方存储数据。
    • 声明执行计划。
    • 提供操作员执行ETL(提取,转换和加载)功能。

    3)Apache Pig与Hive对比

    Apache Pig和Hive都用于创建MapReduce作业。并且在某些情况下,Hive以类似Apache Pig的方式在HDFS上运行。在下表中,我们列出了一些将Apache Pig与Hive分开的重要观点。

    Apache Pig到这里就结束了,操作起来还是比较简单,有疑问的小伙伴,欢迎给我留言,小伙伴也可以参考官方文档。后续会有更多关于大数据文章,请耐心等待。

  • 相关阅读:
    Java Web项目开发中常见路径获取方法
    Genymotion模拟器连接不上开发服务器解决办法
    百度鹰眼轨迹管理台部署到本地或者服务器上
    解决Hibernate4执行update操作,不更新数据的问题
    后台发送http请求通用方法,包括get和post
    Java后端发出post请求带参数并接收返回的json
    $.ajax()方法详解
    常见异常及解决办法
    jQuery通过地址获取经纬度demo
    python 导入模块与使用
  • 原文地址:https://www.cnblogs.com/liugp/p/16296869.html
Copyright © 2020-2023  润新知