- 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 - STARTSWITH() 函式
此函式接受兩個字串引數。它驗證第一個字串是否以第二個字串開頭。
語法
以下是STARTSWITH()函式的語法。
grunt> STARTSWITH(string, substring)
示例
假設在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);
示例
以下是STARTSWITH()函式的示例。在此示例中,我們已驗證所有員工的姓名是否以子字串“Ro”開頭。
grunt> startswith_data = FOREACH emp_data GENERATE (id,name), STARTSWITH (name,’Ro’);
上述語句解析所有員工的姓名,如果這些姓名中的任何一個以子字串‘Ro’開頭。由於員工‘Robin’和‘Robert’的姓名以子字串‘Ro’開頭,因此對於這兩個元組,STARTSWITH()函式返回布林值‘true’,對於其餘元組,其值為‘false’。
語句的結果將儲存在名為startswith_data的關係中。使用 Dump 運算子驗證關係startswith_data的內容,如下所示。
grunt> Dump startswith_data; ((1,Robin),true) ((2,BOB),false) ((3,Maya),false) ((4,Sara),false) ((5,David),false) ((6,maggy),false) ((7,Robert),true) ((8,Syam),false) ((9,Mary),false) ((10,Saran),false) ((11,Stacy),false) ((12,Kelly),false)
apache_pig_string_functions.htm
廣告