在Apache Pig中,可以使用ORDER BY關鍵字對數據進行排序,使用PARTITION BY關鍵字對數據進行分區。以下是示例代碼:
-- Load data from input file
data = LOAD 'input.txt' USING PigStorage(',') AS (id:int, name:chararray, age:int);
-- Sort data by age in ascending order
sorted_data = ORDER data BY age;
-- Store sorted data into output file
STORE sorted_data INTO 'output_sorted' USING PigStorage(',');
-- Load data from input file
data = LOAD 'input.txt' USING PigStorage(',') AS (id:int, name:chararray, age:int);
-- Partition data by age
partitioned_data = FOREACH (GROUP data BY age) GENERATE group AS age, data;
-- Store partitioned data into output file
STORE partitioned_data INTO 'output_partitioned' USING PigStorage(',');
在上述示例中,首先使用LOAD命令加載數據,然后使用ORDER BY對數據進行排序,使用PARTITION BY對數據進行分區,最后使用STORE命令將排序后的數據和分區后的數據存儲到輸出文件中。