Unable to establish a JDBC connection to Hive from Eclipse

前端 未结 1 1169
南笙
南笙 2021-01-18 01:45

I am trying to establish a JDBC connection to Hive so that I can view and create tables and query Hive tables from Eclipse. I used HiveClient sample code: https://cwiki.apac

1条回答
  •  借酒劲吻你
    2021-01-18 02:18

    You have 2 options to connect hiveserver using jdbc

    Option 1 : Hiveserver2

    You are trying to connect hiveserver2, hiveserver version in cloudera manager is hivesever2, which is more secure than hiveserver. JDBC code you are using is hiveserver,Use the following code snippet for hiveserver2

    Class.forName("org.apache.hive.jdbc.HiveDriver");
    Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (key int, value string)");
    String sql = "show tables '" + tableName + "'";
    

    If you look at the connection string, can see the hiveserver version 2(jdbc:hive2://localhost:10000/default", "", ""), second and third arguments are username and password, by default keep it empty string "".

    For executing this program add hiveserver2 specific libraries.

    Instead of writing your own programs for checking hiveserver2 jdbc connection, beeline hive client can be used as follows

    > [testuser02@Abcd-Host1 ~]$ beeline
    
    > beeline> !connect jdbc:hive2://Abcd-Host1:10000/default "" "" ""
    > 
    > 0: jdbc:hive2://Abcd-Host1:10000/default> show tables;
    
    +------------+
    |  tab_name  |
    +------------+
    | sample_07  |
    | sample_08  |
    | test1      |
    +------------+
    3 rows selected (0.334 seconds)
    

    Options 2: Hiveserver1

    If you want to make use of your existing code(code for hiveserver1), which you are having https://cwiki.apache.org/confluence/display/Hive/HiveClient. You got to start a new hiveserver in your userspace in another port. Use the following command to start a hiveserver in a given port

    nohup hive --service hiveserver -p 10001 &

    Now change the port number to 10001 in jdbc connection and run it.

    0 讨论(0)
提交回复
热议问题