比较猪的日期时间 [英] comparing datetime in pig

查看:20
本文介绍了比较猪的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 pig 11 中,是否支持比较日期时间类型?例如:date1:datetime

in pig 11, is there a support for comparing datetime types? for example: date1:datetime

并且过滤器具有条件:date1 >= ToDate('1999-01-01')

and filter has condition: date1 >= ToDate('1999-01-01')

这个比较是否返回正确的结果?

does this comparison returns correct result?

推荐答案

日期比较可以看作是数值比较.例如:

Date comparison can be considered as a numerical comparison. E.g:

cat date1.txt
1999-01-01
2011-03-19
2011-02-24
2011-02-25
2011-05-23
1978-12-13

A = load 'date1.txt' as (in:chararray);
B = foreach A generate ToDate(in, 'yyyy-MM-dd') as (dt:datetime);
--filter dates that are equal or greater than 2011-02-25: 
C = filter B by DaysBetween(dt, 
      (datetime)ToDate('2011-02-25', 'yyyy-MM-dd')) >=(long)0;

dump C;
(2011-03-19T00:00:00.000+01:00)
(2011-02-25T00:00:00.000+01:00)
(2011-05-23T00:00:00.000+02:00)

传递给 ToDate 的自定义格式模式遵循 Java 的 SimpleDateFormat 约定.
注意大写和小写字母,例如 D 表示 一年中的一天,而 d 表示一个月中的一天 .这可能会导致从字符数组到日期时间的日期转换不当.

The custom format pattern passed to ToDate follows the Java's SimpleDateFormat convention.
Watch out for the uppercase and lowercase letters, for example D means Day in year but d refers to Day in month . This can lead to an inappropriate date conversion from chararray to datetime.

或者,如果您的字符数组日期是 ISO 格式,您可以使用 Piggybank 的 UDF 也是如此.

Alternatively, if your chararray dates are in ISO format, you may use the Piggybank's UDFs as well.

这篇关于比较猪的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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