Pig CONCAT 函数
-
CONCAT 函数
Pig-Latin CONCAT() 函数用于连接两个或多个相同类型的表达式。句法下面给出的是CONCAT() 函数的语法。grunt> CONCAT (expression, expression, [...expression])
例子假设我们在HDFS目录/pig_data/中有一个名为student_details.txt的文件,如下所示。001,Rajiv,Reddy,21,9848022337,Hyderabad,89 002,siddarth,Battacharya,22,9848022338,Kolkata,78 003,Rajesh,Khanna,22,9848022339,Delhi,90 004,Preethi,Agarwal,21,9848022330,Pune,93 005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar,75 006,Archana,Mishra,23,9848022335,Chennai,87 007,Komal,Nayak,24,9848022334,trivendram,83 008,Bharathi,Nambiayar,24,9848022333,Chennai,72
并且我们已经将该文件加载到Pig中,关系名称为student_details,如下所示。grunt> student_details = LOAD 'hdfs://localhost:9000/pig_data/student_details.txt' USING PigStorage(',') as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray, gpa:int);
-
连接两个字符串
我们可以使用CONCAT() 函数来连接两个或多个表达式。首先,如下所示,使用Dump运算符验证student_details关系的内容。grunt> Dump student_details; ( 1,Rajiv,Reddy,21,9848022337,Hyderabad,89 ) ( 2,siddarth,Battacharya,22,9848022338,Kolkata,78 ) ( 3,Rajesh,Khanna,22,9848022339,Delhi,90 ) ( 4,Preethi,Agarwal,21,9848022330,Pune,93 ) ( 5,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar,75 ) ( 6,Archana,Mishra,23,9848022335,Chennai,87 ) ( 7,Komal,Nayak,24,9848022334,trivendram,83 ) ( 8,Bharathi,Nambiayar,24,9848022333,Chennai,72 )
然后,使用describe运算符验证结构,如下所示。grunt> Describe student_details; student_details: {id: int, firstname: chararray, lastname: chararray, age: int, phone: chararray, city: chararray, gpa: int}
在上面的模式中,您可以观察到学生的姓名使用两个字符数组值来表示,即firstname和lastname。让我们使用CONCAT() 函数来概括这两个值。grunt> student_name_concat = foreach student_details Generate CONCAT (firstname, lastname);
-
验证
grunt> Dump student_name_concat;grunt> Dump dob_string;
输出它将产生以下输出,显示关系student_name_concat的内容。(RajivReddy) (siddarthBattacharya) (RajeshKhanna) (PreethiAgarwal) (TrupthiMohanthy) (ArchanaMishra) (KomalNayak) (BharathiNambiayar)
我们还可以在两个表达式之间使用可选的定界符,如下所示。grunt> CONCAT(firstname, '_',lastname);
现在,让我们在student_details关系中将学生记录的名字和姓氏连接在一起,方法是在它们之间放置“_”,如下所示。grunt> student_name_concat = foreach student_details GENERATE CONCAT(firstname, '_',lastname);
验证如下所示,使用DUMP运算符验证关系Student_name_concat。grunt> Dump student_name_concat;
输出它将产生以下输出,显示如下student_name_concat关系的内容。(Rajiv_Reddy) (siddarth_Battacharya) (Rajesh_Khanna) (Preethi_Agarwal) (Trupthi_Mohanthy) (Archana_Mishra) (Komal_Nayak) (Bharathi_Nambiayar)