HQL操作之?dāng)?shù)據(jù)操作

HQL操作之?dāng)?shù)據(jù)操作

數(shù)據(jù)導(dǎo)入

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
  • LOCAL :
    • load data local :從本地加載數(shù)據(jù),本地文件會(huì)拷貝到Hive表指定的位置
    • load data :從HDFS加載數(shù)據(jù),移動(dòng)到Hive表指定位置。
  • inpath : 數(shù)據(jù)路徑
  • overwirte:是否覆蓋數(shù)據(jù)。沒(méi)有就是追加
  • partition:將數(shù)據(jù)加載到指定分區(qū)。
  • 創(chuàng)建表的時(shí)候,也可以直接加載數(shù)據(jù),但此時(shí)數(shù)據(jù)文件必須在HDFS中。
CREATE TABLE tabB (
  id INT,
  name string,
  area string
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
Location '/user/hive/tabB';

插入數(shù)據(jù)

-- 創(chuàng)建分區(qū)表
CREATE TABLE tabC (
  id INT
 ,name string
 ,area string
)
partitioned by (month string);
-- 插入數(shù)據(jù)
insert into table tabC partition(month='202001') values (5, 'wangwu', 'BJ'), (4, 'lishi', 'SH'), (3, 'zhangsan', 'TJ');
-- 插入查詢的結(jié)果數(shù)據(jù)
insert into table tabC partition(month='202002') select id, name, area from tabC where month='202001';
-- 多表(多分區(qū))插入模式
from tabC
insert overwrite table tabC partition(month='202003') select id, name, area where month='202002'
insert overwrite table tabC partition(month='202004') select id, name, area where month='202002';


-- 根據(jù)查詢結(jié)果創(chuàng)建表,tabD 不是分區(qū)表
create table if not exists tabD as select * from tabC;

數(shù)據(jù)導(dǎo)出

--將查詢數(shù)據(jù)導(dǎo)出到本地
insert overwrite local directory '/mnd/hadoop/data/tabC' select * from tabC;
-- 格式化輸出
insert overwrite local directory '/mnd/hadoop/data/tabC'
row format delimited fields terminated by ' '
select * from tabC;

-- 輸出到HDFS
insert overwrite directory '/hive-test/tabC'
row format delimited fields terminated by ' '
select * from tabC;

-- dfs 命令導(dǎo)出數(shù)據(jù)到本地。本質(zhì)是執(zhí)行數(shù)據(jù)文件的拷貝
dfs -get /user/hive/warehouse/mydb.db/tabc/month=202001 /home/hadoop/data/tabC4


-- hive 命令導(dǎo)出數(shù)據(jù)到本地。執(zhí)行查詢將查詢結(jié)果重定向到文件 
hive -e "select * from mydb.tabC" > a.log

-- export 導(dǎo)出數(shù)據(jù)到HDFS。使用export導(dǎo)出數(shù)據(jù)時(shí),不僅有數(shù)還有表的元數(shù)據(jù)信息 
export table tabC to '/user/hadoop/data/tabC4';


-- 使用 like tname創(chuàng)建的表結(jié)構(gòu)與原表一致但是沒(méi)數(shù)據(jù)。create ... as select ... 有數(shù)據(jù)到那時(shí)結(jié)構(gòu)可能不一致
create table tabE like tabc;
-- export 導(dǎo)出的數(shù)據(jù),可以使用 import 命令導(dǎo)入到 Hive 表中,HDFS路徑
import table tabE from '/user/hadoop/data/tabC4';

-- 截?cái)啾?,清空?shù)據(jù)。(注意:僅能操作內(nèi)部表) 
truncate table tabE;
-- 以下語(yǔ)句報(bào)錯(cuò),外部表不能執(zhí)行 truncate 操作
alter table tabC set tblproperties("EXTERNAL"="TRUE");
truncate table tabC;

導(dǎo)入數(shù)據(jù):

import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';

小結(jié):

導(dǎo)入數(shù)據(jù):

  • load data
  • insert
  • create table....as select ....
  • Import table

數(shù)據(jù)導(dǎo)出

  • Insert overwrite .... diretory
  • hdfs dfs -get
  • Hive -e "select..." > a.log
  • Export table..
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容