1、show databases;
查看都有哪些數據庫
hive> show databases;
OK
default
Time taken: 0.141 seconds, Fetched: 2 row(s)
hive>
2、create database park;
創建park數據庫
hive> create database park;
OK
Time taken: 0.376 seconds
hive> show databases;
OK
default
park
Time taken: 0.027 seconds, Fetched: 2 row(s)
hive>
3、use park;
進入park數據庫
hive> use park;
OK
Time taken: 0.057 seconds
hive>
4、show tables;
查看當前數據庫下所有表
hive> show tables;
OK
Time taken: 0.046 seconds
hive>
5、create table stu(id int,name string);
創建stu表,以及相關的兩個字段
hive> create table stu(id int,name string);
OK
Time taken: 0.482 seconds
hive> show tables;
OK
stu
Time taken: 0.055 seconds, Fetched: 1 row(s)
hive>
6、insert into stu values(1,‘zhang’);
向stu表插入數據
hive> insert into stu values(1,'zhang');
Query ID = root_20200528154450_bee5a3e2-dc10-4016-9d78-28f1bfea2406
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1590626315637_0002, Tracking URL = http://node1:8088/proxy/application_1590626315637_0002/
Kill Command = /usr/local/src/hadoop-2.9.2/bin/hadoop job -kill job_1590626315637_0002
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2020-05-28 15:45:48,732 Stage-1 map = 0%, reduce = 0%
2020-05-28 15:46:13,543 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 3.03 sec
MapReduce Total cumulative CPU time: 3 seconds 30 msec
Ended Job = job_1590626315637_0002
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to: hdfs://node1:9000/user/hive/warehouse/park.db/stu/.hive-staging_hive_2020-05-28_15-44-50_434_1338240324720857993-1/-ext-10000
Loading data to table park.stu
Table park.stu stats: [numFiles=1, numRows=1, totalSize=8, rawDataSize=7]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Cumulative CPU: 3.3 sec HDFS Read: 3478 HDFS Write: 72 SUCCESS
Total MapReduce CPU Time Spent: 3 seconds 300 msec
OK
Time taken: 116.87 seconds
hive>
7、select * from stu;
查看錶數據
hive> select * from stu;
OK
1 zhang
Time taken: 1.273 seconds, Fetched: 1 row(s)
hive>
8、load data local inpath ‘2.txt’ into table stu;
通過加載文件數據到指定的表裏( 默認當前目錄爲 root/ )
也可以通過其它方法直接上傳到hdfs hive 數據庫中
[root@node1 ~]# vi 2.txt
[root@node1 ~]# cat 2.txt
2 mei
[root@node1 ~]#
hive> select * from stu;
OK
1 zhang
NULL NULL
Time taken: 0.691 seconds, Fetched: 2 row(s)
hive>
加載文件數據到stu表,出現空字符
即第一列和第二列是以空格爲分隔符的。 但是把數據導入到hive之後,hive並不知道分隔符是什麼,所以就不能正確的切分數
據。所以顯示null。 解決辦法:在hive創建表的時候,要指定分割符,並且這個分割符要和外部文件裏的 分割符一致。
9、create table stu1(idint,name string) row format delimited fields terminated by ’ ';
創建stu1表,並指定分割符空格。
hive> create table stu1(id int,name string) row format delimited fields terminated by ' ';
OK
Time taken: 0.567 seconds
hive> show tables;
OK
stu
stu1
Time taken: 0.078 seconds, Fetched: 3 row(s)
hive>
此時,把外部數據導入hive,就可以正確查出數據了。
hive> select * from stu1;
OK
1 zhang
2 wang
3 li
4 zhao
5 chen
6 liu
7 huang
8 yang
9 bai
Time taken: 0.188 seconds, Fetched: 9 row(s)
hive>
10、desc stu;
查看stu表結構
hive> desc stu;
OK
id int
name string
Time taken: 0.287 seconds, Fetched: 2 row(s)
hive>
11、create table stu2 like stu;
創建一張stu2表,表結構和stu表結構相同
hive> show tables;
OK
stu
stu1
stu2
values__tmp__table__2
Time taken: 0.027 seconds, Fetched: 4 row(s)
hive> desc stu2;
OK
id int
name string
Time taken: 0.219 seconds, Fetched: 2 row(s)
hive>
12、alter table stu rename to stu3;
爲表stu重命名爲stu1
hive> alter table stu rename to stu3;
OK
Time taken: 0.365 seconds
hive> show tables;
OK
stu1
stu2
stu3
values__tmp__table__2
Time taken: 0.116 seconds, Fetched: 4 row(s)
hive>
13、alter table stu3 add columns (age int);
爲表stu1增加一個列字段age,類型爲int
hive> alter table stu3 add columns (age int);
OK
Time taken: 0.292 seconds
hive> desc stu3;
OK
id int
name string
age int
Time taken: 0.193 seconds, Fetched: 3 row(s)
hive>
14、 insert overwrite table stu1 select * from stu2;
把stu2表數據插入到stu1表中(只有兩個表屬性相同才能插入)
hive> desc stu2;
OK
id int
name string
Time taken: 0.124 seconds, Fetched: 2 row(s)
hive> desc stu1;
OK
id int
name string
Time taken: 0.115 seconds, Fetched: 2 row(s)
hive> insert overwrite table stu1 select * from stu2;
Query ID = root_20200528161455_dce43421-aafd-481f-a3d8-1a6f45e8fdfd
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1590626315637_0003, Tracking URL = http://node1:8088/proxy/application_1590626315637_0003/
Kill Command = /usr/local/src/hadoop-2.9.2/bin/hadoop job -kill job_1590626315637_0003
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2020-05-28 16:15:23,968 Stage-1 map = 0%, reduce = 0%
2020-05-28 16:15:37,798 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.38 sec
MapReduce Total cumulative CPU time: 1 seconds 380 msec
Ended Job = job_1590626315637_0003
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to: hdfs://node1:9000/user/hive/warehouse/park.db/stu1/.hive-staging_hive_2020-05-28_16-14-55_929_8015397837048918811-1/-ext-10000
Loading data to table park.stu1
Table park.stu1 stats: [numFiles=1, numRows=0, totalSize=0, rawDataSize=0]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Cumulative CPU: 1.38 sec HDFS Read: 3223 HDFS Write: 35 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 380 msec
OK
Time taken: 47.245 seconds
hive>
15、insert overwrite local directory ‘/stu’ row format delimited fields terminated by ’ ’ select * from stu3;
將查詢到的數據保存到本地(不加local 保存到HDFS)
16、insert overwrite directory ‘/stu6’ row format delimited fields terminated by ’ ’ select * from stu3;
保存查詢數據到HDFS
hive> insert overwrite directory '/stu6' row format delimited fields terminated by ' ' select * from stu3;
Query ID = root_20200528162300_48d95d4f-c2c3-46c2-a522-6c2a71b9c8ca
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1590626315637_0005, Tracking URL = http://node1:8088/proxy/application_1590626315637_0005/
Kill Command = /usr/local/src/hadoop-2.9.2/bin/hadoop job -kill job_1590626315637_0005
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2020-05-28 16:23:21,807 Stage-1 map = 0%, reduce = 0%
2020-05-28 16:23:34,654 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 2.48 sec
MapReduce Total cumulative CPU time: 2 seconds 480 msec
Ended Job = job_1590626315637_0005
Stage-3 is selected by condition resolver.
Stage-2 is filtered out by condition resolver.
Stage-4 is filtered out by condition resolver.
Moving data to: hdfs://node1:9000/stu6/.hive-staging_hive_2020-05-28_16-23-00_136_8301333514132090063-1/-ext-10000
Moving data to: /stu6
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Cumulative CPU: 2.48 sec HDFS Read: 3051 HDFS Write: 20 SUCCESS
Total MapReduce CPU Time Spent: 2 seconds 480 msec
OK
Time taken: 38.871 seconds
hive>