Apache-pig-size

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

Apache Pig-SIZE()

Pig Latinの* SIZE()*関数は、任意のPigデータ型に基づいて要素の数を計算するために使用されます。

構文

以下に、* SIZE()*関数の構文を示します。

grunt> SIZE(expression)

戻り値は、Apache Pigのデータ型によって異なります。

Data type Value
int, long, float, double For all these types, the size function returns 1.
Char array For a char array, the size() function returns the number of characters in the array.
Byte array For a bytearray, the size() function returns the number of bytes in the array.
Tuple For a tuple, the size() function returns number of fields in the tuple.
Bag For a bag, the size() function returns number of tuples in the bag.
Map For a map, the size() function returns the number of key/value pairs in the map.

以下に示すように、HDFSディレクトリ /pig_data/employee.txt という名前のファイルがあると仮定します。

*employee.txt*
1,John,2007-01-24,250
2,Ram,2007-05-27,220
3,Jack,2007-05-06,170
3,Jack,2007-04-06,100
4,Jill,2007-04-06,220
5,Zara,2007-06-06,300
5,Zara,2007-02-06,350

そして、以下に示すように、このファイルを関係名 employee_data でPigにロードしました。

grunt> employee_data = LOAD 'hdfs://localhost:9000/pig_data/employee.txt' USING PigStorage(',')
   as (id:int, name:chararray, workdate:chararray, daily_typing_pages:int);

タイプのサイズの計算

特定の列の型のサイズを計算するには、* SIZE()*関数を使用できます。 以下に示すように、名前タイプのサイズを計算しましょう。

grunt> size = FOREACH employee_data GENERATE SIZE(name);

検証

以下に示すように、 DUMP 演算子を使用してリレーション size を確認します。

grunt> Dump size;

出力

次の出力が生成され、リレーション size の内容が次のように表示されます。 この例では、 name 列のサイズを計算しました。 varchar型であるため、* SIZE()*関数は各従業員の名前の文字数を示します。

(4)
(3)
(4)
(4)
(4)
(4)
(4)