JDBC Connection Hive

Source: Internet
Author: User

hive is the base component for data warehousing applications in big Data technology clusters, and is a benchmark for other similar data warehouse applications. The underlying data manipulation can be handled in a scripted manner with hive-client. If you need to develop an application, you need to connect using the JDBC driver for hive.

Code to connect hive requires first starting hive's Metastore and Hiveserver2

Hive--service metastore &hive--service Hiveserver2 &

Where Hive-site.xml is configured as:

&LT;CONFIGURATION&GT;&LT;PROPERTY&GT;&LT;NAME&GT;JAVAX.JDO.OPTION.CONNECTIONURL&LT;/NAME&GT;&LT;VALUE&GT;JDBC: mysql://192.168.174.131:3306/hive?createdatabaseifnotexist=true</value><description>jdbc  Connect string for a jdbc metastore</description></property><property ><name>javax.jdo.option.connectiondrivername</name><value>com.mysql.jdbc.driver</value ><description>driver class name for a jdbc metastore</description ></property><property><name>javax.jdo.option.ConnectionUserName</name><value> root</value><description>username to use against metastore database</ Description></property><property><name>javax.jdo.option.connectionpassword</name> <value>123456</value><description>password to use against metastore  database</description></property><property>  <name>hive.metastore.uris</name>   <value>thrift://192.168.174.131:9083</value>  </property><property><name >hive.support.sql11.reserved.keywords</name><value>false</value></property></ Configuration>


Code to connect to hive requires two dependencies to be added:

<!--S: Connect hive--><!--Https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc-<dependency > <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <!--   Note: The version here is compatible with the version of the jar package in the lib of Hive-<version>1.2.1</version></dependency><dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version >2.6.4</version></dependency><!--E: Connect to Hive--

Code Demo:

package com.fwmagic.jdbc;import java.sql.connection;import java.sql.drivermanager;import  java.sql.preparedstatement;import java.sql.resultset;import com.mysql.jdbc.statement;public  class jdbchive {   private static connection conn;    Private static statement st;      public static void  main (String[] args)  throws exception {      connection  connection = getconnection ();       system.out.println (" Connection: "+connection";      string sql =  "Show tables";       PreparedStatement prepareStatement =  Connection.preparestatement (SQL);      resultset rs =  Preparestatement.executequery ();      &Nbsp;while (Rs.next ()) {         string db =  Rs.getstring (1);          system.out.println (db);       }   }   /*  getting a database connection function  */    Private static connection getconnection ()  {      connection  con = null; //  Create a Connection object to connect to the database       try {          class.forname ("Org.apache.hive.jdbc.HiveDriver");//   Load HIVE2 data-driven          con =  Drivermanager.getconnection (                 "Jdbc:hive2://192.168.174.131:10000/default",  "root",  null);//  Create data connection        } catch  (exception e) &NBsp {         system.out.println ("Hive database connection Failed"  +  E.getmessage ());      }      return con;  //  return the established database connection    }}





This article is from the "Simplelife" blog, make sure to keep this source http://simplelife.blog.51cto.com/9954761/1968412

JDBC Connection Hive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.