Java Operation Hive via JDBC

Source: Internet
Author: User

Direct code: Remember to open hive JDBC Service hive--service Hiveserver

Package Hive;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.statement;public class Hivedemo {static{//registered the JDBC driver try {class.forname (" Org.apache.hadoop.hive.jdbc.HiveDriver ");} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public static void Main (string[] args) throws Exception {//create connection Connection conn = Drivermanager.getconnection ("jdbc:hive ://hadoop:10000/default "," "," ");//SYSTEM.OUT.PRINTLN (conn); Statement st = Conn.createstatement (); String tableName = "u1_data";//delete Table St.executequery ("drop table" +tablename);//CREATE TABLE ResultSet rs = st.executequery ("Create Table "+tablename+" ("+" userid int, "+" MovieID int, "+" rating int, "+" city string, "+" Viewtime string "+") "+" row format D elimited "+" fields terminated by ' \ t ' "+" stored as textfile ");//Show all tables String sql =" Show Tables "; System.out.println ("Running:" +sql), rs = st.executequery (sql), if (Rs.next ()) {System.out.println (rs.getstring (1));} Get table information sql = "desCribe "+tablename; System.out.println ("Running:" +sql), rs = st.executequery (sql), while (Rs.next ()) {System.out.println (rs.getstring (1) + "\ T" +rs.getstring (2));} Load data string FilePath = "Hdfs://hadoop:9000/input"; sql = "Load data inpath '" +filepath+ "' Overwrite into table" +tablename ; System.out.println ("Running:" +sql); rs = st.executequery (sql);//query data sql = "SELECT * from" +tablename+ "Limit 5"; System.out.println ("Running:" +sql), rs = st.executequery (sql), while (Rs.next ()) {System.out.println (rs.getstring (3) + "\ T" +rs.getstring (4));} Number of queries sql = "SELECT COUNT (*) from" +tablename; System.out.println ("Running:" +sql), rs = st.executequery (sql), while (Rs.next ()) {System.out.println (rs.getstring (1) );} Close resource Rs.close (); St.close (); Conn.close ();}}

 

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.