本文版權歸mephisto和博客園共有,歡迎轉載,但須保留此段聲明,並給出原文連接,謝謝合做。html
文章是哥(mephisto)寫的,SourceLinkgit
上一篇,咱們對hive的數據導出,以及集羣Hive數據的遷移進行描述。瞭解到了基本的hive導出操做。這裏,咱們將對hive的CLI及JDBC這些實用性很強的兩個方便進行簡要的介紹。github
下面咱們開始介紹hive的CLI和JDBC。sql
一:說明
在0.11以前只有Hive CLI,他須要安裝Hive Client才能使用。是一個重量級的命令行工具。鏈接的服務器是HiveServer1。shell
二:語法:
usage: hive -d,--define <key=value> Variable subsitution to apply to hive commands. e.g. -d A=B or --define A=B -e <quoted-query-string> SQL from command line -f <filename> SQL from files -H,--help Print help information -h <hostname> Connecting to Hive Server on remote host --hiveconf <property=value> Use value for given property --hivevar <key=value> Variable subsitution to apply to hive commands. e.g. --hivevar A=B -i <filename> Initialization SQL file -p <port> Connecting to Hive Server on port number -S,--silent Silent mode in interactive shell -v,--verbose Verbose mode (echo executed SQL to the console)三:官網例子1
$HIVE_HOME/bin/hive -e 'select a.col from tab1 a'四:官網例子2
運行腳本文件apache
$HIVE_HOME/bin/hive -f /home/my/hive-script.sql
一:介紹
爲了使得CLI輕量化,後來Hive作出了Beeline和HiveServer2。Beeline是一個基於JDBC的SQLLine CLI。api
二:官網例子
bin/beeline !connect jdbc:hive2://localhost:10000 scott tiger org.apache.hive.jdbc.HiveDriver三:實戰
hiveserver2的默認端口是10000。服務器
beeline -u jdbc:hive2://h188:10000
查看有哪些表。app
show tables;![]()
這裏能夠看到咱們在上面幾篇測試的時候的表score,score1,score2,咱們查下score的數據。分佈式
select * from score;
一:介紹
咱們能夠在代碼中經過jdbc鏈接hive。這樣在實際運用場景中就很方便,很原有的非分佈式計算的開發方式基本相似,具備很強的適用性。
二:新建工程
新建工程com.per.hive
三:引入包
版本根據本身使用hadoop集羣而定
commons-logging-1.2.jar hadoop-common-2.6.0.jar hive-exec-0.13.1.jar hive-jdbc-0.13.1.jar hive-service-0.13.1.jar httpclient-4.3.4.jar httpcore-4.3.2.jar log4j-1.2.16.jar slf4j-api-1.7.6.jar slf4j-log4j12-1.7.6.jar
四:添加類HiveDemo
添加HiveDriver
static { try { Class.forName("org.apache.hive.jdbc.HiveDriver"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } }添加test()方法
/** * @Description : 測試 */ private static void test() { try (Connection con = DriverManager .getConnection("jdbc:hive2://h188:10000/")) { Statement stm = con.createStatement(); ResultSet rs = stm.executeQuery("select * from score "); while (rs.next()) { String info = rs.getString(1); info += " " + rs.getString(2); info += " " + rs.getString(3); info += " " + rs.getString(4); System.out.println(info); } } catch (Exception ex) { ex.printStackTrace(); } }調用
public static void main(String[] args) { test(); }
五:運行
運行,查看結果,能夠看見,程序運行的結果與咱們在Beeline命令行查看的結果一致。
這樣咱們的Hive的CLI和JDBC告一段落。
--------------------------------------------------------------------
到此,本章節的內容講述完畢。
https://github.com/sinodzh/HadoopExample/tree/master/2016/com.per.hive
本文版權歸mephisto和博客園共有,歡迎轉載,但須保留此段聲明,並給出原文連接,謝謝合做。
文章是哥(mephisto)寫的,SourceLink