- 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 - SUBSTRING() 函式
此函式從給定字串中返回一個子字串。
語法
以下是 SUBSTRING() 函式的語法。此函式接受三個引數,一個是我們要查詢的字串的列名。另外兩個是所需子字串的起始和結束索引。
grunt> SUBSTRING(string, startIndex, stopIndex)
示例
假設在 HDFS 目錄 /pig_data/ 中有一個名為 emp.txt 的檔案,如下所示。此檔案包含員工詳細資訊,例如 id、姓名、年齡和城市。
emp.txt
001,Robin,22,newyork 002,Stacy,25,Bhuwaneshwar 003,Kelly,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);
以下是 SUBSTRING() 函式的示例。此示例獲取從第 0 個字母開始到第 2 個字母結束的員工姓名子字串。
grunt> substring_data = FOREACH emp_data GENERATE (id,name), SUBSTRING (name, 0, 2);
以上語句從員工姓名中提取所需的子字串。語句的結果將儲存在名為 substring_data 的關係中。
使用 Dump 運算子驗證關係 substring_data 的內容,如下所示。
grunt> Dump substring_data; ((1,Robin),Rob) ((2,Stacy),Sta) ((3,Kelly),Kel)
apache_pig_string_functions.htm
廣告