JDBC 連接Hive 簡單樣例(開啟Kerberos)

今天在移動的云平臺上通過jdbc連接hive,發現云平臺使用了 kerberos的認證。與寧波實驗環境不同。

發現一文解決了問題,轉載如下:

原文地址:http://blog.csdn.net/zengmingen/article/details/78605086

------------------------------

  • 運用 Ambari 搭建的HDP 集群,由于開啟了kerberos ,對外提供Hive數據時統一用JDBC 的方式,所以寫了下面這么一個簡單樣例供第三方數據接入參考。

代碼如下所示:

package com.bmsoft.hive.impl;import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/*** 簡單的jdbc連接hive實例(已開啟kerberos服務)*/public class HiveSimple2 {/*** 用于連接Hive所需的一些參數設置 driverName:用于連接hive的JDBC驅動名 When connecting to* HiveServer2 with Kerberos authentication, the URL format is:* jdbc:hive2://<host>:<port>/<db>;principal=* <Server_Principal_of_HiveServer2>*/private static String driverName = "org.apache.hive.jdbc.HiveDriver";// 注意:這里的principal是固定不變的,其指的hive服務所對應的principal,而不是用戶所對應的principalprivate static String url = "jdbc:hive2://bigdata40:10000/admin;principal=hive/bigdata40@BIGDATA.COM";private static String sql = "";private static ResultSet res;public static Connection get_conn() throws SQLException, ClassNotFoundException {/** 使用Hadoop安全登錄 **/org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();conf.set("hadoop.security.authentication", "Kerberos");if (System.getProperty("os.name").toLowerCase().startsWith("win")) {// 默認:這里不設置的話,win默認會到 C盤下讀取krb5.initSystem.setProperty("java.security.krb5.conf", "C:/Windows/krbconf/bms/krb5.ini");} // linux 會默認到 /etc/krb5.conf 中讀取krb5.conf,本文筆者已將該文件放到/etc/目錄下,因而這里便不用再設置了try {UserGroupInformation.setConfiguration(conf);UserGroupInformation.loginUserFromKeytab("test2/hdp39@BMSOFT.COM", "./conf/test2.keytab");} catch (IOException e1) {e1.printStackTrace();}Class.forName(driverName);Connection conn = DriverManager.getConnection(url);return conn;}/*** 查看數據庫下所有的表** @param statement* @return*/public static boolean show_tables(Statement statement) {sql = "SHOW TABLES";System.out.println("Running:" + sql);try {ResultSet res = statement.executeQuery(sql);System.out.println("執行“+sql+運行結果:");while (res.next()) {System.out.println(res.getString(1));}return true;} catch (SQLException e) {e.printStackTrace();}return false;}/*** 獲取表的描述信息** @param statement* @param tableName* @return*/public static boolean describ_table(Statement statement, String tableName) {sql = "DESCRIBE " + tableName;try {res = statement.executeQuery(sql);System.out.print(tableName + "描述信息:");while (res.next()) {System.out.println(res.getString(1) + "\t" + res.getString(2));}return true;} catch (SQLException e) {e.printStackTrace();}return false;}/*** 刪除表** @param statement* @param tableName* @return*/public static boolean drop_table(Statement statement, String tableName) {sql = "DROP TABLE IF EXISTS " + tableName;System.out.println("Running:" + sql);try {statement.execute(sql);System.out.println(tableName + "刪除成功");return true;} catch (SQLException e) {System.out.println(tableName + "刪除失敗");e.printStackTrace();}return false;}/*** 查看表數據** @param statement* @return*/public static boolean queryData(Statement statement, String tableName) {sql = "SELECT * FROM " + tableName + " LIMIT 20";System.out.println("Running:" + sql);try {res = statement.executeQuery(sql);System.out.println("執行“+sql+運行結果:");while (res.next()) {System.out.println(res.getString(1) + "," + res.getString(2) + "," + res.getString(3));}return true;} catch (SQLException e) {e.printStackTrace();}return false;}/*** 創建表** @param statement* @return*/public static boolean createTable(Statement statement, String tableName) {sql = "CREATE TABLE test_1m_test2 AS SELECT * FROM test_1m_test"; //  為了方便直接復制另一張表數據來創建表System.out.println("Running:" + sql);try {boolean execute = statement.execute(sql);System.out.println("執行結果 :" + execute);return true;} catch (SQLException e) {e.printStackTrace();}return false;}public static void main(String[] args) {try {Connection conn = get_conn();Statement stmt = conn.createStatement();// 創建的表名String tableName = "test_100m";show_tables(stmt);// describ_table(stmt, tableName);/** 刪除表 **/// drop_table(stmt, tableName);// show_tables(stmt);// queryData(stmt, tableName);createTable(stmt, tableName);conn.close();} catch (Exception e) {e.printStackTrace();} finally {System.out.println("!!!!!!END!!!!!!!!");}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176

pom.xml 文件如下所示:

<dependencies><!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><version>1.2.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec</artifactId><version>1.2.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-metastore</artifactId><version>1.2.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hive/hive-common --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-common</artifactId><version>1.2.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.hive/hive-service --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-service</artifactId><version>1.2.1</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version><type>jar</type></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.7.3</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

參考文檔:?
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients

文檔其中比較值得注意的一點是:

JDBC Client Setup for a Secure Cluster
When connecting to HiveServer2 with Kerberos authentication, the URL format is:
jdbc:hive2://<host>:<port>/<db>;principal=<Server_Principal_of_HiveServer2>
  • 1
  • 2
  • 3
  • 這里的principal是固定不變的,其指的hive服務所對應的principal,而不是用戶所對應的principal; 對于這里的可以為不存在的數據庫,但是如果這么做那么在查詢表的時候則需要指出其所在的庫(如db.tablename),否則默認會查詢所對應的表。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/538187.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/538187.shtml
英文地址,請注明出處:http://en.pswp.cn/news/538187.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

新年快樂輪播特效html,基于owl-carousel的卡片水平輪播展示特效

這是一款基于owl-carousel的卡片水平輪播展示特效。該卡片輪播展示特效可以通過前后導航按鈕來切換卡片&#xff0c;它是響應式設計&#xff0c;在手機等小屏幕設備上&#xff0c;會自動調節為只展示一個卡片。使用方法在頁面中引入bootstrap.css和style.css文件&#xff0c;以…

parameter縮略語_“參數”的英文縮寫有嗎?

展開全部“參數”的英文e69da5e887aa3231313335323631343130323136353331333365643662縮寫是“parm”。1.音標&#xff1a;[ prɑ:m ]2.具體含義&#xff1a;參數3.雙語例句&#xff1a;It involves the same parameter as that involved in the enhancement factor for coagul…

JDBC實現從Hive抽取數據導入Oracle

環境&#xff1a;浙江移動華為云平臺 云平臺大數據采用了 Kerberos 認證。 開發歷程&#xff1a; 1、在寧波大數據實驗環境測試通過了JDBC實現從Hive抽取數據導入Oracle功能。 2、通過查看其它項目的數據庫訪問配置&#xff0c;知道了云平臺上的oracle配置。 3、獲取hive的…

加拿大計算機專業學什么,加拿大哥倫比亞大學計算機專業課程

計算機專業是加拿大哥倫比亞大學研究生熱門專業&#xff0c;很多準備申請加拿大研究生留學的都非常關心加拿大英屬哥倫比亞大學計算機專業研究生申請需要注意哪些問題?針對這個問題&#xff0c;出國留學小編為大家進行簡要介紹。英屬哥倫比亞大學計算機科學碩士專業優勢&#…

PC,移動端H5實現實現小球加入購物車效果

HTML部分&#xff1a; <!DOCTYPE html> <html> <head><meta http-equiv"content-type" content"text/html; charsetUTF-8"><title>基于jquery.fly模仿天貓拋物線加入購物車特效代碼</title><style>* {margin: 0…

云桌面部署_云桌面時代降臨-青椒云工作站

云計算理念是當代互聯網時代的新型理念&#xff0c;用戶享受的所有資源、所有應用程序全部都由一個存儲和運算能力超強的云端后臺來提供。云桌面是基于云計算技術&#xff0c;實現各種終端設備之間的互聯互通。我們的電子設備等都只是一個單純的顯示和操作終端&#xff0c;它們…

一些配置文件

--用戶用linux用戶&#xff0c;配置hadoop的Linux用戶。非hive連接mysql的用戶 HIVE_DRIVERorg.apache.hive.jdbc.HiveDriver HIVE_URLjdbc:hive2://192.168.78.128:10000/default HIVE_UserName root HIVE_PassWord 123456 ORACLE_DRIVERoracle.jdbc.driver.OracleDriver …

妙味css3課程---1-1、css中自定義屬性可以用屬性選擇器么

妙味css3課程---1-1、css中自定義屬性可以用屬性選擇器么 一、總結 一句話總結&#xff1a;可以的。 1、如何實現用屬性選擇器實現a標簽根據href里面含有的字段選擇背景圖片&#xff1f; p a[href*text]{background-image:url(img/text.gif);} 2、瀏覽器前綴在js中怎么寫&#…

吉林大學計算機與科學專業排名,吉林大學專業排名 哪些王牌專業推薦就讀

吉林大學&#xff0c;簡稱“吉大”&#xff0c;位于吉林省省會長春。是一所“985”、“211”、“雙一流”大學。下面我們將要來了解到的是吉林大學的專業排名&#xff0c;他的王牌專業有哪些&#xff0c;一起來看一下吧&#xff01;吉林大學專業排名 哪些王牌專業推薦就讀吉林大…

c51為啥要宏定義時鐘_51單片機時鐘實訓報告

時、分、秒計時器設計一、任務及要求用51單片機設計時、分、秒計時器&#xff0c;具體要求如下。1、具有時、分、秒計時功能和8位數碼管顯示功能&#xff0c;顯示格式為&#xff1a;“時&#xff0d;分&#xff0d;秒”&#xff1b;2、用Proteus設計仿真電路進行結果仿真&#…

servlet獲取不到Angular4 post過來的參數

副標題&#xff1a;Java如何從HttpServletRequest中讀取HTTP請求的body 今天接觸一個項目&#xff0c;前臺用angular4 post訪問后臺&#xff0c; this.httpService.post({url: quality/IMSI_MO, IMSImsg: this.InputMsg, TIME1: time1, TIME2: time2 }).subscribe(res > {t…

ios如何看idfv_如何無中生有資源搜索神器

作者 | Castie! 來源 | https://coderzsq.github.io日常扯淡首先申明&#xff0c;這絕對不是標題黨&#xff0c;看完全文你一定也能夠自行的寫出一個資源搜索App&#xff0c;其實這個App&#xff0c;本來是想在App Store賣錢的&#xff0c;畢竟感覺需求量還是很大&#xff0c;雖…

計算機語言需要有英語基礎,有關“計算機語言”的問題

一般需要一點英語基礎&#xff0c;因很多語句其實是英文單詞&#xff0c;且編譯錯誤信息大多是用代碼或英文提示的。但一個完全不會英語的人只要努力還是能學會編程語言的&#xff0c;計算機語言中涉及的英文單詞大多不是很難&#xff0c;花點時間完全能記住&#xff0c;出錯的…

Eclipse Console 加大顯示的行數,禁止彈出

原文鏈接&#xff1a;http://blog.csdn.net/leidengyan/article/details/5686691 -------------------------------------------------- Eclipse Console 加大顯示的行數&#xff1a; 在 Preferences-〉Run/Debug-〉Console里邊&#xff0c;去掉對Limit console output的選擇&…

excel range 判斷日期型_為什么精英都是Excel控?

讓你相見恨晚的Excel精髓攻略&#xff0c;吐血整理&#xff01;三小時幫你提升90%的效率&#xff0c;這份Excel教程必須&#xff01;&#xff08;點贊收藏&#xff09;Excel能夠滿足工作中絕大部分的數據分析需求&#xff0c;很多小細節的設計會節省下工作中非常多的時間&#…

移動端 | Vue.js對比微信小程序基礎語法

&#xff08;1&#xff09;vue 自定義組件與父組件的通信&#xff0c;props&#xff1a;[abb],可以看成自組建的一個自定義屬性 &#xff08;2&#xff09;vue 模版語法{{}} 只能是在DOM中插入&#xff0c;<div>{{acc}}</div>, 綁定屬性的話應v-bind&#xff1a;id…

計算機組裝電源線排,主機箱背部走線技巧 組裝電腦走背線與理線教程

近年來&#xff0c;裝機行業流行一個術語&#xff0c;即“走背線”&#xff0c;那么走背線是什么&#xff1f;裝機之家小編簡單介紹下&#xff0c;通俗的說&#xff1a;走背線就是針對電腦機箱&#xff0c;裝機的時候&#xff0c;將機箱內部和電源的線材做到最干凈整潔&#xf…

查看oracle數據庫允許的最大連接數和當前連接數

原文鏈接&#xff1a;http://blog.csdn.net/zmx729618/article/details/54018629 ----------------------------------------------------------------------------- 在查看數據的連接情況很有用&#xff0c;寫完程序一邊測試代碼一邊查看數據庫連接的釋放情況有助于分析優化…

干煸線椒的做法_美食:農家蒸土雞,剁椒梅鱭魚干,干煸茶樹菇,芹菜炒牛肉的做法...

閱讀本文前&#xff0c;請您先點擊上面“藍色字體”&#xff0c;再點擊“關注”&#xff0c;這樣您就可以免費收到我們的最新內容了&#xff0c;每天都會有更新&#xff0c;完全是免費訂閱&#xff0c;請放心關注。圖文來源網絡&#xff0c;侵權聯系刪除&#xff01; …

計算機出現假桌面怎么解決辦法,win10系統apphangxprocb1引起桌面假死怎么解決【圖文】...

電腦死機怎么辦&#xff1f;有win10系統用戶反應win10系統apphangxprocb1引起桌面假死怎么解決&#xff1f;出現這種情況該怎么解決&#xff1f;下面就將方法分享給大家。描述:出現了一個問題,該問題導致了此程序停止與 Windows 進行交互。錯誤的應用程序路徑: C:Windowsexplor…