hive常用語法

2021-09-26 22:38:30 字數 3409 閱讀 8944

hive基本操作

#檢視資料庫

show databases

#檢視表資訊

show tables

#檢視分割槽

show partitions

#檢視函式

show functions

#檢視詳細資訊 (格式模糊)

desc extended t_tablename;

#檢視詳細資訊 (格式清晰)

desc formatted t_tablename;

#刪除庫

drop database test ;

#強制刪除庫

drop database test cascade; -

***修改表名:

alter table t_old_tables rename to t_new_tables;

***修改分割槽名:

alter table t_school partition(province ='sx',city ='ty') rename to partition(province='gd',city ='sz');

***新增分割槽:

alter table t_school add partition (province='beijing',city='chaoyang');

***刪除分割槽:

alter table t_school drop partition (province='sx',city='yc');

***修改表的檔案格式定義:

alter table t_school partition(province='sx',city='yc') set fileformat sequencefile;

***修改列名定義:

alter table table_name change [column] col_old_name col_new_name column_type [commentcol_comment] [first|(after column_name)]

alter table t_user change price jiage float first;

***增加/替換列:

alter table table_name add|replace columns (col_name data_type[comment col_comment], ...)

alter table t_movie add columns (score string); //新增列

alter table t_movie replace columns (m_name string,yanhuan string); //替換列(字段型別要一致)

修改內部表 t_student 為外部表

alter table t_student set tblproperties(external='true')

修改外部表 t_student 為內部表

alter table t_student set tblproperties(external='false')

建乙個儲存文字檔案的表
create table t_access_text(ip string,url string,access_time string)

row format delimited fields terminated by ','

stored as textfile;

建乙個儲存sequence file檔案的表
create table t_access_seq(ip string,url string,access_time string)

stored as sequencefile;

建乙個儲存parquet file檔案的表
create table t_access_parq(ip string,url string,access_time string)

stored as parquetfile;

內部表
create table t_internal_tables(id int,name string)

row format delimited

fields terminated by ',';

外部表
create table t_external_tables(id int,name string)

row format delimited

fields terminated by ',';

location '/user/logs'; #存放的位置

分割槽表
create table t_partition(name string,age string)

partitioned by(day string)

row format delimited

fields terminated by ',';

# 注意:分割槽字段(這邊的day)不能是表定義中的已存在字段

向分割槽表匯入資料
load data local inpath '你要匯入資料的路徑' into table 表名 partition(分割槽字段='值',分割槽字段='值');

load data local inpath '/home/h_data.dat' into table t_partition partition(day='zhouyi');

復合型別
create table t_test(

id int,

name string,

actors array, --array型別

books map, --map型別

info struct--struct型別

)row format delimited

fields terminated by ','

collection items terminated by '#'

map keys terminated by ':';

函式

毫秒值換算為當前時間

select from_unixtime(131452000,"yyyy-mm-dd hh-mm-ss");
轉換為毫秒值

select unix_timestamp("2018-12-07 10:30:55");
求最大值

select greatest(7,10,22,5);
切分

select split("192.168.62.111","\\.");

hive常用語法

指定分隔符為逗號 create table student id string,birthday string,grade int,m1 int,m2 int,m3 int,m4 int,memo string row format delimited fields terminated by 外部...

hive資料操作常用語法

1 建立臨時表 先create臨時表,然後insert資料 insert into tmp table name select from select from 寫查詢語句 tmp 子查詢的別名 2 清空表中的資料 truncate table tablename 3 刪除表中的部分資料 不支援de...

Hive(三)常用語法(Hive QL)

1.hive建立資料庫 create database schema if notexists 2.hive建立表 hive裡一般有兩種表的結構,表和外部表,以下分別是兩種表的建立 create table phone info id int,name string,storage string,p...