Hive學習筆記

環境描述:

Hadoop集羣版本:hadoop-1.2.1

Hive版本:hive-0.10.0

Hive在使用時只在一個節點上安裝即可。


一、Hive安裝配置

1.上傳hive壓縮包(hive-0.10.0-bin.tar.gz)hadoop集羣的某個節點服務器,解壓安裝:
tar -zxvf hive-0.10.0.tar.gz -C /home/suh/

2.修改hive環境配置文件hive-env.sh,增加以下配置,指明hadoop安裝路徑:(測試好像可以不用指明,也行)

  export HADOOP_HOME=/home/suh/hadoop-1.2.1

  

3.配置hive 使用MySQL數據庫保存 metastore

將默認配置文件模板重命名,然後增加相應配置:

        cp hive-default.xml.template hive-site.xml 

修改hive-site.xml(將<property></property> 對都刪除)
添加如下內容:
<property>
 <name>javax.jdo.option.ConnectionURL</name>
 <value>jdbc:mysql://boss:3306/hive_test?createDatabaseIfNotExist=true</value>
 <description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
 <name>javax.jdo.option.ConnectionDriverName</name>
 <value>com.mysql.jdbc.Driver</value>
 <description>Driver class name for a JDBC metastore</description>
</property>
<property>
 <name>javax.jdo.option.ConnectionUserName</name>
 <value>root</value>
 <description>username to use against metastore database</description>
</property>
<property>
 <name>javax.jdo.option.ConnectionPassword</name>
 <value>123456</value>
 <description>password to use against metastore database</description>
</property>


4.以上配置hive完成後,將mysql的連接驅動jar包拷貝到$HIVE_HOME/lib目錄下
如果出現沒有權限的問題,在mysql授權(在安裝mysql的機器上執行)
mysql -uroot -p
#(執行下面的語句  *.*:所有庫下的所有表   %:任何IP地址或主機都可以連接)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
FLUSH PRIVILEGES;

注意:把mysql的數據庫字符類型改爲latin1,否則show table 時候就開始報錯。

二、Hive 使用

進入到$HIVE_HOME/bin目錄,執行命令:./hive 進入到hive模式,接下里的操作就同mysql類似。

1.建表(默認是內部表)
create table trade_detail(id bigint, account string, income double, expenses double, time string) 
row format delimited fields terminated by '\t';

建分區表
create table trade(tradedate string,tradetime string,securityid string,bidpx1 double,bidsize1 string,offerpx1 double,offersize1 string) 
partitioned by (trade_date string) 
row format delimited fields terminated by ',';

建外部表
create external table td_ext(id bigint, account string, income double, expenses double, time string) 
row format delimited fields terminated by '\t' 
location '/td_ext';


2、Hive中的三種不同的數據導出方式
(1、導出到本地文件系統:
     insert overwrite local directory '/home/suh/hive/trade_01' select * from trade where tradedate='20130726';
 
PS:這條HQL的執行需要啓用Mapreduce完成,運行完這條語句之後,將會在本地文件系統的/home/suh/hive/trade_01目錄下生成文件。
這個文件是Reduce產生的結果(這裏生成的文件名是000000_0),數據中的列與列之間的分隔符是^A(ascii碼是\00001)。
 
(2、導出到HDFS中:
insert overwrite directory '/user/trade02' select * from trade where tradedate='20130725';
 
PS:將會在HDFS的/user/trade02 目錄下保存導出來的數據(這裏生成的文件名是000000_0),數據中的列與列之間的分隔符是^A(ascii碼是\00001)。
和導出文件到本地文件系統的HQL少一個local,數據的存放路徑就不一樣了。
 
(3、導出到Hive的另一個表中:     
     insert into table trade_test partition(trade_date='20130724') select tradedate,tradetime,securityid,bidpx1,bidsize1,offerpx1,offersize1 from trade where tradedate='20130724';

     select tradedate,tradetime,securityid,bidpx1,offerpx1 from trade_test where tradedate='20130724';
PS:前提是trade_test已經存在。

(4、導出後續補充學習:
在hive0.11.0版本後新引進了一個新的特性,也就是當用戶將hive查詢結果輸出到文件,用戶可以指定使用的列的分隔符,而在之前的版本中是不能指定列之間的分隔符的。
例如:
insert overwrite local directory '/home/suh/hive/trade_01' row format delimited  fields terminated by '\t' select * from trade;  

還可以用hive的-e和-f參數來導出數據,其中-e表示後面直接帶雙引號的sql語句;而-f是接一個文件,文件的內容爲一個sql語句。如下所示:
執行:./hive -e "select * from trade" >> /home/suh/hive/trade001.txt 

執行:./hive -f /home/suh/hive/SQL.sql >> /home/suh/hive/trade002.txt
 
 
三、實際業務案例操作:
(1、創建交易數據表及臨時表:
create table trade(tradedate string,tradetime string,securityid string,bidpx1 double,bidsize1 string,offerpx1 double,offersize1 string) partitioned by(trade_date string) row format delimited fields terminated by ',';

create table trade_tmp(tradedate string,tradetime string,securityid string,bidpx1 double,bidsize1 string,offerpx1 double,offersize1 string) row format delimited fields terminated by ',';



(2、導入交易數據集文件total.csv到Hive中,用日期做爲分區表的分區ID:
    由於交易記錄文件total.csv裏的數據是多個日期的記錄,所以先導入到臨時表trade_tmp,然後再從臨時表中導入到正式的trade 分區表中 
導入到臨時表trade_tmp:
    load data local inpath '/home/suh/hive/total.csv' overwrite into table trade_tmp;

從臨時表中導入到正式的trade 分區表:
insert into table trade partition(trade_date='20130724') select tradedate,tradetime,securityid,bidpx1,bidsize1,offerpx1,offersize1 from trade_tmp where tradedate='20130724';
insert into table trade partition(trade_date='20130725') select tradedate,tradetime,securityid,bidpx1,bidsize1,offerpx1,offersize1 from trade_tmp where tradedate='20130725';
insert into table trade partition(trade_date='20130726') select tradedate,tradetime,securityid,bidpx1,bidsize1,offerpx1,offersize1 from trade_tmp where tradedate='20130726';



(3、按securityid分組,分別統計每個產品每日的最高價和最低價:
select tradedate,securityid,max(bidpx1),min(bidpx1),max(offerpx1),min(offerpx1) from trade group by tradedate , securityid;



(4、按securityid分組,以分鐘做爲最小單位,求204001的任意1日的每分鐘均價:
select tradedate,securityid,AVG(bidpx1),AVG(offerpx1) from trade where securityid='204001' group by tradedate,securityid;
發佈了103 篇原創文章 · 獲贊 14 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章