Hive 之 DML 数据操作

叁歲伎倆 2023-09-29 19:07 27阅读 0赞

1. 数据导入

1.1 向表中 load 数据

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

  1. load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
  2. // 加载到 default 库 student 表
  3. load data local inpath '/opt/module/datas/student.txt' into table default.student;
  4. // 覆盖已有数据
  5. load data inpath '/user/atguigu/hive/student.txt' overwrite into table default.student;
  • local:从本地加载,否则从 hdfs
  • overwrite:覆盖已有数据,否则为追加
  • partition:上传到指定分区

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

insert 操作会执行 MR 操作

  1. // 创建一个分区表
  2. create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';
  3. // 一般插入
  4. insert into table student partition(month='201709') values(1,'wangwu');
  5. // 查询中间表(单表)方式插入
  6. insert overwrite table student partition(month='201708')
  7. select id, name from student where month='201709';
  8. // 查询中间表(多表)方式插入
  9. from student
  10. insert overwrite table student partition(month='201707')
  11. select id, name where month='201709'
  12. insert overwrite table student partition(month='201706')
  13. select id, name where month='201709';

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

  1. // 指定 hdfs 数据路径
  2. hive (default)> create table if not exists student5(
  3. id int, name string
  4. )
  5. row format delimited fields terminated by '\t'
  6. location '/user/hive/warehouse/student5';
  7. // 上传数据到 hdfs 上
  8. hive (default)> dfs -put /opt/module/datas/student.txt
  9. /user/hive/warehouse/student5;

1.4 import 导入数据

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

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

2. 数据导出

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

2.1 insert 导出

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

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

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

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

2.2 Hadoop命令导出到本地

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

2.3 Hive Shell 命令导出

  1. // 基本语法:(hive -f/-e 执行语句或者脚本 > file)
  2. bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

2.4 Export导出到HDFS上

  1. (defahiveult)> export table default.student to '/user/hive/warehouse/export/student';

发表评论

表情:
评论列表 (有 0 条评论,27人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HIVE DDL,DML

    每个人都有每个人的生活方式。每个人都有每个人要走的路,每条路不一定适合每个人,这就像每个人穿内裤一样,不在乎外表多么花俏,最重要的是适合自己,人生的十字路口又很多,选择了,就义