5 DML資料操作

2021-09-10 02:12:20 字數 3267 閱讀 7793

語法

load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table table_name [partition (partcol1=val1,…)];

load data:表示載入資料

local:表示從本地載入資料到hive表(複製);否則從hdfs載入資料到hive表(移動)

inpath:表示載入資料的路徑

overwrite into:表示覆蓋表中已有資料,否則表示追加

into table:表示載入到哪張表

student:表示具體的表

partition:表示上傳到指定分割槽

案例準備資料

新建student1.txt和student2.txt,輸入以下內容

student1.txt

1   zhao  18

2 jun 19

student2.txt

3  feng  17

4 xiang 16

5 bin 15

將student2.txt 上傳到hdfs

hadoop fs -put /opt/module/datas/student2.txt /

開始操作

-- 建立student表

create table if not exists student(

id int,

name string,

age int

)partitioned by(year string)

row format delimited fields terminated by '\t';

-- 載入本地檔案到hive

load data local inpath '/opt/module/datas/student.txt'into table student partition(year='2017-2018');

-- 載入hdfs上的檔案

load data inpath '/student.txt' into table student partition(year='2017-2018');

-- 覆蓋上傳

load data local inpath '/opt/module/datas/student.txt' overwrite into table student partition(year='2017-2018');

還是上面那張表

-- 基本插入資料

insert into table student partition(year='2017-2018') values(11, 'zzz',10);

-- 根據單張表的查詢結果插入資料

insert into table student partition(year='2018-2019') select id,name,age from student where year='2017-2018';

-- 根據多張表的查詢結果插入資料

insert into table student partition(year='2019-2020')

select id,name,age from student where year='2017-2018'

union

select id,name,age from student where year='2018-2019';

from student

insert into table student partition(year='2020-2021')

select id,name,age where year in ('2017-2018','2018-2019','2019-2020');

根據查詢結果建立表(查詢的結果會新增到新建立的表中)

create table if not exists student1 as select id,name,age from student where year in ('2017-2018','2018-2019','2019-2020','2020-2021');

create table if not exists student2(

id int, name string, age int

)row format delimited fields terminated by '\t'

location '/user/hive/warehouse/student2';

dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student2/;

select * from student2;

先使用export匯出後,再將資料匯入

-- 將查詢結果匯出到本地

insert overwrite local directory '/opt/module/datas/output/student1' select * from student;

-- 將查詢結果格式化匯出到本地

insert overwrite local directory '/opt/module/datas/output/student2' row format delimited fields terminated by '\t' select * from student;

-- 將查詢結果格式化匯出到hdfs

insert overwrite directory '/output/student1' row format delimited fields terminated by '\t' select * from student;

dfs -get /user/hive/warehouse/student1/000000_0 /opt/module/datas/output/student.txt

基本語法:(hive -f/-e 執行語句或者指令碼 > file)

hive -e 'select * from default.student' > student1.txt

export table student to '/output/student3/';

這個後面會寫文章詳細講述

truncate只能刪除管理表,不能刪除外部表中資料

truncate table student;

DML資料操作

示例 hive本地檔案系統匯入資料到hdfs的hive表hive load data local inpath data st.txt into table st hive select from st hdfs檔案匯入到hdfs的hive表 hdfs有檔案 hive load data inpat...

第5章 DML資料操作 hive筆記

資料匯出 1 語法 hive load data local inpath opt module datas student.txt overwrite into table student partition partcol1 val1,1 load data 表示載入資料 2 local 表示從...

DML 資料操作語言

本小白日常oracle學習總結,若有錯誤望海涵,並希望大神能指點迷津 開發中使用的部分 主要指資料庫的查詢與更新 例如 select,update,查詢該使用者下的所有表 select from tab查詢某乙個表的表結構 desc 表名 select子句中可以直接使用四則運算 select子句對應...