人类可读字符串日期使用Pig转换为日期?

时间:2014-11-14 15:56:07

标签: apache-pig

我将以下人类可读日期格式存储在文本文件中:

Wed Oct 15 09:26:09 BST 2014
Wed Oct 15 19:26:09 BST 2014
Wed Oct 18 08:26:09 BST 2014
Wed Oct 23 10:26:09 BST 2014
Sun Oct 05 09:26:09 BST 2014
Wed Nov 20 19:26:09 BST 2014

如何转换日期,使它们与Pig的ToDate()函数兼容,然后我可以使用GetHour(),GetYear(),GetDay()和GetMonth()将日期范围约束和逻辑应用于我的查询?

1 个答案:

答案 0 :(得分:5)

1.Pig仅支持几种日期格式,因此您需要根据以下任何一种格式转换日期和时间。
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Time Format

2.您的输入具有BST作为时区但不支持猪BST,因此您需要选择与BST相同的不同时区。
时区可在此处http://joda-time.sourceforge.net/timezones.html

获取

示例:

  1. 我选择时间格式为" EEE,d MMM yyyy HH:mm:ss Z" 2001年7月4日星期三12:08:56",bcoz这与您的输入数据有些匹配。
  2. BST时区不可用,所以我选择了' GMT'作为时区,您可以根据需要进行更改。
  3. <强> input.txt中

    Wed Oct 15 09:26:09 BST 2014
    Wed Oct 15 19:26:09 BST 2014
    Wed Oct 18 08:26:09 BST 2014
    Wed Oct 23 10:26:09 BST 2014
    Sun Oct 05 09:26:09 BST 2014
    Wed Nov 20 19:26:09 BST 2014
    

    <强> PigScript:

    A = LOAD 'input.txt' USING PigStorage(' ') AS(day:chararray,month:chararray,date:chararray,time:chararray,tzone:chararray,year:chararray);
    B = FOREACH A GENERATE CONCAT(CONCAT(CONCAT(CONCAT(day,', ',date),' ',month),' ',year),' ',time) AS mytime;
    C = FOREACH B GENERATE ToDate(mytime,'EEE, d MMM yyyy HH:mm:ss','GMT') AS newTime;
    D = FOREACH C GENERATE GetMonth(newTime),GetDay(newTime),GetYear(newTime),GetHour(newTime),GetMinute(newTime);
    DUMP D;
    

    <强>输出:

    (10,15,2014,9,26)
    (10,15,2014,19,26)
    (10,15,2014,8,26)
    (10,22,2014,10,26)
    (10,5,2014,9,26)
    (11,19,2014,19,26)
    
相关问题