Apache PIG - 如何在小数点后削减数字 [英] Apache PIG - How to cut digits after decimal point

查看:168
本文介绍了Apache PIG - 如何在小数点后削减数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在浮点数或双精度数的小数点后删除某个区域?
例如:结果将是2.67894 =>我希望结果为2.6(四舍五入不是2.7)。

解决方案

为此写一个UDF(用户定义的函数)。
$ b $ p

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

  @outputSchema('value:double')
def格式(数据):
return round(data,1)

(当然,您可以参数化UDF以使用不同的精度。)



注册并在猪代码中使用它。例子:

 注册numformat.py使用jython as numformat; 
$ b A = LOAD'so / testdata.csv'使用PigStorage(',')AS(data:double);
B = FOREACH A GENERATE numformat.format(data);
DUMP B;

以下输入:

  2.1234 
12.334

转储结果为:

 (2.1)
(12.3)


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).

解决方案

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)

这篇关于Apache PIG - 如何在小数点后削减数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆