hive 之 DML 操作(六)

1. 数据导入

1.1 向表中 load 数据

load 可以从本地服务器、hdfs 文件系统加载数据到数据表中:

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

// 加载到 default 库 student 表
load data local inpath '/opt/module/datas/student.txt' into table default.student;

// 覆盖已有数据
load data inpath '/user/atguigu/hive/student.txt' overwrite into table default.student;
  • local:从本地加载,否则从 hdfs
  • overwrite:覆盖已有数据,否则为追加
  • partition:上传到指定分区

1.2 通过查询语句向表中插入 (insert) 数据

insert 操作会执行 MR 操作

// 创建一个分区表
create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';

// 一般插入
insert into table  student partition(month='201709') values(1,'wangwu');

// 查询中间表(单表)方式插入
insert overwrite table student partition(month='201708')
             select id, name from student where month='201709';
         
// 查询中间表(多表)方式插入    
from student
              insert overwrite table student partition(month='201707')
              select id, name where month='201709'
              insert overwrite table student partition(month='201706')
              select id, name where month='201709';

1.3 创建表时指定数据路径 location

// 指定 hdfs 数据路径
hive (default)> create table if not exists student5(
              id int, name string
              )
              row format delimited fields terminated by '\t'
              location '/user/hive/warehouse/student5';

// 上传数据到 hdfs 上
hive (default)> dfs -put /opt/module/datas/student.txt
/user/hive/warehouse/student5;

1.4 import 导入数据

import 一般配合 export 使用,先用 export 导出后,再将数据导入

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

2. 数据导出

  • insert:可以导出到本地、hdfs
  • hadoop 命令
  • hive shell 命令
  • Export 命令
  • Sqoop 导出

2.1 insert 导出

1、将查询的结果导出到本地:

hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
            select * from student;

// 格式化再导出
hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
           ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'             select * from student;

2、将查询的结果导出到HDFS上(没有local)

hive (default)> insert overwrite directory '/user/atguigu/student2'
             ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
             select * from student;

2.2 Hadoop命令导出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0
/opt/module/datas/export/student3.txt;

2.3 Hive Shell 命令导出

// 基本语法:(hive -f/-e 执行语句或者脚本 > file)

bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

2.4 Export导出到HDFS上

(defahiveult)> export table default.student to '/user/hive/warehouse/export/student';
posted @ 2021-12-05 20:32  Hubery_Jun  阅读(47)  评论(0编辑  收藏  举报