1 导入
1.1 load
load data [local] inpath '数据路径' into table 表名 [partition]
带local:从本地将数据put到hdfs上的表目录!
不带local: 代表将hdfs上的数据,mv到hdfs上的表的目录!
1.2 insert
insert导入数据会运行MR程序,在特殊的场景下,只能使用insert不能用load!
例如:
①分桶
②希望向hive表中导入的数据以SequnceFile或ORC等其他格式存储!
语法:
insert into | overwrite table 表名 [partition()] values(),(),() | select 语句
insert into: 向表中追加写
insert overwrite: 覆盖写,清空表目录(hdfs层面,和外部表无关),再向表中导入数据
多插入模式:从一张源表查询,执行多条insert语句,插入到多个目的表
from 源表
insert xxxx 目标表1 select xxxx
insert xxxx 目标表2 select xxxx
insert xxxx 目标表3 select xxxx
示例:
from t3
insert overwrite table t31 partition(province='henan',city='mianchi',area='chengguanzhen') select id,name,sex
where province='guangdong' and city='shenzhen' and area='baoan'
insert overwrite table t32 partition(province='hebei',city='mianchi',area='chengguanzhen') select id,name,sex
where province='guangxi' and city='liuzhou' and area='buzhidao'
insert overwrite table t33 partition(province='hexi',city='mianchi',area='chengguanzhen') select id,name,sex
where province='guangxi' and city='nanning' and area='buzhidao'
1.3 location
建表时可以指定表的location属性(表在hdfs上的目录)。适用于数据已经存在在hdfs上了,只需要指定表的目录和数据存放的目录关联即可!
1.4 import
import必须导入的数据是由export命令导出的数据!
IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]]
FROM 'source_path'
[LOCATION 'import_target_path']
要求: ①如果要导入的表不存在,那么hive会根据export表的元数据生成目标表,再导入数据和元数据
②如果表已经存在,在导入之前会进行schame的匹配检查,检查不复合要求,则无法导入!
③如果目标表已经存在,且schame和要导入的表结构匹配,那么要求要导入的分区必须不能存在!
2、导出
2.1 insert
命令:
insert overwrite [local] directory '导出的路径'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select 语句;
带local导出到本地的文件系统,不带local代表导出到hdfs!
2.2 export导出
命令:
EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
TO 'export_target_path' [ FOR replication('eventid') ]
优势: 既导出数据还导出metastore(元数据,表结构)
导出的数据和表结构可以移动到其他的hadoop集群或hive中,使用import导入!