Hive基本操作

2021-09-22 16:22:52 字數 4714 閱讀 5064

庫操作

建立庫

beeline -u " jdbc:hive2: " -n user

beeline -u "jdbc:hive2: hive ***"

# ldap方式

beeline -u jdbc:hive2: -n username -p password --hiveconf mapreduce.job.queuename=

'yarnroot.queen_name' -e 'show databases;'

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";

# 將內部表轉為外部表

alter table student set tblproperties('external'='true');

# 將外部表轉為內部表

alter table student_ext set tblproperties('external'='false');

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";

create table student_ctas as select * from student where id < 95012;
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, hometown string);
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 set

tblproperties

('external'

='true'

);

alter table student set

tblproperties

('external'

='false'

);

修改分割槽
alter table student_ptn add partition(city="shenzhen");
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 "select * from zxl_test.student";

beeline -u " jdbc:hive2: user passwd" -e "sql"

select * from zxl_test.student # test.sql內容

hive -f test.sql

檢視屬性值:  		set 屬性名

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

shylin

hive基本操作

1.顯示所有資料庫 show databases 2.使用某個資料庫 use xx xx表示某個資料庫名 3.顯示某資料庫下的所有表 show tables 4.檢視表結構 顯示各欄位 desc 表名 5.資料匯出到本地 在hive中操作 insert overwrite local directo...

hive 基本操作

檢視表的詳細資訊 desc 表名 desc formatted 表名 建立外部表 create external table emp ext empno int,ename string,job string,mgr int,hiredate string,sal double,comm doubl...

HIVE基本操作

hive操作 一 建立分割槽 乙個表可以有多個分割槽,為避免過多的小檔案,建議只能對離散字段進行分割槽 create table if not exists stocks ymd date,price open float,price high float,price low float,price...