參考鏈接:
學(xué)習(xí)Hadoop第30課
學(xué)習(xí)Hadoop第31課
- 查看數(shù)據(jù)庫(kù):show databases;建立數(shù)據(jù)庫(kù):create database XXXX;
- 查看表:show tables;
- 查看建表語(yǔ)句:
create table student(id int, name string);
show create table student;(student 是表名字)
location顯示了表的位置:‘hdfs://localhost:9000/user/hive/warehouse/student’,student是一個(gè)文件夾,此時(shí)里面是空的,之后向該表插入數(shù)據(jù)就是降文件放到這個(gè)文件夾下
4.加載數(shù)據(jù)
在本地建立文件student.txt,輸入幾行內(nèi)容,用空格分割,并保存
1 zhangsan
2 lisi
3 wangwu
在hive中加載數(shù)據(jù):
hive> load data local inpath '/Users/chenghengchao/student.txt' into table student;
Loading data to table default.student
OK
Time taken: 0.227 seconds
5.查詢數(shù)據(jù)
hive> select * from student;
OK
NULL NULL
NULL NULL
NULL NULL
Time taken: 0.139 seconds, Fetched: 3 row(s)
從結(jié)果來看,有3行兩列記錄,但是全部都是null。這是因?yàn)閯?chuàng)建表的時(shí)候沒有指定分隔符。如果查詢行數(shù),結(jié)果會(huì)是3:
hive> select count(*) from student;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = chenghengchao_20170929100451_7ff3538e-fe6c-40f6-bed2-aeff7a588dac
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapreduce.job.reduces=<number>
Starting Job = job_1506650650336_0001, Tracking URL = http://chenghengchaodeMacBook-Air.local:8088/proxy/application_1506650650336_0001/
Kill Command = /usr/local/Cellar/hadoop/2.8.1/libexec/bin/hadoop job -kill job_1506650650336_0001
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2017-09-29 10:05:08,428 Stage-1 map = 0%, reduce = 0%
2017-09-29 10:05:14,768 Stage-1 map = 100%, reduce = 0%
2017-09-29 10:05:20,056 Stage-1 map = 100%, reduce = 100%
Ended Job = job_1506650650336_0001
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Reduce: 1 HDFS Read: 7587 HDFS Write: 101 SUCCESS
Total MapReduce CPU Time Spent: 0 msec
OK
3
Time taken: 29.506 seconds, Fetched: 1 row(s)
新建一張teacher 表:
hive> create table teacher(id bigint,name string) row format delimited fields terminated by '\t';
OK
Time taken: 0.134 seconds
在本地建立文件teacher.txt,字段之間用tab分割(tab即是\t)
1 趙老師
2 王老師
3 劉老師
4 鄧?yán)蠋?
加載數(shù)據(jù)
hive> load data local inpath '/Users/chenghengchao/teacher.txt' into table teacher;
Loading data to table default.teacher
OK
Time taken: 0.297 seconds
查詢數(shù)據(jù)
hive> select * from teacher;
OK
1 趙老師
2 王老師
3 劉老師
4 鄧?yán)蠋?Time taken: 0.093 seconds, Fetched: 4 row(s)
6.hive 數(shù)據(jù)表與元數(shù)據(jù)的關(guān)系
在搭建hive環(huán)境的時(shí)候,我們使用的是MySQL數(shù)據(jù)庫(kù)存儲(chǔ)元數(shù)據(jù)。再創(chuàng)建一張表people。現(xiàn)在一共有三張表。
hive> create table people (id int,name string);
OK
Time taken: 0.073 seconds
hive> show tables;
OK
people
student
teacher
Time taken: 0.074 seconds, Fetched: 3 row(s)
打開本地的mysql數(shù)據(jù)庫(kù),metastore 數(shù)據(jù)庫(kù)存儲(chǔ)了hive表相關(guān)的信息。
我們建立的表存放在TBLS表中。
表的字段存放在COLUMNS_V2表中。
數(shù)據(jù)在HDFS上的路徑存放在SDS表中。
在文件系統(tǒng)中也是可以看到的:
- 查詢所有數(shù)據(jù)不涉及計(jì)算,只需查詢出來就行了,因此不需要啟動(dòng)MapReduce
(即select * 不會(huì)啟動(dòng)MapReduce)
如果是count,sum等涉及到計(jì)算的操作就會(huì)啟動(dòng)MapReduce了。
8.表的類型有兩種MANAGED_TABLE和EXTERNAL_TABLE,可以在TBLS中查看,也可以用desc 表名查看。 - 在hive下可以直接操作HDFS
# 在hive下直接操作HDFS,建立data文件夾并上傳本地文件到HDFS,命名為a.txt 和b.txt
hive> dfs -mkdir /data;
hive> dfs -put /Users/chenghengchao/student.txt /data/a.txt;
hive> dfs -put /Users/chenghengchao/student.txt /data/b.txt;
可以在HDFS中驗(yàn)證一下:
10.創(chuàng)建外部表并指向已有的data目錄
hive> create external table ext_student(id bigint,name string) row format delimited fields terminated by '\t' location '/data';
OK
Time taken: 0.064 seconds
hive> select * from ext_student;
OK
1 zhangsan
2 lisi
3 wangwu
NULL NULL
1 zhangsan
2 lisi
3 wangwu
NULL NULL
Time taken: 0.076 seconds, Fetched: 8 row(s)
查詢結(jié)果顯示了8條記錄,前4條和后4條是一樣的。這是因?yàn)?data 下面有兩個(gè)文件a.txt 和b.txt,每個(gè)文件有4行,其中3行有數(shù)據(jù),最后一行為空。
如果再創(chuàng)建一個(gè)文件pep.txt,內(nèi)容如下,并把它上傳至/data目錄下。
100 aaa
101 bbb
查詢之后發(fā)現(xiàn)有10條記錄了,即/data下3個(gè)文件的內(nèi)容加到一起。
hive> dfs -put /Users/chenghengchao/pep.txt /data/pep.txt;
hive> select * from ext_student;
OK
1 zhangsan
2 lisi
3 wangwu
NULL NULL
1 zhangsan
2 lisi
3 wangwu
NULL NULL
100 aaa
101 bbb
Time taken: 0.085 seconds, Fetched: 10 row(s)
無論是外部表還是內(nèi)部表,只要把某個(gè)文件放到表的目錄下,就會(huì)被掃描并被查詢出來。查詢的執(zhí)行過程是先通過TBLS表找到student表,然后根據(jù)表id到COLUMNS_V2表查找這張表都有哪些字段,然后再根據(jù)表id到SDS表中查找應(yīng)該到HDFS的那個(gè)目錄下去查找數(shù)據(jù)
- hive分區(qū)表
分區(qū)最大的好處是提高查詢的速度。
創(chuàng)建分區(qū)表:
hive> create external table beauties(id bigint,name string,age int) partitioned by (nation string) row format delimited fields terminated by '\t' location '/beauty';
OK
Time taken: 0.095 seconds
創(chuàng)建表的同時(shí),也在HDFS中創(chuàng)建了目錄/beauty
然后創(chuàng)建兩個(gè)文件china.txt 和japan.txt并上傳到HDFS中的/beauty下
hive> dfs -put /Users/chenghengchao/japan.txt /beauty;
hive> select * from beauties;
OK
Time taken: 0.121 seconds
查詢發(fā)現(xiàn)并沒有數(shù)據(jù),這是因?yàn)闆]有上傳到特定的分區(qū)。
hive> load data local inpath '/Users/chenghengchao/china.txt' into table beauties partition(nation='China');
Loading data to table learnhive.beauties partition (nation=China)
OK
Time taken: 0.994 seconds
hive> select * from beauties;
OK
1 趙薇 30 China
2 趙麗穎 29 China
3 趙雅芝 62 China
Time taken: 0.152 seconds, Fetched: 3 row(s)
按照分區(qū)上傳后,查詢有了結(jié)果,并且多了一列分區(qū)字段
此時(shí),在HDFS中的多了一個(gè)nation=China文件夾,新上傳的china.txt存在此文件夾下。
接下來如果手動(dòng)創(chuàng)建notion=japan文件夾,并且上傳japan.txt文件,再查詢
hive> dfs -mkdir /beauty/nation=Japan;
hive> dfs -put /Users/chenghengchao/japan.txt /beauty/nation=Japan;
hive> select * from beauties;
OK
1 趙薇 30 China
2 趙麗穎 29 China
3 趙雅芝 62 China
Time taken: 0.167 seconds, Fetched: 3 row(s)
還是和之前的數(shù)據(jù)一樣,這是因?yàn)樵獢?shù)據(jù)庫(kù)中沒有記錄Japan這個(gè)分區(qū)。(看SBS表的話,只有notion=China的記錄)
12 修改分區(qū)
hive> alter table beauties add partition(nation='Japan') location '/beauty/nation=Japan'
> ;
OK
Time taken: 0.207 seconds
hive> select * from beauties;
OK
1 趙薇 30 China
2 趙麗穎 29 China
3 趙雅芝 62 China
1 福原愛 30 Japan
2 井上航平 22 Japan
3 酒井法子 45 Japan
Time taken: 0.11 seconds, Fetched: 6 row(s)
查詢結(jié)果多了Japan分區(qū)的數(shù)據(jù),并且在元數(shù)據(jù)庫(kù)中也能看到nation=Japan的記錄。
13 使用分區(qū)查詢數(shù)據(jù)
建立了分區(qū)之后,就能夠使用where條件進(jìn)行查詢,并且查詢效率將會(huì)提高
hive> select * from beauties where nation='China';
OK
1 趙薇 30 China
2 趙麗穎 29 China
3 趙雅芝 62 China
Time taken: 0.419 seconds, Fetched: 3 row(s)
hive> select * from beauties where nation='Japan';
OK
1 福原愛 30 Japan
2 井上航平 22 Japan
3 酒井法子 45 Japan
Time taken: 0.124 seconds, Fetched: 3 row(s)
以上是hive基礎(chǔ)的一些學(xué)習(xí)!