/
DayOfWeek.java
50 lines (43 loc) · 1.38 KB
/
DayOfWeek.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.livingsocial.hive.udf;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
@Description(name = "dayofweek",
value = "_FUNC_(date) - Returns the day of the week",
extended = "Examples:\n"
+ " > SELECT _FUNC_('2011-08-29') FROM src LIMIT 1;\n"
+ " 1\n")
public class DayOfWeek extends UDF {
private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private final Calendar calendar = Calendar.getInstance();
private IntWritable result = new IntWritable();
public DayOfWeek() {
calendar.setFirstDayOfWeek(Calendar.MONDAY);
}
/**
* Get the day of week from a date string.
*
* @param dateString
* the dateString in the format of "yyyy-MM-dd"
* @return an int from 1 to 7. null if the dateString is not a valid date
* string.
*/
public IntWritable evaluate(Text dateString) {
if (dateString == null) {
return null;
}
try {
Date date = formatter.parse(dateString.toString());
calendar.setTime(date);
result.set(calendar.get(Calendar.DAY_OF_WEEK));
return result;
} catch (ParseException e) {
return null;
}
}
}