DML
1.Load
Load語句可將文件導入到Hive表中。
hive>
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)];
關鍵字說明:
-
local
:表示從本地加載數(shù)據(jù)到Hive表;否則從HDFS加載數(shù)據(jù)到Hive表。 -
overwrite
:表示覆蓋表中已有數(shù)據(jù),否則表示追加。 -
partition
:表示上傳到指定分區(qū),若目標是分區(qū)表,需指定分區(qū)。
1.加載本地文件到hive
本地文件路徑:
執(zhí)行l(wèi)oad語句:load data local inpath '/opt/module/hive-3.1.3/datas/student.txt' into table student;
查詢數(shù)據(jù)(select * from student):
2.加載數(shù)據(jù)覆蓋表中已有的數(shù)據(jù)
load data local inpath '/opt/module/hive-3.1.3/datas/student.txt' overwrite into table student;
3.加載HDFS文件到hive
上傳文件到HDF根目錄:
執(zhí)行加載文件語句: load data inpath '/student.txt' into table student;
從本地加載是copy的過程,從HDFS加載是move的過程。
2.Insert
1.將查詢結果插入表中
INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement;
-
INTO
:將結果追加到目標表 -
OVERWRITE
:用結果覆蓋原有數(shù)據(jù)
(1)新建一張表
hive (default)>
create table student1(
id int,
name string
)
row format delimited fields terminated by '\t';
(2)根據(jù)查詢結果插入數(shù)據(jù)
hive (default)> insert overwrite table student2
select
id,
name
from student;
2.將給定Values插入表中
INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1[=val1], partcol2[=val2] ...)] VALUES values_row [, values_row ...]
hive (default)> insert into table student1 values(1,'wangwu'),(2,'zhaoliu');
3.將查詢結果寫入目標路徑
INSERT OVERWRITE [LOCAL] DIRECTORY directory [ROW FORMAT row_format] [STORED AS file_format] select_statement;
insert overwrite local directory '/opt/module/datas/student' ROW FORMAT S ERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
select id,name from student;
3.Export&Import
Export
導出語句可將表的數(shù)據(jù)和元數(shù)據(jù)信息一并導出到HDFS的路徑-
Import
可將Export
導出的內容導入Hive,表的數(shù)據(jù)和元數(shù)據(jù)信息都會恢復。 Export
和Import
可用于兩個Hive實例之間的數(shù)據(jù)遷移。
--導出
EXPORT TABLE tablename TO 'export_target_path'
--導入
IMPORT [EXTERNAL] TABLE new_or_original_tablename FROM 'source_path' [LOCATION 'import_target_path']
示例:
--導出
hive>
export table default.student to '/user/hive/warehouse/export/student';
文章來源:http://www.zghlxwxcb.cn/news/detail-860614.html
--導入
hive>
import table student2 from '/user/hive/warehouse/export/student';
文章來源地址http://www.zghlxwxcb.cn/news/detail-860614.html
到了這里,關于Hive——DML(Data Manipulation Language)數(shù)據(jù)操作語句用法詳解的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!