JDBC Usage for hive

Source: Internet
Author: User
Tags stmt

① new MAVEN project, load dependent packages

Add in Pom.xml

<dependency>

<groupId>jdk.tools</groupId>

<artifactId>jdk.tools</artifactId>

<version>1.8</version>

<scope>system</scope>

<systempath>${java_home}/Lib/tools.jar</systempath>

</dependency>

<dependency>

<groupId>JUnit</groupId>

<artifactId>JUnit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactid>hive-exec</artifactId>

<version>2.1.1</version>

</dependency>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactid>hive-jdbc</artifactId>

<version>2.1.1</version>

</dependency>

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>Hadoop-common</artifactid>

<version>2.6.1</version>

</dependency>

② Start the Hive service, start the cluster

(hive1.2.1 version needs to be started with Hiveserver2)

Hive–-service hiveserver2–-hiveconf hive.server2.thrift.port=11111 (Turn on service and set port number)

③ Configuration Core-xite.xml

<property>

<name>hadoop.proxyuser.neworigin.groups</name>

<value>*</value>

<description>allow the Superuser Oozie to impersonate all members of the group Group1 and group2</description>

</property>

<property>

<name>hadoop.proxyuser.neworigin.hosts</name>

<value>*</value>

<description>the Superuser can connect only from Host1 and Host2 to impersonate a user</description>

</property>

④ writing Java code

 PackageCom.neworigin.HiveTest1;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJdbcutil {StaticString drivername= "Org.apache.hive.jdbc.HiveDriver";StaticString url= "Jdbc:hive2://s100:11111/myhive";StaticString user= "Neworigin";StaticString pass= "123";//Create a connection Public StaticConnection Getconn ()throwsexception{Class.forName (drivername); Connection Conn=drivermanager.getconnection (Url,user,pass); returnConn;}//Create command Public StaticStatement getstmt (Connection conn)throwssqlexception{returnconn.createstatement ();} Public voidClosefunc (Connection conn,statement stmt)throwssqlexception{stmt.close ();    Conn.close (); }} PackageCom.neworigin.HiveTest1;Importjava.sql.Connection;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.Statement; Public classJdbctest { Public Static voidMain (string[] args)throwsException {Connection conn= Jdbcutil.getconn ();//Create a connectionStatement STMT=JDBCUTIL.GETSTMT (conn);//To create an execution objectString sql= "SELECT * from Myhive.employee";//Execute SQL statementString sql2= "CREATE TABLE Jdbctest (ID int,name string)"; ResultSet Set= Stmt.executequery (SQL);//returns the result set of the executionResultSetMetaData meta = set.getmetadata ();//get field     while(Set.next ()) { for(intI=1;i<=meta.getcolumncount (); i++) {System.out.print (set.getstring (i)+" ");    } System.out.println (); } System.out.println ("The first SQL statement finishes executing"); Booleanb =Stmt.execute (SQL2); if(b) {System.out.println (Success); }    }}

JDBC Usage for 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.