Hive 啟動
~$ hive
退出
hive>quit; --退出hive
or
hive> exit; --exit會影響之前的使用,所以需要下一句kill掉hadoop的進程 >hadoop job -kill jobid
選擇使用哪個數據庫
hive> use database_name; --使用哪個數據庫
查看數據表結構
hive> describe tab_name; or desc tab_name; --查看表的結構及表的路徑
查看數據庫的描述及路徑
hive> describe database database_name; or hive> desc database database_name; --查看數據庫的描述及路徑
Hive QL
- 創建數據庫
-- 創建hello_world數據庫 create database hello_world; -- 如果數據庫已經存在就會拋出一個錯誤信息,使用如下語句可以避免拋出錯誤信息: create database if not exists database_name
- 查看所有數據庫
show databases;
- 查看所有表
show tables;
- 創建內部表
-- 創建hello_world_inner create table hello_world_inner ( id bigint, account string, name string, age int ) row format delimited fields terminated by '\t';
- 創建分區表
create table hello_world_parti ( id bigint, name string ) partitioned by (dt string, country string) ;
- 展示表分區
show partitions hello_world_parti;
- 更改表名稱
alter table hello_world_parti to hello_world2_parti;
- 刪除數據表
hive>drop table t1 ; --刪除表t1 or hive> drop table if exists t1;
- 可以用下面的命令來修改數據庫的路徑:
hive> create database database_name location '路徑'; hive> drop database if exists database_name; --刪除空的數據庫 hive> drop database if exists database_name cascade; --先刪除數據庫中的表再刪除數據庫
- 導入數據
load data local inpath '/home/deploy/user_info.txt' into table user_info;
導入數據的幾種方式
比如有一張測試表:
create table hello ( id int, name string, message string ) partitioned by ( dt string ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE ;
- 從本地文件系統中導入數據到hive表
load data local inpath 'data.txt' into table hello;
- 從HDFS上導入數據到hive表
- 從別的表中查詢出相應的數據並導入到hive表中
- 創建表時從別的表查到數據並插入的所創建的表中