Hive服務(wù)
Hive外殼環(huán)境是可以使用hive命令來運(yùn)行的一項(xiàng)服務(wù)。可以在運(yùn)行時(shí)使用-
service選項(xiàng)指明要使用哪種服務(wù)。鍵入hive-servicehelp可以獲得可用服務(wù)
列表。下面介紹最有用的一些服務(wù)。
cli
???Hive的命令行接口(外殼環(huán)境)。這是默認(rèn)的服務(wù)。
hiveserver
??? 讓Hive以提供Trift服務(wù)的服務(wù)器形式運(yùn)行,允許用不同語言編寫的客戶端進(jìn)
??? 行訪問。使用Thrift,? JDBC和ODBC連接器的客戶端需要運(yùn)行Hive服務(wù)器來
??? 和Hive進(jìn)行通信。通過設(shè)置HIVE_ PORT環(huán)境變量來指明服務(wù)器所監(jiān)聽的端口
??? 號(默認(rèn)為10 000).
hwi
Hive的Web接口。參見第372頁的補(bǔ)充內(nèi)容“HiveWeb Interface"。
(hive –service hwi)啟動(dòng)web服務(wù)后通過訪問http://ip:9999/hwi
jar
??? 與hadoopjar等價(jià)的Hive的接口。這是運(yùn)行類路徑中同時(shí)包含Hadoop和
???Hive類的Java應(yīng)用程序的簡便方法。
metastore
??? 默認(rèn)情況下,metastore和Hive服務(wù)運(yùn)行在同一個(gè)進(jìn)程里。使用這個(gè)服務(wù),可
??? 以讓metastore作為一個(gè)單獨(dú)的(遠(yuǎn)程)進(jìn)程運(yùn)行。通過設(shè)置METASTORE_PORT
環(huán)境變量可以指定服務(wù)器監(jiān)聽的端口號。
?
?
?
?
?
Hive客戶端
啟動(dòng)(hive --service hiveserver &)hive遠(yuǎn)程訪問服務(wù)
會(huì)提示Starting Hive Thrift Server 。
這個(gè)時(shí)候就可以通過thrift 客戶端,jdbc驅(qū)動(dòng),odbc驅(qū)動(dòng)去訪問和操作了。
?
Metastore
?
metastore是Hive元數(shù)據(jù)的集中存放地。metastore包括兩部分:服務(wù)和后臺(tái)數(shù)據(jù)的存儲(chǔ)。
?
默認(rèn)derby數(shù)據(jù),不過只能單機(jī)訪問。
一般都放在遠(yuǎn)程數(shù)據(jù)庫,hive和元數(shù)據(jù)數(shù)據(jù)庫分開放。比如mysql直接配置上mysql參數(shù)即可。參考安裝部分。
?
?
?
?
?
HiveQL
?
Hive查詢的和數(shù)據(jù)處理的語言,內(nèi)部會(huì)解析成對應(yīng)的操作或者mapreduce程序等處理。
?
數(shù)據(jù)類型
基本數(shù)據(jù)類型
TINYINT: 1個(gè)字節(jié)
SMALLINT: 2個(gè)字節(jié)
INT: 4個(gè)字節(jié)??
BIGINT: 8個(gè)字節(jié)
BOOLEAN: TRUE/FALSE?
FLOAT: 4個(gè)字節(jié),單精度浮點(diǎn)型
DOUBLE: 8個(gè)字節(jié),雙精度浮點(diǎn)型
STRING??????字符串
復(fù)雜數(shù)據(jù)類型
ARRAY: 有序字段
MAP: 無序字段
STRUCT: 一組命名的字段
?
?
數(shù)據(jù)轉(zhuǎn)換
Hive中數(shù)據(jù)部分可以通行的范圍是允許隱身轉(zhuǎn)換的。
個(gè)人處理數(shù)據(jù)要顯示指定轉(zhuǎn)化的話可以調(diào)用cast函數(shù)比如:cast(‘1’ as int)
當(dāng)然如果說處理的數(shù)據(jù)屬于非法的話,比如cast(‘x’ as int) 會(huì)直接返回null
?
?
表
Hive表格邏輯上由存儲(chǔ)的數(shù)據(jù)和描述表格中數(shù)據(jù)形式的相關(guān)元數(shù)據(jù)組成。
Hive表中存在兩種形式一個(gè)是在自己倉庫目錄(托管表),另一種是hdfs倉庫目錄以外的(外部表)。對于托管表基本上是load和drop的時(shí)候直接對數(shù)據(jù)和元數(shù)據(jù)都操作。但是外部表卻是基本只對元數(shù)據(jù)操作。
?
創(chuàng)建普通表語句
?
create table records (yearstring,temperature int,quality int) row format delimited fields terminated by'\t'
?
?
創(chuàng)建外部表語句
?
外部表數(shù)據(jù)位置
?
[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
?
?
創(chuàng)建表指定外部表數(shù)據(jù)位置&查看數(shù)據(jù)
?
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
分區(qū)和桶
?
?
?
分區(qū)表是hive中一種存放表但是可以根據(jù)個(gè)別列來分別存放的形式的表結(jié)構(gòu)。區(qū)別于普通表的時(shí)候要指定分區(qū)的列,而且數(shù)據(jù)中是不存在分區(qū)列的,而且不能存在。
一個(gè)分區(qū)表表中有可以多個(gè)維度分區(qū)。
?
?
創(chuàng)建分區(qū)表語句
?
create table tb_test (yearstring,temperature int,quality int) partitioned by (ds string,ds2 string) row format delimited fieldsterminated by '\t';
?
?
查看分區(qū)
?
show partitions tb_test;
?
?
?
加載數(shù)據(jù)到指定分區(qū)表
?
load data local inpath'/root/hcr/tmp/sample.txt' into table tb_test partition(ds='2013-12-06',ds2='shanghai')
?
?
根據(jù)分區(qū)條件查詢
?
?
select * from tb_test where ds='2013-12-06';
?
?
創(chuàng)建桶語句
?
create table tb_test_bucket(yearstring,temperature int,quality int) clustered by(temperature) into 3 buckets row format delimited fields terminated by '\t';
?
?
加載數(shù)據(jù)到桶中
?
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
?
?
查看數(shù)據(jù)取樣測試
?
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
?
導(dǎo)入數(shù)據(jù)
Insert overwrite table
在插入數(shù)據(jù)的時(shí)候是強(qiáng)制替換的overwrite
?
動(dòng)態(tài)分區(qū)使用(從一個(gè)表中的分區(qū)中取數(shù)據(jù)放到另一個(gè)目標(biāo)分區(qū)表中,分區(qū)是在查詢表已經(jīng)存在的。)
?
設(shè)定環(huán)境
?
?
set hive.exec.dynamic.partition=true;sethive.exec.dynamic.partition.mode=nonstrict;
?
?
目標(biāo)分區(qū)表
?
create table tb_test_pt (yearstring,temperature int,quality int) partitioned by (ds string) row format delimited fields terminated by'\t';
?
?
動(dòng)態(tài)分區(qū)取數(shù)插入
?
insert overwrite table tb_test_pt partition(ds) select year,temperature,quality,ds from tb_test;
?
?
多表導(dǎo)入
?
在hive中是支持如下語法
?
?
from sourceTableinsert overwrite table targetTableselect col1,col2
?
源表數(shù)據(jù)
?
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
?
創(chuàng)建三個(gè)目標(biāo)表
?
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';
?
插入多表執(zhí)行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;
操作結(jié)果
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 查詢的數(shù)據(jù)直接放到一個(gè)新表中。(因?yàn)槭窃有圆僮鳎瑂o如果查詢失敗,那么創(chuàng)建也是失敗)
?
操作實(shí)例
?
create table tb_records_ctasasselect year,temperature from tb_test;
?
?
數(shù)據(jù)導(dǎo)出
?
導(dǎo)出到本地目錄
?
insert overwrite local directory'/root/hcr/tmp/ex_abc2.txt' select * from m_t2;
?
導(dǎo)出到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;
?
?
等等具體還有好多修改表信息的操作
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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