如何在猪拉丁字母中按字母顺序排序字符串字符

时间:2017-12-06 22:27:02

标签: string sorting apache-pig

如果我有一张表格如下:name:chararray

示例:

acab
bca
das
desac

如何在Pig中按字母顺序命名列'名称'?

像这样:

aacb
abc
ads
acdes

1 个答案:

答案 0 :(得分:0)

编写自己的UDF。将名称传递给UDF。在UDF中将字符串转换为chararray并对其进行排序。返回已排序的字符串。

PIG

REGISTER ORDER_UDF.jar;
A = LOAD 'data.txt' USING PigStorage(',') AS (name: chararray);
B = FOREACH A GENERATE ORDER_UDF.ORDER(name);
DUMP B;

UDF

import java.io.IOException;
import java.util.Arrays;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;

   public class ORDER extends EvalFunc<String>
   {
     public String exec(Tuple input) throws IOException {
        if (input == null || input.size() == 0)
            return null;
        try{
            char tempArray[] = ((String)input).toCharArray();
            Arrays.sort(tempArray);       
            return new String(tempArray);
        }catch(Exception e){
            throw new IOException("Caught exception processing input row ", e);
        }
    }
  }