- 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 運算子
- Pig Latin 內建函式
- Apache Pig - 評估函式
- 載入與儲存函式
- Apache Pig - Bag 和 Tuple 函式
- Apache Pig - 字串函式
- Apache Pig - 日期時間函式
- Apache Pig - 數學函式
- Apache Pig 有用資源
- Apache Pig - 快速指南
- Apache Pig - 有用資源
- Apache Pig - 討論
Apache Pig - STRSPLIT() 函式
此函式用於使用給定分隔符分割給定字串。
語法
STRSPLIT() 的語法如下所示。此函式接受需要分割的字串、正則表示式和指定限制的整數值(字串應分割成的子字串數量)。此函式解析字串,當遇到給定的正則表示式時,它將字串分割成 n 個子字串,其中 n 將是傳遞給 limit 的值。
grunt> STRSPLIT(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);
以下是 STRSPLIT() 函式的示例。如果您觀察 emp.txt 檔案,您會發現,在 name 列中,我們使用分隔符 '_' 分隔了員工的姓名和姓氏。
在此示例中,我們嘗試使用 STRSPLIT() 函式分割員工的姓名和姓氏。
grunt> strsplit_data = FOREACH emp_data GENERATE (id,name), STRSPLIT (name,'_',2);
語句的結果將儲存在名為 strsplit_data 的關係中。使用 Dump 運算子驗證關係 strsplit_data 的內容,如下所示。
grunt> Dump strsplit_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
廣告