日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

hive2 java连接_用Java代码通过JDBC连接Hiveserver2

發布時間:2025/3/11 java 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hive2 java连接_用Java代码通过JDBC连接Hiveserver2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.在終端啟動hiveserver2

#hiveserver2

2.使用beeline連接hive

另外打開一個終端,輸入如下命令(xavierdb必須是已經存在的數據庫)

#beeline -u jdbc:hive2://localhost:10000/xavierdb -n hive -p hive

3.添加maven依賴

org.apache.hive

hive-jdbc

1.1.0

junit

junit

4.9

org.apache.hadoop

hadoop-common

2.6.0

org.apache.hadoop

hadoop-client

2.6.0

org.apache.hive

hive-metastore

1.1.0

org.apache.hive

hive-exec

1.1.0

maven依賴

出現過的錯誤: 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://172.19.224.213:10000/xavierdb";

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 selectDb() 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) " +

"row format delimited " +

"fields terminated by ',' " +//字段與字段之間的分隔符

"collection items terminated by ':'"+//一個字段各個item的分隔符

"lines terminated by '\n' ");//行分隔符

}

// 查詢所有表

@Test

public void selectTab() 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 student");

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 student");

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 student1");

}

@After

public void destory() throws SQLException {

if (res != null) state.close();

if (state != null) state.close();

if (con != null) con.close();

}

}

標簽:JDBC,Java,res,void,hive,throws,Hiveserver2,state,public

來源: https://www.cnblogs.com/xavier-xd/p/10399581.html

總結

以上是生活随笔為你收集整理的hive2 java连接_用Java代码通过JDBC连接Hiveserver2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。