1
+ /**
2
+ * Created by surthi on 30/06/17.
3
+ */
4
+ import java .sql .SQLException ;
5
+ import java .sql .Connection ;
6
+ import java .sql .ResultSet ;
7
+ import java .sql .Statement ;
8
+ import java .sql .DriverManager ;
9
+
10
+
11
+ /**
12
+ * This is a test case that connects to sprak's thrift server and asserts that the data is present.
13
+ * In this example, 3 asserts are done: "show tables", "describe table <table-name>" and "select * from <table-name>".
14
+ */
15
+ public class TestThriftClient {
16
+ private static String driverName = "org.apache.hive.jdbc.HiveDriver" ;
17
+
18
+ public static void main (String [] args ) throws SQLException {
19
+
20
+ // Assert that the driver is present in classpath
21
+ try {
22
+ Class .forName (driverName );
23
+ } catch (ClassNotFoundException e ) {
24
+ e .printStackTrace ();
25
+ System .exit (1 );
26
+ }
27
+
28
+ // Change the connection-url, username and password accordingly
29
+ String connUrl = "jdbc:hive2://172.20.1.190:10000/default" ;
30
+ String username = "" ;
31
+ String password = "" ;
32
+
33
+ Connection con = DriverManager .getConnection (connUrl , username , password );
34
+ Statement stmt = con .createStatement ();
35
+
36
+ // show tables
37
+ String sql = "show tables" ;
38
+ System .out .println ("Running: " + sql );
39
+ ResultSet res = stmt .executeQuery (sql );
40
+ if (res .next ()) {
41
+ System .out .println (res .getString (1 ));
42
+ }
43
+
44
+ // describe table
45
+ String tableName = "hiveivnews" ;
46
+ sql = "describe " + tableName ;
47
+ System .out .println ("Running: " + sql );
48
+ res = stmt .executeQuery (sql );
49
+ while (res .next ()) {
50
+ System .out .println (res .getString (1 ) + "\t " + res .getString (2 ));
51
+ }
52
+
53
+ // select * query
54
+ sql = "select * from " + tableName ;
55
+ System .out .println ("Running: " + sql );
56
+ res = stmt .executeQuery (sql );
57
+ while (res .next ()) {
58
+ System .out .println (String .valueOf (res .getString (2 )) + "\t " + res .getString (7 ));
59
+ }
60
+ }
61
+ }
0 commit comments