Hive gets today, yesterday, tomorrow's date

Source: Internet
Author: User

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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.