- Apache Pig 教程
- Apache Pig - 首頁
- Apache Pig 簡介
- Apache Pig - 概述
- Apache Pig - 架構
- Apache Pig 環境
- Apache Pig - 安裝
- Apache Pig - 執行
- Apache Pig - Grunt Shell
- Pig Latin
- Pig Latin - 基礎
- 載入 & 儲存運算子
- Apache Pig - 讀取資料
- Apache Pig - 儲存資料
- 診斷運算子
- Apache Pig - 診斷運算子
- Apache Pig - Describe 運算子
- Apache Pig - Explain 運算子
- Apache Pig - Illustrate 運算子
- 分組 & 連線
- Apache Pig - Group 運算子
- Apache Pig - Cogroup 運算子
- Apache Pig - Join 運算子
- Apache Pig - Cross 運算子
- Pig Latin 內建函式
- Apache Pig - Eval 函式
- 載入 & 儲存函式
- Apache Pig - Bag & Tuple 函式
- Apache Pig - 字串函式
- Apache Pig - 日期時間函式
- Apache Pig - 數學函式
- Apache Pig 有用資源
- Apache Pig - 快速指南
- Apache Pig - 有用資源
- Apache Pig - 討論
Apache Pig - STRSPLITTOBAG()
此函式類似於 STRSPLIT() 函式。它根據給定的分隔符分割字串,並將結果返回到一個包中。
語法
下面給出 STRSPLITTOBAG() 的語法。此函式接受一個需要分割的字串、一個正則表示式以及一個指定限制的整數值(字串應分割成的子字串數量)。此函式解析字串,並在遇到給定的正則表示式時,將字串分割成 n 個子字串,其中 n 將是傳遞給 limit 的值。
grunt> STRSPLITTOBAG(string, regex, limit)
示例
假設在 HDFS 目錄 /pig_data/ 中有一個名為 emp.txt 的檔案,如下所示。此檔案包含員工詳細資訊,例如 id、姓名、年齡和城市。
emp.txt
001,Robin_Smith,22,newyork 002,BOB_Wilson,23,Kolkata 003,Maya_Reddy,23,Tokyo 004,Sara_Jain,25,London 005,David_Miller,23,Bhuwaneshwar 006,Maggy_Moore,22,Chennai 007,Robert_Scott,22,newyork 008,Syam_Ketavarapu,23,Kolkata 009,Mary_Carter,25,Tokyo 010,Saran_Naidu,25,London 011,Stacy_Green,25,Bhuwaneshwar 012,Kelly_Moore,22,Chennai
並且,我們已使用名為 emp_data 的關係將此檔案載入到 Pig 中,如下所示。
grunt> emp_data = LOAD 'hdfs://:9000/pig_data/emp.txt' USING PigStorage(',')
as (id:int, name:chararray, age:int, city:chararray);
以下是 STRSPLITTOBAG() 函式的示例。如果您觀察 emp.txt 檔案,您會發現,在 name 列中,我們使用分隔符 “_” 分隔了員工的姓名和姓氏。
在此示例中,我們嘗試分割員工的姓名和姓氏,並使用 STRSPLITTOBAG() 函式在包中獲取結果。
grunt> strsplittobag_data = FOREACH emp_data GENERATE (id,name), STRSPLITTOBAG (name,'_',2);
語句的結果將儲存在名為 strsplittobag_data 的關係中。使用 Dump 運算子驗證關係 strsplittobag_data 的內容,如下所示。
grunt> Dump strsplittobag_data;
((1,Robin_Smith),{(Robin),(Smith)})
((2,BOB_Wilson),{(BOB),(Wilson)})
((3,Maya_Reddy),{(Maya),(Reddy)})
((4,Sara_Jain),{(Sara),(Jain)})
((5,David_Miller),{(David),(Miller)})
((6,Maggy_Moore),{(Maggy),(Moore)})
((7,Robert_Scott),{(Robert),(Scott)})
((8,Syam_Ketavarapu),{(Syam),(Ketavarapu)})
((9,Mary_Carter),{(Mary),(Carter)})
((10,Saran_Naidu),{(Saran),(Naidu)})
((11,Stacy_Green),{(Stacy),(Green)})
((12,Kelly_Moore),{(Kelly),(Moore)})
apache_pig_string_functions.htm
廣告