Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom UserDefinedFunction in Hive
    text
    copied!<p><strong>Problem Statement</strong></p> <p>I created the below <code>UserDefinedFunction</code> to get the <code>yesterday's date</code> in the format I wanted as I will be passing the format into this below method from the query.</p> <pre><code>public final class YesterdayDate extends UDF { public String evaluate(final String format) { DateFormat dateFormat = new SimpleDateFormat(format); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return dateFormat.format(cal.getTime()).toString(); } } </code></pre> <p>So whenever I try to run the query like below by adding the jar to classpath and creating the temporary function <code>yesterdaydate</code>, I always get zero result back-</p> <pre><code>hive&gt; create temporary function yesterdaydate as 'com.example.hive.udf.YesterdayDate'; OK Time taken: 0.512 seconds </code></pre> <p>Below is the query I am running-</p> <pre><code>hive&gt; SELECT * FROM REALTIME where dt= yesterdaydate('yyyyMMdd') LIMIT 10; OK </code></pre> <p>And I always get zero result back but the data is there in that table for Aug 5th.</p> <p>What wrong I am doing? Any suggestion will be appreciated.</p> <p>Query should be like this if today's date is Aug 6th- then the query will be like below by using the above user defined function-</p> <pre><code>SELECT * FROM REALTIME where dt= '20120805' LIMIT 10; </code></pre> <p>NOTE:- As I am working with <code>Hive 0.6</code> so it doesn’t support variable substitution thing, so I cannot use <code>hiveconf</code> here and the above table has been partitioned on <code>dt(date) column</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload