猪 - 如何操纵和比较日期? [英] Pig - How to manipulate and compare dates?

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

问题描述

我有一个包含这样的条目的文件:

I have a file which contains entries like this:

1,1,07 2012,07 2013,11,blablabla

这两个第一字段标识。三是开始日期(月年)和第四个是结束日期。第五个字段是月数btweens这两个日期。而最后一个字段包含文本。

The two first fields are ids. The third is the begin date(month year) and the fourth is the end date. The fifth field is the number of months btweens these two dates. And the last field contains text.

下面是我的猪code加载此数据:

Here is my pig code to load this data:

f = LOAD 'file.txt' USING PigStorage(',') AS (id1:int, id2:int, date1:chararray, date2:chararray, duration:int, text:chararray);

我想过滤我的文件,这样我只保留项,其中DATE2从今天起是不到三年的时间。是否有可能在猪?

I would like to filter my file so that I keep only the entries where date2 is less than three years from today. Is it possible to that in Pig ?

感谢。

推荐答案

没有必要写一个自定义函数:

No need to write a custom function:

在猪0.11您可以在现场DATE2从chararray使用 TODATE转换为datetime数据类型()< /一>函数,然后得到根据它的currentTime的()和date2使用YearsBetween()和过滤器之间的差异。例如:

In Pig 0.11 you can convert the date2 field from chararray to datetime data type using the ToDate() function, and then get the difference between the CurrentTime() and date2 using YearsBetween() and filter according to it. for example:

G =过滤器F BY YearsBetween(CURRENTTIME(),TODATE(DATE2 +'01','年月日'))3;

g = FILTER f BY YearsBetween(CurrentTime(),ToDate(date2 + ' 01', 'yyyy MM dd'))<3

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

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