-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHiveJDBCUtils.java
More file actions
410 lines (352 loc) · 10.9 KB
/
HiveJDBCUtils.java
File metadata and controls
410 lines (352 loc) · 10.9 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*-------------------------------------------------------------------------
*
* foreign-data wrapper for HIVE
*
* IDENTIFICATION
* hive_fdw/HiveJDBCUtils.java
*
*-------------------------------------------------------------------------
*/
import java.sql.*;
import java.text.*;
import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.util.*;
public class HiveJDBCUtils
{
private ResultSet result_set;
private ResultSet result_set1;
private Connection conn;
private int NumberOfColumns;
private int NumberOfRows;
private Statement sql;
private String[] Iterate;
private static HiveJDBCLoader Hive_Driver_Loader;
private StringWriter exception_stack_trace_string_writer;
private PrintWriter exception_stack_trace_print_writer;
private ArrayList < String > mylist;
/*
* ConnInitialize
* Initiates the connection to the foreign database after setting
* up initial configuration and executes the query.
*/
public String
ConnInitialize(String[] options_array) throws IOException
{
DatabaseMetaData db_metadata;
ResultSetMetaData result_set_metadata;
Properties HiveProperties;
Class HiveDriverClass = null;
Driver HiveDriver = null;
String DriverClassName = options_array[0];
String url = options_array[1];
String userName = options_array[2];
String password = options_array[3];
exception_stack_trace_string_writer = new StringWriter();
exception_stack_trace_print_writer = new PrintWriter(exception_stack_trace_string_writer);
NumberOfColumns = 0;
conn = null;
try
{
File JarFile = new File(options_array[4]);
String jarfile_path = JarFile.toURI().toURL().toString();
if (Hive_Driver_Loader == null)
{
/* If Hive_Driver_Loader is being created. */
Hive_Driver_Loader = new HiveJDBCLoader(new URL[]{JarFile.toURI().toURL()});
}
else if (Hive_Driver_Loader.CheckIfClassIsLoaded(DriverClassName) == null)
{
Hive_Driver_Loader.addPath(jarfile_path);
}
HiveDriverClass = Hive_Driver_Loader.loadClass(DriverClassName);
HiveDriver = (Driver)HiveDriverClass.newInstance();
HiveProperties = new Properties();
HiveProperties.put("user", userName);
HiveProperties.put("password", password);
conn = HiveDriver.connect(url, HiveProperties);
}
catch (Exception initialize_exception)
{
/* If an exception occurs,it is returned back to the
* calling C code by returning a Java String object
* that has the exception's stack trace.
* If all goes well,a null String is returned. */
initialize_exception.printStackTrace(exception_stack_trace_print_writer);
return (new String(exception_stack_trace_string_writer.toString()));
}
return null;
}
/*
* Initialize
* Initiates the connection to the foreign database after setting
* up initial configuration and executes the query.
*/
public String
Execute_Query(String query) throws IOException
{
DatabaseMetaData db_metadata;
ResultSetMetaData result_set_metadata;
Properties HiveProperties;
Class HiveDriverClass = null;
Driver HiveDriver = null;
exception_stack_trace_string_writer = new StringWriter();
exception_stack_trace_print_writer = new PrintWriter(exception_stack_trace_string_writer);
NumberOfColumns = 0;
try
{
db_metadata = conn.getMetaData();
sql = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
result_set = sql.executeQuery(query);
result_set_metadata = result_set.getMetaData();
NumberOfColumns = result_set_metadata.getColumnCount();
Iterate = new String[NumberOfColumns];
}
catch (Exception initialize_exception)
{
/* If an exception occurs,it is returned back to the
* calling C code by returning a Java String object
* that has the exception's stack trace.
* If all goes well,a null String is returned. */
initialize_exception.printStackTrace(exception_stack_trace_print_writer);
return (new String(exception_stack_trace_string_writer.toString()));
}
return null;
}
/*
* ReturnResultSet
* Returns the result set that is returned from the foreign database
* after execution of the query to C code.
*/
public String[]
ReturnResultSet()
{
int i = 0;
try
{
/* Row-by-row processing is done in hive_fdw.One row
* at a time is returned to the C code. */
if (result_set.next())
{
for (i = 0; i < NumberOfColumns; i++)
{
Iterate[i] = result_set.getString(i+1);
}
++NumberOfRows;
/* The current row in result_set is returned
* to the C code in a Java String array that
* has the value of the fields of the current
* row as it values. */
return (Iterate);
}
}
catch (Exception returnresultset_exception)
{
returnresultset_exception.printStackTrace();
}
/* All of result_set's rows have been returned to the C code. */
return null;
}
/*
* Close
* Releases the resources used.
*/
public String
Close()
{
try
{
result_set.close();
conn.close();
result_set = null;
conn = null;
Iterate = null;
}
catch (Exception close_exception)
{
/* If an exception occurs,it is returned back to the
* calling C code by returning a Java String object
* that has the exception's stack trace.
* If all goes well,a null String is returned. */
close_exception.printStackTrace(exception_stack_trace_print_writer);
return (new String(exception_stack_trace_string_writer.toString()));
}
return null;
}
/*
* Cancel
* Cancels the query and releases the resources in case query
* cancellation is requested by the user.
*/
public String
Cancel()
{
try
{
result_set.close();
conn.close();
}
catch(Exception cancel_exception)
{
/* If an exception occurs,it is returned back to the
* calling C code by returning a Java String object
* that has the exception's stack trace.
* If all goes well,a null String is returned. */
cancel_exception.printStackTrace(exception_stack_trace_print_writer);
return (new String(exception_stack_trace_string_writer.toString()));
}
return null;
}
/*
** Generates CREATE FOREIGN TABLE statements for each of the tables
** in the source schema and returns the list of these statements
** to the caller.
**/
public String
PrepareDDLStmtList(String schema, String servername) throws IOException
{
DatabaseMetaData db_metadata;
ResultSetMetaData result_set_metadata;
ResultSetMetaData result_set_metadata1;
Properties HiveProperties;
Class HiveDriverClass = null;
Driver HiveDriver = null;
String[] column_name;
String[] column_type;
int[] column_size;
int[] column_length;
int total_col = 0;
int col = 0;
int i = 0;
exception_stack_trace_string_writer = new StringWriter();
exception_stack_trace_print_writer = new PrintWriter(exception_stack_trace_string_writer);
NumberOfColumns = 0;
try
{
db_metadata = conn.getMetaData();
result_set = db_metadata.getTables(null, schema , "%", null);
result_set_metadata = result_set.getMetaData();
NumberOfColumns = result_set_metadata.getColumnCount();
Iterate = new String[100];
column_name = new String[200];
column_type = new String[200];
column_size = new int[200];
column_length = new int[200];
mylist = new ArrayList<String>();
try
{
while (result_set.next())
{
String table_name = result_set.getString("TABLE_NAME");
result_set1 = db_metadata.getColumns(null,
schema,
table_name ,
null);
result_set_metadata1 = result_set1.getMetaData();
NumberOfColumns = result_set_metadata1.getColumnCount();
while (result_set1.next())
{
column_name[total_col] = result_set1.getString("COLUMN_NAME");
column_type[total_col] = result_set1.getString("TYPE_NAME");
column_size[total_col] = result_set1.getInt("COLUMN_SIZE");
column_length[total_col] = result_set1.getInt("CHAR_OCTET_LENGTH");
String field = column_type[total_col];
if (field.compareTo( "TINYINT") == 0)
column_type[total_col] = "smallint";
else if (field.compareTo("INT") == 0)
column_type[total_col] = "int";
else if (field.compareTo("BIGINT") == 0)
column_type[total_col] = "bigint";
else if (field.compareTo("BOOLEAN") == 0)
column_type[total_col] = "boolean";
else if (field.compareTo("FLOAT") == 0)
column_type[total_col] = "float";
else if (field.compareTo("DOUBLE") == 0)
column_type[total_col] = "double precision";
else if (field.compareTo("STRING") == 0)
column_type[total_col] = "varchar";
else if (field.compareTo("BINARY") == 0)
column_type[total_col] = "bytea";
else if (field.compareTo("TIMESTAMP") == 0)
column_type[total_col] = "timestamp";
else if (field.compareTo("DECIMAL") == 0)
column_type[total_col] = "decimal";
else if (field.compareTo("DATE") == 0)
column_type[total_col] = "date";
else if (field.compareTo("CHAR") == 0)
column_type[total_col] = "char";
else if (field.compareTo("VARCHAR") == 0)
column_type[total_col] = "varchar";
else /* We need to error out for unsupported data types */
{
System.out.println("Unsupported Hive Data Type "+ field);
}
total_col++;
}
String stmt_str = "CREATE FOREIGN TABLE "+table_name+"(";
for(col = 0;col<total_col;col++)
{
if(column_type[col].compareTo("CHAR") == 0)
{
stmt_str = stmt_str+column_name[col]+" "+column_type[col]+"("+column_size[col]+")";
}
else if(column_type[col].compareTo("VARCHAR") == 0)
{
stmt_str = stmt_str+column_name[col]+" "+column_type[col]+"("+column_size[col]+")";
}
else
{
stmt_str = stmt_str+column_name[col]+" "+column_type[col];
}
if( col == total_col-1 )
{
stmt_str = stmt_str+")";
}
else
{
stmt_str = stmt_str+",";
}
}
stmt_str = stmt_str+" SERVER " + servername + " OPTIONS (table '"+table_name+"',schema '"+schema+"');";
mylist.add(stmt_str);
String s = mylist.get(i);
System.out.println("STATEMENT "+s);
i++;
total_col = 0;
}
int sz = mylist.size();
Iterate = new String[sz];
for (int j = 0; j < sz; j++)
{
Iterate[j] = mylist.get(j);
}
NumberOfRows = sz;
}
catch (Exception returnresultset_exception)
{
returnresultset_exception.printStackTrace();
}
}
catch (Exception initialize_exception)
{
/* If an exception occurs,it is returned back to the
* calling C code by returning a Java String object
* that has the exception's stack trace.
* If all goes well,a null String is returned. */
initialize_exception.printStackTrace(exception_stack_trace_print_writer);
return (new String(exception_stack_trace_string_writer.toString()));
}
return null;
}
/* Retruns the foreign table DDL statements string array
*
*/
public String[]
ReturnDDLStmtList()
{
int i=0;
return Iterate;
}
}