標籤:hadoop hive 資料操作
hive資料操作
hive命令列操作
hive -d --define <key=value> 定義一個key-value可以在命令列中使用
hive -d database <databasename> 指定使用的資料庫
hive -e “hql” 不需要進入cli執行hql語句,可以在指令碼中使用
hive -f fileName 將hql放到一個file檔案中執行,sql語句來自file檔案
hive -h hostname 訪問主機,通過主機的地址
hive -H --help 列印協助資訊
hive -H --hiveconf <property=value> 使用的資訊可以在這裡定義property的value
hive -H --hivevar <key-value> 使用可變的命令,將一個命令重新賦值使用
hive -i filename hive的初始設定檔案,可以將hive的一些初始化資訊放到這個檔案中,比如使用自訂函數的時候可以將相應的jar包目錄寫進去
hive -S --silent 在shell下進入安靜模式,不需要列印一些輸出資訊
hive -v --verbose 列印執行的詳細資料,比如執行的SQL語句
執行個體:查詢test表,並且將列印的結果放到/home/data/select_result.txt中
hive -S -e "select * from test" > /home/data/select_result.txt
含有執行的SQL語句
hive -v -e "select * from test" > /home/data/select_result.txt
執行放在檔案中的SQL
hive -f /home/colin/hive-1.2.1/select_test
hive cli中使用list查看分布式緩衝中的file|jar|archive(比如通過add jar添加進去的,可以通過list jar查看添加到分布式緩衝中的jar包)
hive cli中使用source執行指定目錄下的檔案,比如執行指定目錄下的一個sql檔案 source /home/colin/hive-1.2.1/select_test
hive操作變數
組態變數
set val_Name=val_Value;
${hiveconf:val}
查看linux下的環境變數
${env:變數名稱},env查看所有的環境變數
執行個體定義變數val_test,設定為yang,作為查詢語句條件
set val_test=yang;
select * from test2 where name=‘${hiveconf:val_test}‘;
查看
HIVE_HOME的環境變數
select ‘${env:HIVE_HOME}‘ from test;
註:test表中有多少條記錄,列印多少次路徑
hive資料載入
內表資料載入
建立表時載入
create table tableName as select col_1,col_2... from tableName2;
建立表的時候指定資料位元置
create table tablename(col_name typye comment ...) location ‘path‘;(path為hdfs中的路徑,注意這個path是檔案上層的目錄,也就是說指定檔案到上層目錄,目錄下的資料都會作為該表的資料。並且這種方式不會在hive/warehouse下建立該表的目錄,因為他會把hdfs中指定的path作為該表目錄操作 )
註:這種指定方式,在內表中會將資料的擁有權給當前表,當表刪除的時候資料也會刪除(連同上層目錄)
本機資料載入
load data local inpath ‘localpath‘ [overwrite] into table tableName;
載入HDFS中資料
load data inpath ‘hdfspath‘ [overwrite] into table tableName;
注意:這種方式,是將hdfs中指定位置的資料移動到表的目錄下
使用Hadoop命令拷貝資料到指定位置(hive中shell執行和Linux中shell執行)
hdfs dfs -copyFromLocal /home/data /data
hive shell中 dfs -copyFromLocal /home/data data(hadoop命令直接可以在hive中執行,同樣hive也可以執行linux命令,但是需要在命令前加上!)
由查詢語句載入資料
insert [overwrite|into] table tableName select col1,col2... from tablenName2 where ...
from tableNable2 insert [overwrite|into] table tableName select col1,col2... where ...
select col1,col2.. from tableName2 where ... insert [overwrite|into] table tableName;
註:可以select的欄位名字可以和table中不同,hive在資料載入時候不會進列欄位檢測和類型檢測。只有在查詢的時候檢測
外表資料載入
建立表的時候指定資料位元置(因為外表對資料沒有控制權)
create external table (col_Name type comment...) location ‘path‘;
通過insert語句,和內表一樣
通過hadoop命令,和內表一樣
hive分區表資料載入
內部分區表和內表資料載入類似
外部分區表和外表資料載入類似
不同之處是指定分區;在外部分區表中資料存放的層次要表的分區一致,如果分區表下沒有新增分區,即使目錄下有資料也是查不到的,當滿足目錄結構對應的時候需要添加分區 alter table tableName add partition (dt=20150820)。
load data local inpath ‘path‘ [overwrie] into table tableNmme partition(pName=‘..‘);
insert [overwrite|into] table tableName partition(pName=‘..‘) select col1,col2.. from tableName2 where ...
注意:row format分隔字元如果設定多個字元起分割作用,只有第一個字元有作用
load資料的時候,欄位類型不能相互轉換,否則會載入為NULL
插入資料時候如果selct後的類型也不能相互轉換,否則插入為NULL;
在HDFS中NULL是以\N來顯示的
Hive資料匯出
匯出方式:
Hadoop命令的方式
get
text
通過Insert....DIRECTOR
insert overwrite [local] directory ‘path‘ [row format delimited fields terminated by ‘\t‘ lines terminated by ‘\n‘] select col1,col2.. from tableName
註:如果使用local是導到本地,否則是HDFS中,row format只對導到本地起作用(在1.2.1hive中已經能夠在HDFS中使用row format了
)。
通過Shell命令加管道
通過第三方工具
執行個體:
hdfs dfs -get /user/hive/warehouse/test4/* ./data/newdata
hdfs dfs -text /user/hive/warehouse/test5/* > ./data/newdata(可以對多種格式進行輸出,壓縮、序列化等)
hive -S -e "select * from test4" | grep yang > ./data/newdata
hive動態分區
分區不確定,需要從查詢結果中查看。不需要為每個分區都使用alter table添加
使用動態分區需要配置的參數:
set hive.exec.dynamic.partition=true;//使用動態分區
set hive.exec.dynamic.partition.model=nonstrick;//分區有兩種方式:一種是strick有限制分區,需要有一個靜態分區,且放在最前面。一種就是nostrick不受限制模式
set hive.exec.max.dynamic.partitions.pernode=10000;//每個節點產生動態分區的最大個數
set hive.exec.max.dynamic.partitions=100000;//產生動態分區的最大個數
set hive.exec.max.created.fiels=150000;//一個任務最多可以建立的檔案數目
set dfs.datanode.max.xcievers=8192;//限定一次最多開啟的檔案數
insert overwrie table test7 partition(dt) select name,time as dt from test6;
表屬性操作
修改表名:
alter table tableName rename to newName
修改列明:
alter table tableName change column old_col new_col newType comment ‘....‘ after colName(如果要為第一列則將aftercolName 改為first)
增加列:
alter table tableName add columns (c1 type comment ‘..‘,c2 type comment ‘...‘)
修改表屬性
查看錶屬性
desc formatted tablename
這個是可以要修改的表的屬性資訊
Table Parameters:
COLUMN_STATS_ACCURATE false
last_modified_by colin
last_modified_time 1440154819
numFiles 0
numRows -1
rawDataSize -1
totalSize 0
transient_lastDdlTime 1440154819
修改屬性:
alter table tableName set tblproerties(‘propertiesName‘=‘.....‘);
比如修改comment
alter table tableName set tblproperties("comment"="xxxxx");
修改序列化資訊:
無分區表
alter table tableName set serdepropertie(‘fields.delim‘=‘\t‘);
有分區表
alter table tableName partition(dt=‘xxxx‘) set serdeproperties(‘fields.delim‘=‘\t‘);
修改Location:
alter table tableName [partition(..)] set localtion ‘path‘;
內部表外部表格轉換:
alter table tableName set tblproperties (‘EXTERNAL‘=‘TRUE|FALSE‘);必須大寫EXTERNAL
更多屬性操作查看:https://cwiki.apache.org/confluence/display/Hive/Home
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
hive學習筆記-資料操作