且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apache PIG - 如何削减小数点后的数字

更新时间:2023-02-21 15:55:44

为此编写一个 UDF(用户定义函数).

Write a UDF (User Defined Function) for this.

一个非常简单的python UDF (numformat.py):

A very simple python UDF (numformat.py):

@outputSchema('value:double')
def format(data):
    return round(data,1)

(当然,您可以对 UDF 进行参数化以使用不同的精度.)

(Of course you can parametrized the UDF to use different precision.)

比注册并在您的猪代码中使用它.示例:

Than register and use it in your pig code. Example:

REGISTER numformat.py USING jython as numformat;

A = LOAD 'so/testdata.csv' USING PigStorage(',') AS (data:double);
B = FOREACH A GENERATE numformat.format(data);
DUMP B;

对于以下输入:

2.1234
12.334

转储结果为:

(2.1)
(12.3)