Apache PIG - How to cut digits after decimal point

一世执手 提交于 2020-01-25 16:23:11

问题


Is there any possibility to cut a certain area after the decimal point of a float or double number? For example: the result would be 2.67894 => I want to have 2.6 as result (and not 2.7 when rounded).


回答1:


try it.. val is your values like 2.666,3.666,4.666666,5.3456334.....

b = foreach a GENERATE (FLOOR(val * 10) / 10);

dump b;



回答2:


Write a UDF (User Defined Function) for this.

A very simple python UDF (numformat.py):

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

(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;

For the following input:

2.1234
12.334

The dumped result is:

(2.1)
(12.3)


来源:https://stackoverflow.com/questions/29922857/apache-pig-how-to-cut-digits-after-decimal-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!