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:
<CONFIGURATION><PROPERTY><NAME>JAVAX.JDO.OPTION.CONNECTIONURL</NAME><VALUE>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