hive 数据导入 导出

目录

    • hive outline
    • 数据导入hive
      • 本地文件
        • 1. 加载本地文件studet.txt到hive默认数据库student表中(拷贝+追加)
        • 2. 加载本地文件studet.txt到hive默认数据库student表中(拷贝+覆盖)
      • hdfs
        • 3. 加载hdfs文件到hive默认数据库student表中(移动+追加)
        • 4. 加载hdfs文件到hive默认数据库student表中(移动+覆盖)
      • hql
        • 5. insert+values (很少使用)
        • 6. insert + select (大量使用)
        • 动态分区插入
        • hive cte
    • 数据从hive导出
      • 本地
        • 1.将查询的结果导出到本地
        • 2.将查询的结果格式化导出到本地
      • hdfs
        • 3.将查询的结果导出到 hdfs 上(没有 local 关键字)
      • hive shell 命令导出

hive outline

链接

  • 数据导入hive

本地文件

注意:所谓本地文件,指的是源数据文件(这里是studet.txt)和hiveserver2在同一节点上

1. 加载本地文件studet.txt到hive默认数据库student表中(拷贝+追加)

拷贝:本地文件依然存在
追加:如果hive表student有数据,会追加
注:

  1. /opt/modules/hive-1.2.1/input是本地文件所在路径
  2. 不走mr
 load data local inpath '/opt/modules/hive-1.2.1/inpu/student.txt' into table default.student;
2. 加载本地文件studet.txt到hive默认数据库student表中(拷贝+覆盖)

覆盖:如果hive表student有数据,会覆盖

 load data local inpath '/opt/modules/hive-1.2.1/inpu/student.txt' overwrite  into table default.student;

hdfs

3. 加载hdfs文件到hive默认数据库student表中(移动+追加)

移动:hdfs上的input目录下的student.txt文件从input目录移动了hive数据仓库所在的地方
追加:如果hive表student有数据,会追加
注解:

  1. 其中/input/student.txt是hdfs的文件路径
  2. 不走mr
 load data inpath '/input/student.txt' into table default.student;
4. 加载hdfs文件到hive默认数据库student表中(移动+覆盖)

移动: hdfs上的input目录下的student.txt文件从input目录移动了hive数据仓库所在的地方

覆盖:如果hive表student有数据,会覆盖

注解:

  1. 其中/input/student.txt是hdfs的文件路
  2. 不走mr
 load data inpath '/input/student.txt' overwrite into table default.student;

hql

5. insert+values (很少使用)

注解:会走mr

insert into table  default.student  values(1,'x');
6. insert + select (大量使用)

注解:

  1. 会走mr
  2. 在数据文件存储格式转换时经常用(例如把文件存储格式由textfile—>sequencefile)

这里会把对people 表的查询结果插入到表student表 中

 insert overwrite table student select * from people;            
动态分区插入

hive 动态分区/加载数据到目标分区表

hive cte

从其他表中查询数据,然后插入

  • 数据从hive导出

本地

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

注意:

  1. /opt/modules/output/hive/student是本地路径
  2. 如果不指定分隔符,默认为\001
 insert overwrite local directory '/opt/modules/output/hive/student' select * from student;
2.将查询的结果格式化导出到本地

/opt/modules/output/hive/student1是本地路径

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

hdfs

3.将查询的结果导出到 hdfs 上(没有 local 关键字)
 insert overwrite directory '/output/hive/student2' row format delimited fields terminated by '\t' select * from student;

hive shell 命令导出

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

bin/hive  -e  'select * from default.student;' > /opt/modules/output/hive/student3.txt;

你可能感兴趣的:(Hive,hive)