hive 导出json格式 文件_Hive 系列 之 基本操作合集
下面是本課程概覽:
(1)hive系列之簡(jiǎn)介,安裝,beeline和hiveserver2
(2)hive系列之基本操作
(3)hive系列之udf
(4)hive系列之二級(jí)分區(qū)和動(dòng)態(tài)分區(qū)
(5)hive系列之分桶表
(6)hive系列之常用函數(shù)
(7)hive系列之系統(tǒng)講解開(kāi)窗函數(shù)
(8)hive系列之存儲(chǔ)格式及常用壓縮格式
(9)hive系列之?dāng)?shù)據(jù)倉(cāng)庫(kù)建模理論
(10)hive系列之?dāng)?shù)據(jù)倉(cāng)庫(kù)建模-維度表和事實(shí)表
(11)hive系列之?dāng)?shù)據(jù)倉(cāng)庫(kù)建模-退化維度和緩慢變化維
(12)hive系列之常用企業(yè)性能優(yōu)化1
(13)hive系列之常用企業(yè)性能優(yōu)化2
(14)hive系列之常用企業(yè)性能優(yōu)化3
今天是第二講,Hive 的基本操作
今天的內(nèi)容比較多,也比較枯燥,盡可能對(duì)著操作一遍,加深認(rèn)識(shí)
01
數(shù)據(jù)庫(kù)相關(guān)
1.創(chuàng)建數(shù)據(jù)庫(kù)
create database db_hive;
create database if not exists db_hive;
2.創(chuàng)建數(shù)據(jù)庫(kù)到指定目錄下
create database if not exists db_hive_03 location '/user/wangkai/hive/warehouse/db_hive03.db';
3.顯示當(dāng)前所有的數(shù)據(jù)庫(kù)
show databases;
4.查看數(shù)據(jù)庫(kù)描述
desc database db_hive;
5.刪除數(shù)據(jù)庫(kù)就不說(shuō)了,很危險(xiǎn)
02
數(shù)據(jù)類型
1、基本類型
這里著重說(shuō)一下 TIMESTAMP
TIMESTAMP 類型的主要作用是在數(shù)據(jù)比較的效率上比較高
TIMESTAMP 類型 ,其對(duì)應(yīng)的格式為:yyyy-MM-dd HH:MM:SS,從文件中導(dǎo)入時(shí),必須滿足這個(gè)格式的才能導(dǎo)入,否則顯示為null
比如現(xiàn)在建一個(gè)表
create table dw.t_date (
t1 date,
t2 timestamp
)
comment '日期測(cè)試表'
row format delimited
fields terminated by ',';
創(chuàng)建文件提交到 hdfs 上
文件內(nèi)容:
2019-09-01,2019-09-01 11:12:00
2019-08-01,2019-09-01 12:00
加載到表中
LOAD DATA INPATH '/tmp/datetest/date.txt' into TABLE dw.t_date;
最終t2不滿足格式,所以顯示了null
Hive 中比較常用的時(shí)間相關(guān)的udf :
unix_timestamp() 返回當(dāng)前的時(shí)間戳
unix_timestamp('2019-09-01 11:10:10') 返回指定日期的時(shí)間戳
from_unixtime(1567307471) 返回 yyyy-MM-dd HH:MM:SS 格式的字符串
2、復(fù)雜類型
ARRAY、Map、struct、union,這些復(fù)雜類型是由基礎(chǔ)類型構(gòu)成的
(1) Array
表示數(shù)組,是具有相同類型的變量的集合,這些變量稱為數(shù)組的元素,每個(gè)元素都有下標(biāo),從0開(kāi)始
如下,我們建一張 person 表,名字、工作地點(diǎn)、愛(ài)好
create table dw.person(
name string,
work_locations array<string>,
other_info map<string,string>,
other_info2 struct<one:string,two:int>
)
row format delimited
fields terminated by ','
collection items terminated by '|'
map keys terminated by ':';
字段的分隔符是 ,
集合(數(shù)組和map)元素的分隔符是 |
map或者struct 的 鍵值對(duì)分隔符是 :
測(cè)試數(shù)據(jù)如下:
導(dǎo)入到表中
查詢第一個(gè)工作地點(diǎn):
select name,work_locations[0] from dw.person;
(2) Map
是一組鍵值對(duì)元組集合,使用數(shù)組表示法,map['first'] 可以獲取值
比如上面的表中,查詢性別
select name,other_info['sex'] from dw.person;
(3)Struct
是不同類型元素的集合,可以用 點(diǎn) 描述符來(lái)取元素
select name,other_info2.one from dw.person;
03
內(nèi)部表、外部表、分區(qū)表
1、內(nèi)部表和外部表的區(qū)別
(1)未被 external 修飾的是內(nèi)部表,被 external 關(guān)鍵字修飾的是外部表
(2)內(nèi)部表由 Hive 自身管理,外部表由 HDFS 管理(也就是數(shù)據(jù)在別的目錄下,但元數(shù)據(jù)還是由 Hive 管理的)
(3)內(nèi)部表數(shù)據(jù)的存儲(chǔ)位置是:hive.metastore.warehouse.dir(默認(rèn)是:/user/hive/warehouse),外部表數(shù)據(jù)的存儲(chǔ)位置由自己制定
(4)刪除內(nèi)部表會(huì)直接刪除元數(shù)據(jù)和存儲(chǔ)在表下面的數(shù)據(jù),而刪除外部表只會(huì)刪除元數(shù)據(jù),HDFS 上的數(shù)據(jù)并不會(huì)被刪除
2、分區(qū)表
為了對(duì)表進(jìn)行合理的管理,以及提高查詢的效率,Hive 可以將表組織成分區(qū),一個(gè)分區(qū)實(shí)際上就是一個(gè)目錄,一個(gè)表可以在多個(gè)維度上創(chuàng)建分區(qū),分區(qū)之間的關(guān)系,就是目錄樹(shù)的關(guān)系。
就是在系統(tǒng)上創(chuàng)建文件夾,把分類數(shù)據(jù)放在不同的文件夾下,加快查詢速度。
比如創(chuàng)建日期和公司兩個(gè)分區(qū),那么就可以指定查詢某個(gè)月某個(gè)公司的數(shù)據(jù),而不同全表掃描
3、實(shí)戰(zhàn)
(1)內(nèi)部表
上面建的dw.person 就是內(nèi)部表,hive 會(huì)在 hdfs 文件上創(chuàng)建一個(gè)目錄,刪除表的時(shí)候,目錄下的數(shù)據(jù)也會(huì)被刪除
(2)外部表
使用關(guān)鍵字 external
create external table dw.t_test2 (
c1 string,
c2 string
)
如果沒(méi)有指定位置,hive 會(huì)生成一個(gè)目錄,如果指定了位置,那么不會(huì)創(chuàng)建目錄
create external table dw.t_test3 (
c1 string,
c2 string
)
location '/tmp/person';
刪除表,并不會(huì)刪除 hdfs 文件的
(3)分區(qū)表
create table dw.t_test4 (
c1 string,
c2 string
)
partitioned by (appId string)
新增分區(qū)
alter table dw.t_test4 add partition(appId='app');
會(huì)在hdfs上新增一個(gè)目錄
(4)外部分區(qū)表
顧名思義,即是外部表,又是分區(qū)表
create external table dw.t_test5 (
c1 string,
c2 string
)
partitioned by (appId string);
給外部分區(qū)表添加記錄
alter table dw.t_test4 add partition(appId='app') location '/tmp/test1';
04
其他建表方式
1.用查詢出來(lái)的數(shù)據(jù)建表
create table IF NOT EXISTS dw.log_20170629_2
AS select ip,user from dw.log_20170629;
2.僅僅創(chuàng)建已有表的表結(jié)構(gòu)
create table IF NOT EXISTS dw.log_20170630
like dw.log_20170629;
05
加載和導(dǎo)出數(shù)據(jù)
1.加載數(shù)據(jù)
(1)這個(gè)命令是從本地拷貝文件到集群上,如果文件存在,則會(huì)重命名新增一份文件
load data local inpath '/opt/datas/student.txt' into table dw.student;
(2)加載數(shù)據(jù)并覆蓋原有數(shù)據(jù),會(huì)刪除原有文件,再拷貝
load data local inpath '/opt/datas/student.txt' overwrite into table dw.student;
(3)加載數(shù)據(jù)到分區(qū)表
load data local inpath '/home/hadoop/data/emp.txt' into table dw.emp_partition partition (month='201509');
(4)加載hdfs數(shù)據(jù),從hdfs移動(dòng)數(shù)據(jù)到集群上
load data inpath '/opt/datas/student.txt' into table dw.student;
2.導(dǎo)出數(shù)據(jù)
(1)導(dǎo)出文件到本地目錄
insert overwrite local directory '/home/hadoop/data/hive_exp_emp2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY 't'
select * from default.emp;
(2)hive命令
bin/hive -e "select * from default.emp;" > /opt/datas/exp_res.txt
(3)導(dǎo)出到hdfs中
insert overwrite directory '/user/wangkai/hive/hive_exp_emp'
ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' COLLECTION ITEMS TERMINATED BY 'n'
下一篇是 hive 的 udf ,持續(xù)關(guān)注 kk大數(shù)據(jù) 噢
總結(jié)
以上是生活随笔為你收集整理的hive 导出json格式 文件_Hive 系列 之 基本操作合集的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 苹果笔记本进不识别u盘启动不了怎么办 苹
- 下一篇: 工作汇报ppt案例_【赠书】开工大吉!今