可读的字符串日期使用Pig转换为日期? [英] Human readable String date converted to date using Pig?

查看:75
本文介绍了可读的字符串日期使用Pig转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following human readable date formats stored in a text file:

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()将日期范围约束和逻辑应用于我的查询?

How can I convert the dates using so they are compatible with Pig's ToDate() function where I can then use GetHour(), GetYear(), GetDay() and GetMonth() to apply date range constraints and logic to my queries?

推荐答案

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

1.Pig support only few formats of date, so you need to convert your date and time according to any one of the below format.
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

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

2.Your input has BST as timezone but in pig BST is not supported, so you need to choose a different timezone which is equivalent to BST.
Timezones are available here http://joda-time.sourceforge.net/timezones.html

示例:

  1. 我选择的时间格式为"EEE,d MMM yyyy HH:mm:ss Z" Wed,4 Jul 2001 12:08:56,bcoz这与您的输入数据有些匹配.
  2. BST时区不可用,因此我选择了"GMT"作为时区,您可以根据需要进行更改.

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)

这篇关于可读的字符串日期使用Pig转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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