Hive基本使用

2021-09-19 16:44:15 字數 4333 閱讀 2250

hive基本使用

庫操作

建立庫

# 進入終端

beeline -u " jdbc:hive2: "

-n user

# 判斷是否存在並新增注釋

create database if not exists zxl_test comment 'hive_test'

;# 新增屬性

create database if not exists zxl_test with

dbproperties

('creator'

='zxl'

,'date'

='2019-04-09');

# 描述庫

desc database zxl_test;

desc database extended zxl_test;

# 檢視建庫語句

show create database zxl_test;

刪除庫

# 庫下無表,直接刪除

drop database dbname;

drop database if exists dbname;

# 庫下有表

方式1:先刪除表再刪除庫;

方式2: drop database if exists zxl_test cascade;

表操作

建立表

# 內部表

create table student

(id int, name string, *** string, age int,department string) row format delimited fields terminated by ","

;# 外部表

create external table student_ext

(id int, name string, *** string, age int,department string) row format delimited fields terminated by "," location "/hive/student"

;# 分割槽表

create external table student_ptn

(id int, name string, *** string, age int,department string) partitioned by

(city string) row format delimited fields terminated by "," location "/hive/student_ptn"

;# 新增分割槽

alter table student_ptn add partition

(city=

"beijing");

#分桶表

create external table student_bck

(id int, name string, *** string, age int,department string) clustered by

(id) sorted by

(id asc, name desc) into 4 buckets row format delimited fields terminated by "," location "/hive/student_bck"

;# 匯入本地資料

load data local inpath "/data1/student.txt" into table student;

#使用ctas建立表

create table student_ctas as select *

from student where id <

95012;

# 複製表結構

(able前面沒有加external關鍵字則是內部表,否則外部表)

create table student_copy like student;

檢視表

# 檢視庫下的表

show tables in zxl_test;

# 模糊匹配表名

show tables like '*dent*'

;# 檢視表結構

desc student;

desc extended student;

desc formatted student;

(格式好)

# 檢視分割槽資訊

show partitions student_ptn;

# 檢視建表語句

show create table student_ptn;

修改表

# 修改表名

alter table student rename to new_student;

# 增加字段

alter table new_student add columns

(score int)

;# 修改字段定義

alter table new_student change name new_name string;

# 替換所有字段

alter table new_student replace columns

(id int, name string, address string)

;# 替換可以用來刪除字段

alter table new_student replace columns

(id int, name string)

;# 刪除表

drop table new_student;

# 清空表

truncate table student_ptn;

修改分割槽

# 靜態分割槽

alter table student_ptn add partition

(city=

"shenzhen");

# 動態分割槽 [內容直接插入到另一張表student_ptn_age中,並實現age為動態分割槽]

load data local inpath "/data1/student.txt" into table student_ptn partition

(city=

"beijing");

create table student_ptn_age

(id int,name string,*** string,department string) partitioned by

(age int)

;set hive.exec.dynamic.partition.mode=nonstrict;

insert overwrite table student_ptn_age partition

(age) select id,name,***,department,age from student_ptn;

# 刪除分割槽

alter table student_ptn drop partition

(city=

'beijing'

);

hive函式

內建函式

# 檢視內建函式

show functions;

# 顯示函式的詳細資訊

desc function extended substr;

自定義函式udf

udf(user-defined function)作用於單個資料行,產生乙個資料行作為輸出。(數學函式,字 符串函式)

udaf(使用者定義聚集函式 user- defined aggregation funcation):接收多個輸入資料行,並產 生乙個輸出資料行。(count,max)

udtf(**生成函式 user-defined table functions):接收一行輸入,輸出多行(explode)

hive命令列

# hive -e sql語句  執行完退出終端

hive -e "select * from zxl_test.student"

;#hive -f sql檔名 執行完退出終端

select *

from zxl_test.student # test.sql內容

hive -f test.sql

# hive臨時設定

檢視屬性值: set 屬性名

臨時修改屬性值: set 屬性名 = 屬性值

shylin

Hive基本使用

啟動hadoop sbin start all.sh 啟動hive bin hive 建立表 create table table name col name data type comment col comment create table hive wordcount context stri...

HIVE基本使用

筆者注 這裡安裝的版本為hive 0.13.1 注 表示可選 create database if not exists db hive use db hive drop database if exists db hive create table stu id int name string r...

Hive 3 Hive 基本使用

1 建立庫 create database if not exists mydb 2 檢視庫 show databases 3 切換資料庫 use mydb 4 建立表 create table if not exists t user id string,name string 或 create ...