Hive學習(六)DML資料操作

2021-10-01 14:52:03 字數 2076 閱讀 6109

目錄

資料匯入

裝載資料(load)

通過查詢語句向表中插入資料(insert)

查詢語句中建立表並載入資料(as select)

資料匯出

清除表中資料

語法:

load data [local] inpath 'file_path' [overwrite] into table tb_name [partition(partcol1=val1,...)]
load data:表示載入資料

local:表示從本地載入資料到 hive 表;否則從 hdfs 載入資料到 hive 表

inpath:表示載入資料的路徑

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

into table:表示載入到哪張表

tb_name:表示具體的表

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

案例,載入本地資料到student表中:

hive> load data local inpath '/opt/module/datas/student.txt' into table student_part partition(dt='201912');

loading data to table iceink.student_part partition (dt=201912)

partition iceink.student_part stats: [numfiles=2, numrows=0, totalsize=96, rawdatasize=0]

ok

-- 基本插入資料

insert into table student_part partition(dt='201909') values(1,'posion',20);

​-- 根據單張表查詢結果

insert overwrite table student_part partition(dt='201909') select id,name,age from student_part where dt = '201911';

create table if not exists student_copy2 as select id,name,age from student;
資料匯出有很多種方式,如:insert匯出、hadoop命令匯出、hive shell匯出、export匯出、sqoop匯出

下面主要介紹insert匯出。

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

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

​結果資料:

1iceink18

2icydate18

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

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

​結果資料:

1 iceink 18

2 icydate 18

​-- 將查詢結果匯出到hdfs上,沒有local

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

語法: truncate table tb_name;

注意:

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

hive> truncate table student_external; failed: semanticexception [error 10146]: cannot truncate non-managed table student_external.

Hive學習筆記(五) DML 資料操作

5.2 資料匯出 5.3 清除表中資料 truncate 1 語法 hive load data local inpath opt module datas student.txt overwrite into table student partition partcol1 val1,1 load...

Hive中DML資料操作

1.資料匯入 1 向表中裝載資料 load 語法 load data 表示載入資料 local 表示從本地載入資料到hive表 否則從hdfs載入資料到hive表 inpath 表示載入資料的路徑 into table 表示載入到哪張表 student 表示具體的表 overwrite 表示覆蓋表中...

(六)MySQL資料操作DML

順序插入資料 insert into 表名 values 值1,值2,值3 指定字段插入資料 insert into 表名 欄位1,欄位2,欄位3 values 值1,值2,值3 插入多條記錄 insert into 表名 values 值1,值2,值3 值1,值2,值3 插入查詢結果 insert...