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

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

问题描述

有没有可能在浮点数或双数的小数点后切割某个区域?例如:结果是 2.67894 => 我想要 2.6 作为结果(而不是四舍五入时的 2.7).

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

推荐答案

为此编写一个 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)

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

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