1.在終端啟動hiveserver2
#hiveserver2
2.使用beeline連接hive
另外打開一個終端,輸入如下命令(xavierdb必須是已經存在的數據庫)
#beeline -u jdbc:hive2://localhost:10000/xavierdb -n hive -p hive
3.添加maven依賴

<!-- https://mvnrepository.com/artifact/org.apache.hadoop.hive/hive-jdbc --> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore --> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-metastore</artifactId> <version>1.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore --> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>1.1.0</version> </dependency>
出現過的錯誤: Error: Could not open client transport with JDBC Uri: jdbc:hive2://localhost:10000/default
解決辦法:檢查發現運行beeline時Driver版本Driver: Hive JDBC (version 1.1.0-cdh5.16.1)比maven依賴中的Driver版本低,將maven版本調至1.1.0問題解決
Java API測試:
注意:這里的url必須是beeline值中使用的url
package TestOption;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import java.sql.*;
/**
* @Author:Xavier
* @Data:2019-02-18 11:43
**/
public class HiveOption {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://yourhost:10000/yourdatabase";
private static Connection con = null;
private static Statement state = null;
private static ResultSet res = null;
//加載驅動,創建連接
@Before
public void init() throws ClassNotFoundException, SQLException {
Class.forName(driverName);
con = DriverManager.getConnection(url, "hive", "hive");
state = con.createStatement();
}
//創建數據庫
@Test
public void CreateDb() throws SQLException {
state.execute("create database xavierdb1");
}
// 查詢所有數據庫
@Test
public void showtDb() throws SQLException {
res = state.executeQuery("show databases");
while (res.next()) {
System.out.println(res.getString(1));
}
}
// 刪除數據庫
@Test
public void dropDb() throws SQLException {
state.execute("drop database if exists xavierdb1");
}
/*
*
*
* 內部表基本操作
*
*
* */
// 創建表
@Test
public void createTab() throws SQLException {
state.execute("create table if not exists student ( " +
"name string , " +
"age int , " +
"agent string ," +
"adress struct<street:STRING,city:STRING>) " +
"row format delimited " +
"fields terminated by ',' " +//字段與字段之間的分隔符
"collection items terminated by ':'" +//一個字段各個item的分隔符
"lines terminated by '\n' ");//行分隔符
}
// 查詢所有表
@Test
public void showTab() throws SQLException {
res = state.executeQuery("show tables");
while (res.next()) {
System.out.println(res.getString(1));
}
}
// 查看表結構
@Test
public void descTab() throws SQLException {
res = state.executeQuery("desc emp");
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
}
// 加載數據
@Test
public void loadData() throws SQLException {
String infile = " '/root/studentData' ";
state.execute("load data local inpath " + infile + "overwrite into table student");
}
// 查詢數據
@Test
public void selectTab() throws SQLException {
res = state.executeQuery("select * from student1");
while (res.next()) {
System.out.println(
res.getString(1) + "-" +
res.getString(2) + "-" +
res.getString(3) + "-" +
res.getString(4));
}
}
// 統計查詢(會運行mapreduce作業,資源開銷較大)
@Test
public void countData() throws SQLException {
res = state.executeQuery("select count(1) from student");
while (res.next()) {
System.out.println(res.getInt(1));
}
}
// 刪除表
@Test
public void dropTab() throws SQLException {
state.execute("drop table emp");
}
/*
* 外部表基本操作
*
*外部表刪除后,hdfs文件系統上的數據還在,
*重新創建同路徑外部表后,其數據仍然存在
*
* */
//創建外部表
@Test
public void createExTab() throws SQLException {
state.execute(