Sample program and parsing of Java connection hive

Source: Internet
Author: User
Tags stmt

Later programming is based on this sample to save time on the API lookup.

private static String drivername = "Org.apache.hadoop.hive.jdbc.HiveDriver";

try{

Class.forName (drivername);//Data driven, different data drivers can be used for different class paths.

}catch (ClassNotFoundException e) {

E.printstacktrack ();//optional

System.exit (1);//optional

}

Connection con = drivermanager.getconnection ("Jdbc:hive://localhost:10000/default", "", "" ");

Drivermanager:the Basic service for managing a set of JDBC drivers.

A detailed description of the method used is as follows:

Here, Subprotocol = "Hive";

Statement stmt = Con.createstatement ();

A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments.

A simple SQL statement that can be used to execute without parameters

String tableName = "Customerhivetablejava";

Table to be created;

ResultSet res = stmt.executequery ("CREATE TABLE" +tablename+ "(custid int, fname string,lname string) row format delimited Fields terminated by ', ' stored as textfile ');

ResultSet a table of data representing a database result set, which is usually generated by executing A statement that Q Ueries the database.

Show tables

String st = "Show tables" + TableName + "";

System.out.println ("Running:" + sql);

res = stmt.executequery (sql);//res records all returned records

if (Res.next ()) {

System.out.println (res.getstring (1));

}

Loading data into the table

String filepath = "/path";

sql = "Load data local Inpath" +filepath+ "into table" + tableName;

System.out.println ("Running:" + sql);

Sample program and parsing of Java 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.