hive 二 hive常用命令操作

2021-10-05 09:49:32 字數 3766 閱讀 4920

三、常用命令

四、常用資料型別

這節將介紹hive常用命令操作,包括資料庫操作、表操作、資料操作等。hive的採用了類sql的語法,也稱為hql。

2.1 以互動方式執行命令

bin/hive
此時可以在命令列中輸入set hive.cli.print.current.db=true 可以顯示當前選中資料庫。

2.2 以非互動方式執行命令

bin/hive -e hql
3、以檔案方式執行命令
bin/hive -f hql檔案
備註:

hive常用引數如下:

bin/hive -option

option:

-h 幫助

-e 執行hql語句

-f 執行hql檔案

-p 指定埠

-s 不列印日誌

-i 從檔案初始化hql

-v 輸出執行的hql到控制台

這裡採用非互動方式執行hql語句,定義:

hive_exe=

"/home/china/programs/hive/bin/hive -e"

3.1 資料庫操作
######### 資料庫操作 ###########

#檢視資料庫

$"show databases;"

#建立資料庫

$"create database school;"

#刪除資料庫

$"drop database school;"

#選中資料庫

$"use school;"

##################################

3.2 表操作
######### 表操作 ##################

#建立表

$"create table school.stu(id int, stu_name string) row format delimited fields terminated by ',' ;"

#檢視表 方式一

$"desc school.stu;"

#檢視表 方式二

show create table stu;

#修改表 修改表名

#格式:alter table name rename to new_name

$"alter table school.stu_1 rename to school.stu_2;"

#修改表 新增字段

#格式:alter table name add columns (col_spec[, col_spec...])

$"alter table school.stu_2 add columns (stu_id int);"

#修改表 修改字段

#格式:alter table name change column_name new_name new_type

$"alter table school.stu_2 change stu_id stu_id string;"

#修改表 刪除字段,hive沒有刪除欄位的語句,不過可以通過replace間接刪除

#格式:alter table name replace columns(col_spec[, col_spec...]);

$"alter table school.stu_2 replace columns(id int, stu_name string);"

#刪除表

#格式:drop table if exists table_name;

$"drop table if exists school.stu_2;"

##################################

3.3 資料操作

hive沒有行級的資料增刪改操作。

######### 資料操作 ##################

#hive沒有行級的資料插入、更新、刪除操作

#從本地檔案匯入資料

#格式: load data local inpath data_file overwrite into table table_name

$"load data local inpath 'hive_data_stu.csv' overwrite into table school.stu_1;"

#從hdfs目錄匯入資料

#格式: load data inpath data_file overwrite into table table_name

$"load data inpath '/hive_data_dump/000000_0' overwrite into table school.stu;"

#查詢$

"select * from school.stu_1;"

#條件查詢

$#資料匯出到本地目錄

#格式: insert overwrite local directory result_dir select * from table_name;

$"insert overwrite local directory '/home/china/shell_space/my_study/hive_data_dump' row format delimited fields terminated by ',' select * from school.stu_1;"

$"insert overwrite local directory '/home/china/shell_space/my_study/hive_data_dump' select * from school.stu_1;"

#資料匯出到hdfs目錄

#格式: insert overwrite directory result_dir select * from table_name;

$"insert overwrite directory '/hive_data_dump' row format delimited fields terminated by ',' select * from school.stu_1;"

$"insert overwrite directory '/hive_data_dump' select * from school.stu_1;"

################################

######### 常用資料型別 ###########

boolean 布林型別,true或false

#整數tinyint 1byte有符號整數

smallint 2byte有符號整數

int 4byte有符號整數

bigint 8byte有符號整數

#浮點數

float 4byte單精度浮點數

double 8byte雙精度浮點數

binary 位元組陣列

#字串

string 字串

#日期timestamp 可以為整數(距2023年1月1日的秒數),也可以是浮點數(距2023年1月1日的秒數,小數點後是納秒,保留9位),也可以是字串(格式為yyyy-mm-dd hh:mm:ss.fffffffff)

#集合資料型別

array陣列型別,如arraystruct結構體型別,如:structmap鍵值對型別,如:map################################

型別之前轉換可以使用cast(s as type)

hive常用命令

進入hive目錄後執行hive命令進入命令模式 建立新錶 hive create table t hive a int,b int,c int row format delimited fields terminated by t 匯入資料t hive.txt到t hive表 hive load d...

hive常用命令

建立新錶 hive create table t hive a int,b int,c int row format delimited fields terminated by t 匯入資料t hive.txt到t hive表 hive load data local inpath home co...

Hive常用命令

檢視hdfs路徑 show create table table name 建表 create table tbname var1 char type1,var2 char type2 載入資料到表 刪除表 drop table tbname if expr1,expr2,expr3 expr1 判...