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 ();}}