hive几种导入数据方式

load

语法: load data [local] inpath ‘xx’ into table 表名 partition() local: 如果导入的文件在本地文件系统,需要加上local,使用put将本地上传到hdfs 不加local默认导入的文件是在hdfs,使用mv将源文件移动到目标目录

hadoop fs -put department / load data inpath ‘/department’ into table deptpart3 partition(area=‘suzhou’);

insert

insert方式运行MR程序,通过程序将数据输出到表目录! 在某些场景,必须使用insert方式来导入数据: ①向分桶表插入数据 ②如果指定表中的数据,不是以纯文本形式存储,需要使用insert方式导入

语法: insert into|overwrite table 表名 select xxx | values(),(),() 
			insert into: 向表中追加新的数据
			insert overwrite: 先清空表中所有的数据,再向表中添加新的数据
			
特殊情况: 多插入模式(从一张源表查询,向多个目标表插入)
			from 源表
			insert xxxx  目标表  select xxx
			insert xxxx  目标表  select xxx
			insert xxxx  目标表  select xxx

举例: from deptpart2
         insert into table deptpart1 partition(area=huaxi) select deptno,dname,loc
         insert into table deptpart1 partition(area=huaxinan) select deptno,dname,loc

location

在建表时,指定表的location为数据存放的目录 create external table if not exists default.deptpart3(deptno int, dname string, loc int) PARTITIONED BY(area string) row format delimited fields terminated by ‘ ’ location ‘hdfs://hadoop1:9000/deptpart3’;

导出

  1. insert : 将一条sql运算的结果,插入到指定的路径 语法: insert overwrite [local] directory ‘/opt/module/datas/export/student’ row format xxxx select * from student;
  2. export : 既能导出数据,还可以导出元数据(表结构)! export会在hdfs的导出目录中,生成数据和元数据! 导出的元数据是和RDMS无关! 如果是分区表,可以选择将分区表的部分分区进行导出! 语法: export table 表名 [partition(分区信息) ] to hdfspath export table deptpart1 partition(area=‘huazhong’) to ‘/export1’;

import

不仅可以导入数据还可以顺便导入元数据(表结构)。Import只能导入export输出的内容!

IMPORT [[EXTERNAL] TABLE 表名(新表或已经存在的表) [PARTITION (part_column=“value”[, …])]] FROM ‘source_path’ [LOCATION ‘import_target_path’] ①如果向一个新表中导入数据,hive会根据要导入表的元数据自动创建表 ②如果向一个已经存在的表导入数据,在导入之前会先检查表的结构和属性是否一致 只有在表的结构和属性一致时,才会执行导入 ③不管表是否为空,要导入的分区必须是不存在的

import external table importtable1  from /export1
经验分享 程序员 微信小程序 职场和发展