- 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 - INDEXOF() 函式
INDEXOF() 函式接受一個字串值、一個字元和一個索引(整數)。它返回字串中給定字元的第一次出現,從給定索引開始向前搜尋。
語法
以下是 INDEXOF() 函式的語法。
grunt> INDEXOF(string, 'character', startIndex)
示例
假設在 HDFS 目錄 /pig_data/ 中有一個名為 emp.txt 的檔案,如下所示。此檔案包含員工詳細資訊,例如 id、姓名、年齡和城市。
emp.txt
001,Robin,22,newyork 002,BOB,23,Kolkata 003,Maya,23,Tokyo 004,Sara,25,London 005,David,23,Bhuwaneshwar 006,Maggy,22,Chennai 007,Robert,22,newyork 008,Syam,23,Kolkata 009,Mary,25,Tokyo 010,Saran,25,London 011,Stacy,25,Bhuwaneshwar 012,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);
以下是 INDEXOF() 函式的一個示例。在此示例中,我們使用此函式查詢每個員工姓名中字母 'r' 的出現次數。
grunt> indexof_data = FOREACH emp_data GENERATE (id,name), INDEXOF(name, 'r',0);
上述語句解析每個員工的姓名,並返回字母 ‘r’ 首次出現的索引值。如果姓名中不包含字母 ‘r’,則返回 -1 值
語句的結果將儲存在名為 indexof_data 的關係中。使用 Dump 運算子驗證關係 indexof_data 的內容,如下所示。
grunt> Dump indexof_data; ((1,Robin),-1) ((2,BOB),-1) ((3,Maya),-1) ((4,Sara),2) ((5,David),-1) ((6,Maggy),-1) ((7,Robert),4) ((8,Syam),-1) ((9,Mary),2) ((10,Saran),2) ((11,Stacy),-1) ((12,Kelly),-1)
apache_pig_string_functions.htm
廣告