Hive数据导入导出

Hive的数据导入导出指的是将数据从本地或者是hdfs导入到表中,其中本地导入的话,其实是两部操作,一是上传到hdfs,二是在hdfs移动到仓库位置。记住,本地上传一定要加local

一、数据导入

1、Load模式

1)语法

hive> load data [local] inpath   文件路径  [overwrite] into table 表名 [partition (partcol1=val1,…)];

local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表,在hdfs上是移动数据,不是复制

overwrite:表示覆盖表中已有数据,否则表示追加,写了它也得写into

2、Insert

into是表示的追加,overwrite指的是覆盖

1)insert into 表名 partition (partcol1=val1,…)  values(val1, val2)

Hive插入数据很慢,其实,hive更适合做读操作

2) As select创建表的时候加载数据,或者从其他的表中获得数据在  insert into select

实例1:create table if not exists student3   as select id, name from student;

实例2: insert into student   select  id,name from student_ori; 

3) 创建表时通过Location指定加载数据路径

hive (default)> create table if not exists student5(

              id int, name string

              )    row format delimited fields terminated by '\t'    location '/user/hive/warehouse/student5';

4)   Import数据到指定Hive表中

注意:先用export导出后,再将数据导入。因为export导出的时候有元数据,

hive (default)> import table student2 partition(month='201709') from

 '/user/hive/warehouse/export/student';

二、数据导出

1、insert 导出 (只会这种就可以)

最常用是Insert导出

写入本地文件和hdfs只能用overwrite,不能用into

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

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

2).将查询的结果格式化导 出到本地

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

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

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

2、 Hadoop命令导出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0

/opt/module/datas/export/student3.txt;

3、 Hive Shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)一个大于号是追加,两个是覆盖

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

4、Export导出到HDFS

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

5、Sqoop导出

三 清除表中数据(Truncate)

注意:Truncate只能删除管理表,不能删除外部表中数据

hive (default)> truncate table student;

猜你喜欢

转载自blog.csdn.net/student__software/article/details/81634688
今日推荐