topic: Resolving Hive's question of getting today, yesterday, tomorrow's date
Note: Due to the hive built-in function, there is no date function available to get yesterday and tomorrow, so you need to write a custom function to implement
1. Get today's time (format can be customized)
Statement:
Select From_unixtime (Unix_timestamp (), ' Yyyy-mm-dd HH:mm:ss ')
return Result:
2016-09-14 16:21:59
2. Get Yesterday, tomorrow time (need to write UDF custom functions)
(1) For more information on the buzz of writing hive UDFs, see: http://blog.csdn.net/high2011/article/details/52425430
(2) The Java code that implements the UDF here is as follows:
Import Org.apache.hadoop.hive.ql.exec.UDF;
Import Org.apache.hadoop.io.Text;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.GregorianCalendar;
/** * Document: This type of role----> get tomorrow or yesterday time * USER:YANGJF * DATE:2016/9/14 16:26 * * public class NextDay extends UDF {
Public String Evaluate (int i) {date date=new date ();//take time calendar calendar = new GregorianCalendar ();
Calendar.settime (date); Calendar.add (Calendar. Date,i);//Increase the date by one day. Integer backwards, negative numbers move forward, 0 represents today's time date=calendar.gettime ();
This time is the result of the date pushing back a day simpledateformat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Formatter.format (date);
return datestring;
} public static void Main (string[] args) {System.out.println ("Today's Time:" +new nextday (). Evaluate (0));
System.out.println ("Tomorrow Time:" +new nextday (). Evaluate (1)); SYSTEM.OUT.PRINTLN ("Yesterday Days Time:" +new nextday (). EvaluatE (-1)); }}/** * Results: Today time: 2016-09-14 16:35:58 * Tomorrow time: 2016-09-15 16:35:58 * Yesterday time: 2016-09-13 16:35:58 */
(3) Registering temporary functions in hive: "Getmydate"
(4) Use the function:
Get today's time----"Select Getmydate (0);
2016-09-14 16:38:31
Get Tomorrow Time----"Select getmydate (1);
2016-09-15 16:38:31
Get yesterday time----"SELECT Getmydate (-1);
2016-09-13 16:38:31
The above tests have been passed, can be used directly, if in doubt, please leave a message, you are welcome to criticize.