Alex's Hadoop Rookie Tutorial: Lesson 11th Java calls to hive

Source: Internet
Author: User
Tags stmt

Statement

    • This article is based on CentOS 6.x + CDH 5.x
When it comes to hive, you have to talk about how to call hive when you write a program. Here's an example of how to call hive query data data through Java to prepare a text file called A.txt, the content is
1,terry2,alex3,jimmy4,mike5,kate

and uploaded to the hive server's/data/directory.
The JDBC call method loads the driver load driver (only hive2 jdbc).
Class.forName ("Org.apache.hive.jdbc.HiveDriver");

Connecting to a database
Connection con = drivermanager.getconnection ("Jdbc:hive2://host1:10000/default", "Hive", "");

    • The host1 here is the host name
    • 10000 is the hive default port name
    • Default is the database
    • Hive is the default user name and the default password is empty
Database Operation statement Delete table
Stmt.execute ("drop table if exists" + tableName);

Create a table
Stmt.execute ("CREATE TABLE" + TableName + "(key int, value string) ROW FORMAT delimited fields TERMINATED by ' \054 '");

Querying data
ResultSet res = stmt.executequery ("SELECT * from" + tableName);

Import data
Stmt.execute ("Load data local inpath '" + filepath + "' into table" + tableName);


Example building a project open Eclipse to build a MAVEN project

Pom.xml
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><groupid>org.crazycake</groupid><artifactid> Play-hive</artifactid><version>0.0.1-snapshot</version><packaging>jar</packaging ><name>play-hive</name><url>http://maven.apache.org</url><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding></properties><dependencies ><dependency><groupId>junit</groupId><artifactId>junit</artifactId>< version>3.8.1</version><scope>test</scope></dependency><dependency>< groupid>org.apache.hive</groupid><artifactid>hive-jdbc</artifactid><version>0.14.0 </version></dependency><depenDency><groupid>org.apache.hadoop</groupid><artifactid>hadoop-common</artifactid> <version>2.2.0</version></dependency></dependencies><build><plugins>< plugin><artifactid>maven-compiler-plugin</artifactid><version>2.0.2</version>< configuration><source>1.6</source><target>1.6</target><encoding>utf-8</ encoding><optimise>true</optimise><compilerargument>-nowarn</compilerargument></ configuration></plugin><plugin><groupid>org.apache.maven.plugins</groupid>< artifactid>maven-shade-plugin</artifactid><version>2.3</version><configuration>< Transformers><transformerimplementation= " Org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer "></transformer></ Transformers></configuration><executions><execution><phase>package</phase><goals><goal>shade</goal></goals></execution></executions></ Plugin></plugins></build></project>



The most important of these is the two paragraphs
<dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId> <version>0.14.0</version></dependency><dependency><groupId>org.apache.hadoop< /groupid><artifactid>hadoop-common</artifactid><version>2.2.0</version></ Dependency>

Everything else doesn't matter.
Build a table, import, and query data to create a class hivejdbcclient
Package Org.crazycake.play_hive;import Java.sql.sqlexception;import Java.sql.connection;import java.sql.ResultSet; Import java.sql.statement;import java.sql.drivermanager;/** * Test Hive Client Connection * @author Alexxiyang (https://github.com/ Alexxiyang) * */public class Hivejdbcclient {/** * Note: Hive-server2 referenced driver is org.apache.hive.* and hive-server is org   . apache.hadoop.hive.* */private static String drivername = "Org.apache.hive.jdbc.HiveDriver";       /** * @param args * @throws SQLException * * public static void Main (string[] args) throws SQLException {try {    Class.forName (drivername);      } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();    System.exit (1); The default port for//hive is 10000, if you want to modify the Hive.server2.thrift.port property value of the Hive-site.xml file//default username hive, the default password is empty Connection con = D        Rivermanager.getconnection ("Jdbc:hive2://host1:10000/default", "Hive", "");    Statement stmt = Con.createstatement (); The table name of the test Testhivedrivertable String tableName = "testhivedrivertable";        Delete Stmt.execute if it already exists ("drop table if exists" + tableName); Create this table Stmt.execute ("CREATE TABLE" + TableName + "(key int, value string) ROW FORMAT delimited fields TERMINATED by    ' \054 ');    See if the creation succeeds String sql = "Show tables '" + tableName + "'";    System.out.println ("Running:" + sql);    ResultSet res = stmt.executequery (SQL);    if (Res.next ()) {System.out.println (res.getstring (1));    }//Look at the following table structure sql = "describe" + tableName;    System.out.println ("Running:" + sql);    res = stmt.executequery (SQL);    while (Res.next ()) {System.out.println (res.getstring (1) + "\ T" + res.getstring (2));    }//Load data into the table//Note:filepath is the location of the local file, note that this is not your computer!    You have to upload this file to the server first, and then the path here is the path to the file on the server//NOTE:/data/a.txt String filepath = "/data/a.txt";    sql = "Load data local inpath '" + filepath + "' into table" + tableName;    System.out.println ("Running:" + sql); StMt.execute (SQL);    SELECT * Query sql = "SELECT * from" + tableName;    System.out.println ("Running:" + sql);    res = stmt.executequery (SQL);    while (Res.next ()) {System.out.println (String.valueof (Res.getint (1)) + "\ T" + res.getstring (2));    }//Count look how many data sql = "SELECT COUNT (1) from" + TableName;    System.out.println ("Running:" + sql);    res = stmt.executequery (SQL);    if (Res.next ()) {System.out.println (res.getstring (1)); }  }}


Output is
Running:show tables ' testhivedrivertable ' Testhivedrivertablerunning:describe Testhivedrivertablekeyintvaluestringrunning:load data local inpath '/data/a.txt ' into table Testhivedrivertablerunning:select * from Testhivedrivertable1terry2alex3jimmy4mike5katerunning:select count (1) from Testhivedrivertable

In fact, the Java call is very simple, that is, you execute the statement in the hive shell with JDBC to do it again, so you transfer the past statement of the environment is the Hive server machine, which is written in the path from the hive server host root directory path to find data, So our a.txt has to be uploaded to the server, and this code will run normally.

Resources
    • Https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC

Alex's Hadoop Rookie Tutorial: Lesson 11th Java calls to hive

Related Article

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.