hive表的常見操作

2021-10-12 08:47:43 字數 3064 閱讀 9726

一、 建立表

1)建立外部表

create external table ad_rule_result(

source string comment '',

entity_type int comment '')

partitioned by(day string,hour string)

row format delimited

fields terminated by ','

location 'hdfs://mycluster/user/vc/public/ad_rule_result';

2)建立內部表:

create table dw.zjmj_only_refund_money_risk_result_df(

buyer_id string comment '買家id',

refund_order_count int comment '僅退款訂單數',

order_count int comment '有效訂單數',

refund_order_rate float comment '僅退款訂單佔比',

solar_name string comment '處罰名',

day string comment ''

) comment '僅退款規則命中的買家'

location 'hdfs://user/hive/warehouse/dw.db/zjmj_only_refund_money_risk_result_df'

3)從老表中複製乙個表結構出來

create table if not exists temp.ad_rule_result like vc.ad_rule_result
4)備份表

create table temp.ad_rule_result_2  as 

select * from vc.ad_rule_result

二、刪除資料

1)刪除表

drop table if exists dw.zjmj_only_refund_money_risk_result_df
2)刪除某個分割槽

alter table vc.ad_rule_result drop partition (day='2020-08-10' , hour='21')
3)清空表中資料

truncate table vc.ad_rule_result
三、插入資料

1)從檔案中插入資料到hive表中

load data inpath   '/user/vc/***/part-00001_test' into table vc.ad_rule_result   partition(day='2020-08-10',hour='01')
2)將查詢到的資料插入表中

insert overwrite table dw.zjmj_only_refund_money_risk_result_df  

select * from dw.zjmj_only_refund_money_risk_result_df where day !='2020-07-30'

3)將查詢到的資料插入到某個分割槽(根據資料大小進行小檔案合併)

set mapred.max.split.size=256000000;

set mapred.min.split.size.per.node=100000000;

set mapred.min.split.size.per.rack=100000000;

set hive.input.format=org.apache.hadoop.hive.ql.io.combinehiveinputformat;

insert overwrite table vc.ad_rule_result partition(day='2020-08-10', hour='17')

select source,entity_type, entity_id, rule, hit_time, feature from vc.ad_rule_result where day='2020-08-10' and hour='17';

四、 修改表

1)刪除某列

hive 沒有刪除欄位的命令,但是可以用replace 來實現

alter table  vc.ad_rule_result_back replace columns(

source string comment '',

entity_type int comment '',

entity_id string comment '',

rule string comment '',

hit_time bigint comment '',

feature string comment '');

新增乙個列

alter table vc.ad_rule_result add columns(mark string)
3)更改表名

alter table table_name rename to new_table_name;
五、檢視表資訊

1)檢視建表語句

show create table test.stu
2)檢視表的結構

desc table test.stu
3)檢視表的分割槽列表

show  partitions  test.stu

Hive中庫和表的常見操作

目錄 表的常見操作 create database schema if not exists database name comment database comment 庫的注釋說明 location hdfs path 庫在hdfs上的路徑 with dbproperties property ...

hive常見的DML操作

一 load資料 load data local inpath filepath overwrite into table tablename partition partcol1 val1,partcol2 val2 filepath 如果是local則是本地檔案,否則就是hdfs檔案 overw...

HIVE中的表操作

1.內部表 2.外部表 3.分割槽表 4.分通表 擴充套件 臨時表 只有在程序中有效 程序結束 表所有資料刪除 與內部表類似 show databases 檢視資料庫 show tables 檢視表 use 資料庫名 進入資料庫 drop 資料庫名 刪除資料庫 drop 表名 刪除表 內部表建立的方...