Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Community Articles

Find and share helpful community-sourced technical articles.
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!
Labels (1)
avatar
package com.big.data.hiveserver2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.IOException;
import java.sql.*;

public class HiveServer2Client {
    private static final Logger LOGGER = LoggerFactory.getLogger(HiveServer2Client.class);
    private static String HIVESERVE2DRIVER = "org.apache.hive.jdbc.HiveDriver";


    public static void main(String[] args) throws SQLException, IOException {

        // set the configuration
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        // /etc/security/keytab/hive.service.keytab is from local machine, This is the user which is executing the command
        UserGroupInformation.loginUserFromKeytab("hive/cluster20182.field.fieldorg.com@REALM_NAME", "/etc/security/keytab/hive.service.keytab");


        // load the driver
        try {
            Class.forName(HIVESERVE2DRIVER);
        } catch (ClassNotFoundException e) {
            LOGGER.error("Driver not found");
        }

        Connection con = DriverManager.getConnection("jdbc:hive2://cluster20181.field.fieldorg.com:2181,cluster20180.field.fieldorg.com:2181,cluster20182.field.fieldorg.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2;principal=hive/cluster20182.field.fieldorg.com@REALM_NAME");
        Statement stmt = con.createStatement();

        // Table Name
        String tableName = "testHiveDriverTable";
        stmt.execute("drop table " + tableName);

        LOGGER.info("Table {} is dropped", tableName);
        stmt.execute("create table " + tableName + " (key int, value string)");

        // show tables
        String sql = "show tables '" + tableName + "'";
        LOGGER.info("Running {} ", sql);

        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
            LOGGER.info(" return from HiveServer {}", res.getString(1));
        }
        // describe table
        sql = "describe " + tableName;
        LOGGER.info("DESCRIBE newwly created table sql command :  {}" + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.printf("HOOOO");
            LOGGER.info("Return from HiveServer {}", res.getString(1) + "\t" + res.getString(2));
        }

        // close the connection
        con.close();
    }

}



Dependency:
<dependencies>
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>1.2.1000.2.6.0.3-8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
    </dependency>
</dependencies>



Repo : http://repo.hortonworks.com/content/groups/public


<br>
13,007 Views
0 Kudos
Version history
Last update:
‎01-01-2018 06:25 PM
Updated by:
Contributors