Hive服務
Hive外殼環境是可以使用hive命令來運行的一項服務。可以在運行時使用-
service選項指明要使用哪種服務。鍵入hive-servicehelp可以獲得可用服務
列表。下面介紹最有用的一些服務。
cli
???Hive的命令行接口(外殼環境)。這是默認的服務。
hiveserver
??? 讓Hive以提供Trift服務的服務器形式運行,允許用不同語言編寫的客戶端進
??? 行訪問。使用Thrift,? JDBC和ODBC連接器的客戶端需要運行Hive服務器來
??? 和Hive進行通信。通過設置HIVE_ PORT環境變量來指明服務器所監聽的端口
??? 號(默認為10 000).
hwi
Hive的Web接口。參見第372頁的補充內容“HiveWeb Interface"。
(hive –service hwi)啟動web服務后通過訪問http://ip:9999/hwi
jar
??? 與hadoopjar等價的Hive的接口。這是運行類路徑中同時包含Hadoop和
???Hive類的Java應用程序的簡便方法。
metastore
??? 默認情況下,metastore和Hive服務運行在同一個進程里。使用這個服務,可
??? 以讓metastore作為一個單獨的(遠程)進程運行。通過設置METASTORE_PORT
環境變量可以指定服務器監聽的端口號。
?
?
?
?
?
Hive客戶端
啟動(hive --service hiveserver &)hive遠程訪問服務
會提示Starting Hive Thrift Server 。
這個時候就可以通過thrift 客戶端,jdbc驅動,odbc驅動去訪問和操作了。
?
Metastore
?
metastore是Hive元數據的集中存放地。metastore包括兩部分:服務和后臺數據的存儲。
?
默認derby數據,不過只能單機訪問。
一般都放在遠程數據庫,hive和元數據數據庫分開放。比如mysql直接配置上mysql參數即可。參考安裝部分。
?
?
?
?
?
HiveQL
?
Hive查詢的和數據處理的語言,內部會解析成對應的操作或者mapreduce程序等處理。
?
數據類型
基本數據類型
TINYINT: 1個字節
SMALLINT: 2個字節
INT: 4個字節??
BIGINT: 8個字節
BOOLEAN: TRUE/FALSE?
FLOAT: 4個字節,單精度浮點型
DOUBLE: 8個字節,雙精度浮點型
STRING??????字符串
復雜數據類型
ARRAY: 有序字段
MAP: 無序字段
STRUCT: 一組命名的字段
?
?
數據轉換
Hive中數據部分可以通行的范圍是允許隱身轉換的。
個人處理數據要顯示指定轉化的話可以調用cast函數比如:cast(‘1’ as int)
當然如果說處理的數據屬于非法的話,比如cast(‘x’ as int) 會直接返回null
?
?
表
Hive表格邏輯上由存儲的數據和描述表格中數據形式的相關元數據組成。
Hive表中存在兩種形式一個是在自己倉庫目錄(托管表),另一種是hdfs倉庫目錄以外的(外部表)。對于托管表基本上是load和drop的時候直接對數據和元數據都操作。但是外部表卻是基本只對元數據操作。
?
創建普通表語句
?
create table records (yearstring,temperature int,quality int) row format delimited fields terminated by'\t'
?
?
創建外部表語句
?
外部表數據位置
?
[root@ebsdi-23260-oozie tmp]# hadoop fs-put sample.txt /user/houchangren/tmp/location[root@ebsdi-23260-oozie tmp]# hadoop fs-mkdir /user/houchangren/tmp/location[root@ebsdi-23260-oozie tmp]# hadoop fs-put sample.txt /user/houchangren/tmp/location[root@ebsdi-23260-oozie tmp]# hadoop fs-cat /user/houchangren/tmp/location/sample.txt1990 44 11991 45 21992 41 31993 43 21994 41 1
?
?
創建表指定外部表數據位置&查看數據
?
hive> create external tabletb_ext_records(year string,temperature int,quality int) row format delimitedfields terminated by '\t' location '/user/houchangren/tmp/location/';OKTime taken: 0.133 secondshive> select * from tb_ext_records;OK1990 44 11991 45 21992 41 31993 43 21994 41 1Time taken: 0.107 seconds
分區和桶
?
?
?
分區表是hive中一種存放表但是可以根據個別列來分別存放的形式的表結構。區別于普通表的時候要指定分區的列,而且數據中是不存在分區列的,而且不能存在。
一個分區表表中有可以多個維度分區。
?
?
創建分區表語句
?
create table tb_test (yearstring,temperature int,quality int) partitioned by (ds string,ds2 string) row format delimited fieldsterminated by '\t';
?
?
查看分區
?
show partitions tb_test;
?
?
?
加載數據到指定分區表
?
load data local inpath'/root/hcr/tmp/sample.txt' into table tb_test partition(ds='2013-12-06',ds2='shanghai')
?
?
根據分區條件查詢
?
?
select * from tb_test where ds='2013-12-06';
?
?
創建桶語句
?
create table tb_test_bucket(yearstring,temperature int,quality int) clustered by(temperature) into 3 buckets row format delimited fields terminated by '\t';
?
?
加載數據到桶中
?
insert overwrite table tb_test_bucket select * from records;
?
?
查看hdfs文件
?
hive> dfs -ls/user/hive/warehouse/tb_test_bucket;Found 3 items-rw-r--r-- 2 root supergroup 202013-12-09 11:36 /user/hive/warehouse/tb_test_bucket/000000_0-rw-r--r-- 2 root supergroup 202013-12-09 11:36 /user/hive/warehouse/tb_test_bucket/000001_0-rw-r--r-- 2 root supergroup 60 2013-12-0911:36 /user/hive/warehouse/tb_test_bucket/000002_0
?
?
查看數據取樣測試
?
select * from tb_test_bucket table sample(bucket 1 out of 2 on temperature);
?
?
?
?
hive> select * from tb_test_bucket tablesample(bucket 1 out of 2 on temperature);Total MapReduce jobs = 1Launching Job 1 out of 1Number of reduce tasks is set to 0 since there's no reduce operatorStarting Job = job_201311101215_51576, Tracking URL = http://hadoop-master.TB.com:50030/jobdetails.jsp?jobid=job_201311101215_51576Kill Command = /usr/lib/hadoop-0.20/bin/hadoop job -Dmapred.job.tracker=hadoop-master.TB.com:8021 -kill job_201311101215_51576Hadoop job information for Stage-1: number of mappers: 3; number of reducers: 02013-12-09 11:36:48,415 Stage-1 map = 0%, reduce = 0%2013-12-09 11:36:50,449 Stage-1 map = 33%, reduce = 0%, Cumulative CPU 2.81 sec2013-12-09 11:36:51,463 Stage-1 map = 67%, reduce = 0%, Cumulative CPU 2.81 sec2013-12-09 11:36:52,475 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 4.39 sec2013-12-09 11:36:53,489 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 4.39 sec2013-12-09 11:36:54,504 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 4.39 secMapReduce Total cumulative CPU time: 4 seconds 390 msecEnded Job = job_201311101215_51576MapReduce Jobs Launched:Job 0: Map: 3 Accumulative CPU: 4.39 sec HDFS Read: 802 HDFS Write: 20 SUCESSTotal MapReduce CPU Time Spent: 4 seconds 390 msecOK1990 44 11990 44 1Time taken: 11.094 seconds
?
導入數據
Insert overwrite table
在插入數據的時候是強制替換的overwrite
?
動態分區使用(從一個表中的分區中取數據放到另一個目標分區表中,分區是在查詢表已經存在的。)
?
設定環境
?
?
set hive.exec.dynamic.partition=true;sethive.exec.dynamic.partition.mode=nonstrict;
?
?
目標分區表
?
create table tb_test_pt (yearstring,temperature int,quality int) partitioned by (ds string) row format delimited fields terminated by'\t';
?
?
動態分區取數插入
?
insert overwrite table tb_test_pt partition(ds) select year,temperature,quality,ds from tb_test;
?
?
多表導入
?
在hive中是支持如下語法
?
?
from sourceTableinsert overwrite table targetTableselect col1,col2
?
源表數據
?
hive> select * from tb_test;OK1990 44 1 2013-12-06 shandong1991 45 2 2013-12-06 shandong1992 41 3 2013-12-06 shandong1993 43 2 2013-12-06 shandong1994 41 1 2013-12-06 shandong1990 44 1 2013-12-06 shanghai1991 45 2 2013-12-06 shanghai1992 41 3 2013-12-06 shanghai1993 43 2 2013-12-06 shanghai1994 41 1 2013-12-06 shanghai
?
創建三個目標表
?
create table tb_records_by_year (year string,count int) row format delimited fields terminated by '\t';create table tb_stations_by_year (year string,count int) row format delimited fields terminated by '\t';create table tb_good_records_by_year (year string,count int) row format delimited fields terminated by '\t';
?
插入多表執行sql
?
?
from tb_testinsert overwrite table tb_stations_by_yearselect year,count(distinct temperature)group by yearinsert overwrite table tb_records_by_yearselect year,count(1)group by yearinsert overwrite table tb_good_records_by_yearselect year,count(1)where temperature!=9999 and (quality =0 or quality=1 or quality=3)group by year;
操作結果
hive> select * from tb_records_by_year;OK1990 21991 21992 21993 21994 2Time taken: 0.088 secondshive> select * from tb_stations_by_year;OK1990 11991 11992 11993 11994 1Time taken: 0.081 secondshive> select * from tb_good_records_by_year;OK1990 21992 21994 2Time taken: 0.085 seconds
?
?
?
?
Create Table … As? Select (CTAS)
把 hive 查詢的數據直接放到一個新表中。(因為是原子性操作,so如果查詢失敗,那么創建也是失敗)
?
操作實例
?
create table tb_records_ctasasselect year,temperature from tb_test;
?
?
數據導出
?
導出到本地目錄
?
insert overwrite local directory'/root/hcr/tmp/ex_abc2.txt' select * from m_t2;
?
導出到hdfs目錄
?
insert overwrite directory'/user/houchangren/tmp/m_t2' select * from m_t2;
?
?
表的修改Alter table
?
修改表名rename to
?
alter table tb_records_ctas rename totb_records_2
?
增加新列
?
alter table tb_records_2 add columns(new_col int);
?
?
修改某一列的信息
?
ALTER TABLE tb_records_2 CHANGE COLUMN new_col col1 string;
?
?
等等具體還有好多修改表信息的操作
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
