這個MIN()Pig Latin 的函數用於獲取 single-column 包中某個列的最小(最低)值(數字或字符數組)。在計算最小值時,MIN()函數忽略 NULL 值。
注意~’
為了得到全局最小值,我們需要執行一個Group All操作,並使用 MIN() 函數計算最小值。
要獲得組的最小值,我們需要使用Group By運算符並繼續執行最小函數。
用法
下麵給出的是MIN()函數。
grunt> MIN(expression)
示例
假設我們有一個名為student_details.txt在 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);
計算最低 GPA
我們可以使用 內置 函數MIN()(區分大小寫)從一組給定的數值計算最小值。讓我們將關係分組student_details使用Group All運算符,並將結果存儲在名為的關係中student_group_all如下所示
grunt> student_group_all = Group student_details All;
它將產生如下所示的關係。
grunt> Dump student_group_all; (all,{(8,Bharathi,Nambiayar,24,9848022333,Chennai,72), (7,Komal,Nayak,24,9848022 334,trivendram,83), (6,Archana,Mishra,23,9848022335,Chennai,87), (5,Trupthi,Mohan thy,23,9848022336,Bhuwaneshwar,75), (4,Preethi,Agarwal,21,9848022330,Pune,93), (3 ,Rajesh,Khanna,22,9848022339,Delhi,90), (2,siddarth,Battacharya,22,9848022338,Ko lkata,78), (1,Rajiv,Reddy,21,9848022337,Hyderabad,89)})
現在讓我們計算 GPA 的全局最小值,即所有學生的 GPA 值中的最小值,使用MIN()函數如下圖。
grunt> student_gpa_min = foreach student_group_all Generate
(student_details.firstname, student_details.gpa), MIN(student_details.gpa);
確認
驗證關係student_gpa_min使用DUMP操作符如下圖。
grunt> Dump student_gpa_min;
輸出
它將產生以下輸出,顯示關係的內容student_gpa_min。
(({(Bharathi),(Komal),(Archana),(Trupthi),(Preethi),(Rajesh),(siddarth),(Rajiv) } , { (72) , (83) , (87) , (75) , (93) , (90) , (78) , (89) }) ,72)
相關用法
- Apache Pig MAX()用法及代碼示例
- Apache Pig MilliSecondsBetween()用法及代碼示例
- Apache Pig MinutesBetween()用法及代碼示例
- Apache Pig MonthsBetween()用法及代碼示例
- Apache Pig HoursBetween()用法及代碼示例
- Apache Pig TOKENIZE()用法及代碼示例
- Apache Pig SQRT()用法及代碼示例
- Apache Pig TAN()用法及代碼示例
- Apache Pig TOMAP()用法及代碼示例
- Apache Pig TOTUPLE()用法及代碼示例
- Apache Pig EqualsIgnoreCase()用法及代碼示例
- Apache Pig GetHour()用法及代碼示例
- Apache Pig EXP()用法及代碼示例
- Apache Pig CurrentTime()用法及代碼示例
- Apache Pig UPPER()用法及代碼示例
- Apache Pig PluckTuple()用法及代碼示例
- Apache Pig UCFIRST()用法及代碼示例
- Apache Pig LAST_INDEX_OF()用法及代碼示例
- Apache Pig GetMonth()用法及代碼示例
- Apache Pig COUNT_STAR()用法及代碼示例
注:本文由純淨天空篩選整理自 Apache Pig - MIN()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。