Apache-pig-pig-latin-basics

提供:Dev Guides
移動先:案内検索

豚のラテン語-基本

Pig Latinは、Apache Pigを使用してHadoopのデータを分析するために使用される言語です。 この章では、Pig Latinステートメント、データ型、一般演算子と関係演算子、Pig Latin UDFなど、Pig Latinの基本について説明します。

豚ラテン–データモデル

前の章で説明したように、Pigのデータモデルは完全にネストされています。 Relation は、Pig Latinデータモデルの最も外側の構造です。 そして、それは*バッグ*です-

  • バッグはタプルのコレクションです。
  • タプルは、順序付けられたフィールドのセットです。
  • フィールドはデータの一部です。

豚のラテン語-ステートメッツ

Pig Latinを使用してデータを処理する際、*ステートメント*は基本的な構成要素です。

  • これらのステートメントは relations で機能します。 *式*と*スキーマ*が含まれます。
  • すべてのステートメントはセミコロン(;)で終わります。
  • ステートメントを通じて、Pig Latinが提供する演算子を使用してさまざまな操作を実行します。
  • LOADとSTOREを除いて、他のすべての操作を実行している間、Pig Latinステートメントはリレーションを入力として受け取り、別のリレーションを出力として生成します。
  • Gruntシェルで Load ステートメントを入力するとすぐに、そのセマンティックチェックが実行されます。 スキーマの内容を表示するには、 Dump 演算子を使用する必要があります。 dump 操作を実行した後にのみ、データをファイルシステムにロードするMapReduceジョブが実行されます。

以下に、Apache PigにデータをロードするPig Latinステートメントを示します。

grunt> Student_data = LOAD 'student_data.txt' USING PigStorage(',')as
   ( id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray );

豚のラテン語-データ型

下の表は、Pig Latinデータ型を示しています。

S.N.

データ・タイプ

説明と例

1

int

符号付き32ビット整数を表します。

:8

2

long

符号付き64ビット整数を表します。

:5L

3

浮く

符号付き32ビット浮動小数点を表します。

:5.5F

4

ダブル

64ビットの浮動小数点を表します。

:10.5

5

文字配列

Unicode UTF-8形式の文字配列(文字列)を表します。

:「チュートリアルポイント」

6

バイト配列

バイト配列(blob)を表します。

7

ブール値

ブール値を表します。

:true/false。

8

日付時刻

日時を表します。

:1970-01-01T00:00:00.000 + 00:00

9

Biginteger

Java BigIntegerを表します。

:60708090709

10

十進法

Java BigDecimalを表します

:185.98376256272893883

複合型

11

タプル

タプルは、順序付けられたフィールドのセットです。

:(raja、30)

12

Bag

バッグはタプルのコレクションです。

:\ {(raju、30)、(Mohhammad、45)}

13

Map

マップは、キーと値のペアのセットです。

:[「name」#「Raju」、「age」#30]

ヌル値

上記のすべてのデータ型の値はNULLにできます。 Apache Pigは、SQLと同様の方法でnull値を処理します。

nullは、不明な値または存在しない値です。 オプションの値のプレースホルダーとして使用されます。 これらのヌルは自然に発生する場合と、操作の結果である場合があります。

豚のラテン語-算術演算子

次の表に、Pig Latinの算術演算子を示します。 a = 10およびb = 20と仮定します。

Operator Description Example
PLUS Addition − Adds values on either side of the operator a PLUS b will give 30
Subtraction − Subtracts right hand operand from left hand operand a − b will give −10
* *Multiplication *− Multiplies values on either side of the operator a* b will give 200
/ Division − Divides left hand operand by right hand operand b/a will give 2
% *Modulus *− Divides left hand operand by right hand operand and returns remainder b % a will give 0
? :
  • Bincond* − Evaluates the Boolean operators. It has three operands as shown below.

変数 x =(式)? value1 if truevalue2 if false

a

b =(a == 1)? 20:30;

a = 1の場合、bの値は20です。

a!= 1の場合、bの値は30です。

a

場合

WHEN

THEN

他の終わり

Case − The case operator is equivalent to nested bincond operator.

CASE f2 % 2

0で「偶数」の場合

1 THEN 'ODD’の場合

END

豚ラテン–比較演算子

次の表に、Pig Latinの比較演算子を示します。

Operator Description Example
== Equal − Checks if the values of two operands are equal or not; if yes, then the condition becomes true. (a = b) is not true
!= Not Equal − Checks if the values of two operands are equal or not. If the values are not equal, then condition becomes true. (a != b) is true.
> Greater than − Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true. (a > b) is not true.
< Less than − Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true. (a < b) is true.
>= Greater than or equal to − Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true. (a >= b) is not true.
Less than or equal to − Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true. (a ⇐ b) is true.
matches Pattern matching − Checks whether the string in the left-hand side matches with the constant in the right-hand side. f1 matches '.tutorial.'

豚のラテン語–型構築演算子

次の表に、Pig LatinのType構成演算子を示します。

Operator Description Example
() Tuple constructor operator − This operator is used to construct a tuple. (Raju, 30)
\{} Bag constructor operator − This operator is used to construct a bag. \{(Raju, 30), (Mohammad, 45)}
[] Map constructor operator − This operator is used to construct a tuple. [name#Raja, age#30]

豚のラテン語-リレーショナルオペレーション

次の表に、Pig Latinの関係演算子を示します。

オペレーター

説明

読み込みと保存

LOAD

データをファイルシステム(ローカル/HDFS)からリレーションにロードするには。

格納

ファイルシステム(ローカル/HDFS)への関係を保存します。

フィルタリング

フィルタ

リレーションから不要な行を削除します。

DISTINCT

リレーションから重複行を削除します。

FOREACH、GENERATE

データの列に基づいてデータ変換を生成します。

ストリーム

外部プログラムを使用して関係を変換します。

グループ化と参加

JOIN

2つ以上の関係を結合します。

コグループ

データを2つ以上の関係にグループ化する。

グループ

データを単一の関係にグループ化する。

クロス

2つ以上の関係の外積を作成します。

ソート

注文

1つまたは複数のフィールド(昇順または降順)に基づいてソートされた順序でリレーションを配置します。

限定

リレーションから限られた数のタプルを取得します。

結合と分割

連合

2つ以上の関係を単一の関係に結合すること。

スプリット

単一のリレーションを2つ以上のリレーションに分割します。

診断オペレーター

DUMP

コンソールでリレーションの内容を印刷します。

DESCRIBE

関係のスキーマを記述するため。

説明する

論理、物理、またはMapReduce実行プランを表示して、関係を計算します。

イラスト

一連のステートメントのステップごとの実行を表示します。