1
1
package com .esri .hadoop .shims ;
2
2
3
+ import java .lang .reflect .Method ;
4
+ import java .util .TimeZone ;
5
+
6
+
3
7
public class HiveShims {
4
8
5
9
/**
6
10
* This class is supplied for compatibility between Hive versions.
7
- * At 10.0 the serde constants were moved to another package. Also,
8
- * at 11.0 the previous class will be re-added for backwards
9
- * compatibility, but deprecated
11
+ * At 0.10 the serde constants were moved to another package. Also,
12
+ * at 0.11 the previous class will be re-added for backwards
13
+ * compatibility, but deprecated.
10
14
*
11
15
*/
12
16
public static class serdeConstants {
13
17
public static final String LIST_COLUMNS ;
14
18
public static final String LIST_COLUMN_TYPES ;
15
-
19
+
16
20
static {
17
21
Class <?> clazz = null ;
18
22
19
23
try {
20
- // Hive 10 and above constants
24
+ // Hive 0. 10 and above constants
21
25
clazz = Class .forName ("org.apache.hadoop.hive.serde.serdeConstants" );
22
26
} catch (ClassNotFoundException e ) {
23
27
try {
24
- // Hive 9 and below constants
28
+ // Hive 0. 9 and below constants
25
29
clazz = Class .forName ("org.apache.hadoop.hive.serde.Constants" );
26
30
} catch (ClassNotFoundException e1 ) {
27
31
// not much we can do here
@@ -31,7 +35,7 @@ public static class serdeConstants {
31
35
LIST_COLUMNS = getAsStringOrNull (clazz , "LIST_COLUMNS" );
32
36
LIST_COLUMN_TYPES = getAsStringOrNull (clazz , "LIST_COLUMN_TYPES" );
33
37
}
34
-
38
+
35
39
static String getAsStringOrNull (Class <?> clazz , String constant ) {
36
40
try {
37
41
return (String ) clazz .getField (constant ).get (null );
@@ -40,4 +44,136 @@ static String getAsStringOrNull(Class<?> clazz, String constant) {
40
44
}
41
45
}
42
46
}
47
+
48
+ /**
49
+ * Classes o.a.h.h.common.type Date & Timestamp were introduced in Hive-3.1 version.
50
+ */
51
+ public static Long getPrimitiveEpoch (Object prim , TimeZone tz ) {
52
+ if (prim instanceof java .sql .Timestamp ) {
53
+ return ((java .sql .Timestamp )prim ).getTime ();
54
+ } else if (prim instanceof java .util .Date ) {
55
+ return ((java .util .Date )prim ).getTime ();
56
+ } else {
57
+ try {
58
+ Class <?> dtClazz = Class .forName ("org.apache.hadoop.hive.common.type.Date" );
59
+ if (prim .getClass () == dtClazz ) {
60
+ Method dtGetImpl = dtClazz .getMethod ("toEpochMilli" );
61
+ return (java .lang .Long )(dtGetImpl .invoke (prim ));
62
+ } else {
63
+ Class <?> ttClazz = Class .forName ("org.apache.hadoop.hive.common.type.Timestamp" );
64
+ if (prim .getClass () == ttClazz ) {
65
+ Method ttGetImpl = ttClazz .getMethod ("toEpochMilli" );
66
+ return (java .lang .Long )(ttGetImpl .invoke (prim ));
67
+ } else {
68
+ return null ;
69
+ }
70
+ }
71
+ } catch (Exception exc ) {
72
+ return null ;
73
+ }
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Type DATE was introduced in Hive-0.12 - class DateWritable in API.
79
+ * Class DateWritableV2 is used instead as of Hive-3.1 version.
80
+ */
81
+ public static void setDateWritable (Object dwHive , long epoch
82
+ , TimeZone tz
83
+ ) {
84
+ try { // Hive 3.1 and above
85
+ Class <?> dtClazz = Class .forName ("org.apache.hadoop.hive.common.type.Date" );
86
+ Class <?> dwClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.DateWritableV2" );
87
+ Method dtSetImpl = dtClazz .getMethod ("setTimeInMillis" , long .class );
88
+ Method dwSetImpl = dwClazz .getMethod ("set" , dtClazz );
89
+ Object dtObj = dtClazz .getConstructor ().newInstance ();
90
+ dtSetImpl .invoke (dtObj , epoch );
91
+ dwSetImpl .invoke (dwHive , dtObj );
92
+ } catch (Exception e1 ) {
93
+ try { // Hive 0.12 and above
94
+ Class <?> dwClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.DateWritable" );
95
+ Method dwSetImpl = dwClazz .getMethod ("set" , java .sql .Date .class );
96
+ dwSetImpl .invoke (dwHive , new java .sql .Date (epoch ));
97
+ } catch (Exception e2 ) { // Hive 0.11 and below
98
+ // column type DATE not supported
99
+ throw new UnsupportedOperationException ("DATE type" );
100
+ }
101
+ }
102
+ } // setDateWritable
103
+
104
+ /**
105
+ * Type DATE was introduced in Hive-0.12 - class DateWritable in API.
106
+ * Class DateWritableV2 is used instead as of Hive-3.1 version.
107
+ */
108
+ public static void setDateWritable (Object dwHive , java .sql .Date jsd ) {
109
+ try { // Hive 3.1 and above
110
+ Class <?> dtClazz = Class .forName ("org.apache.hadoop.hive.common.type.Date" );
111
+ Class <?> dwClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.DateWritableV2" );
112
+ Method dtSetImpl = dtClazz .getMethod ("setTimeInMillis" , long .class );
113
+ Method dwSetImpl = dwClazz .getMethod ("set" , dtClazz );
114
+ Object dtObj = dtClazz .getConstructor ().newInstance ();
115
+ dtSetImpl .invoke (dtObj , jsd .getTime ());
116
+ dwSetImpl .invoke (dwHive , dtObj );
117
+ } catch (Exception e1 ) {
118
+ try { // Hive 0.12 and above
119
+ Class <?> dwClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.DateWritable" );
120
+ Method dwSetImpl = dwClazz .getMethod ("set" , java .sql .Date .class );
121
+ dwSetImpl .invoke (dwHive , jsd );
122
+ } catch (Exception e2 ) { // Hive 0.11 and below
123
+ // column type DATE not supported
124
+ throw new UnsupportedOperationException ("DATE type" );
125
+ }
126
+ }
127
+ } // setDateWritable
128
+
129
+ /**
130
+ * Type TIMESTAMP was introduced in Hive-0.12 - class TimestampWritable in API.
131
+ * Class TimestampWritableV2 is used instead as of Hive-3.1 version.
132
+ */
133
+ public static void setTimeWritable (Object twHive , long epoch ) {
134
+ try { // Hive 3.1 and above
135
+ Class <?> ttClazz = Class .forName ("org.apache.hadoop.hive.common.type.Timestamp" );
136
+ Class <?> twClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.TimestampWritableV2" );
137
+ Method ttSetImpl = ttClazz .getMethod ("setTimeInMillis" , long .class );
138
+ Method twSetImpl = twClazz .getMethod ("set" , ttClazz );
139
+ Object ttObj = ttClazz .getConstructor ().newInstance ();
140
+ ttSetImpl .invoke (ttObj , epoch );
141
+ twSetImpl .invoke (twHive , ttObj );
142
+ } catch (Exception e1 ) {
143
+ try { // Hive 0.12 and above
144
+ Class <?> twClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.TimestampWritable" );
145
+ Method twSetImpl = twClazz .getMethod ("set" , java .sql .Timestamp .class );
146
+ twSetImpl .invoke (twHive , new java .sql .Timestamp (epoch ));
147
+ } catch (Exception e2 ) { // Hive 0.11 and below
148
+ // column type TIMESTAMP not supported
149
+ throw new UnsupportedOperationException ("TIMESTAMP type" );
150
+ }
151
+ }
152
+ } // setTimeWritable
153
+
154
+ /**
155
+ * Type TIMESTAMP was introduced in Hive-0.12 - class TimestampWritable in API.
156
+ * Class TimestampWritableV2 is used instead as of Hive-3.1 version.
157
+ */
158
+ public static void setTimeWritable (Object twHive , java .sql .Timestamp jst ) {
159
+ long epoch = jst .getTime ();
160
+ try { // Hive 3.1 and above
161
+ Class <?> ttClazz = Class .forName ("org.apache.hadoop.hive.common.type.Timestamp" );
162
+ Class <?> twClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.TimestampWritableV2" );
163
+ Method ttSetImpl = ttClazz .getMethod ("setTimeInMillis" , long .class );
164
+ Method twSetImpl = twClazz .getMethod ("set" , ttClazz );
165
+ Object ttObj = ttClazz .getConstructor ().newInstance ();
166
+ ttSetImpl .invoke (ttObj , epoch );
167
+ twSetImpl .invoke (twHive , ttObj );
168
+ } catch (Exception e1 ) {
169
+ try { // Hive 0.12 and above
170
+ Class <?> twClazz = Class .forName ("org.apache.hadoop.hive.serde2.io.TimestampWritable" );
171
+ Method twSetImpl = twClazz .getMethod ("set" , java .sql .Timestamp .class );
172
+ twSetImpl .invoke (twHive , new java .sql .Timestamp (epoch ));
173
+ } catch (Exception e2 ) { // Hive 0.11 and below
174
+ // column type TIMESTAMP not supported
175
+ throw new UnsupportedOperationException ("TIMESTAMP type" );
176
+ }
177
+ }
178
+ } // setTimeWritable
43
179
}
0 commit comments