数据导入
向表导入数据
load data [local] inpath 'hdfs://master:9000/xx/[文档]' [overwrite] into table 表
注意:如果表有分区,load data [local] inpath 'hdfs://master:9000/xx/[文档]' [overwrite] into table 表 partition(year=2014,month=12)
从本地数据导入hive中
create table t_name(name string,age int,sex string);
load data location inpath '/home/hadoop/logs/aaa.log' into table t_name;
从HDFS导入数据到hive中
create table t_name(name string,age int,sex string);
load data inpath '/home/hadoop/logs/aaa.log' into table t_name;
导入分区表:
load data location inpath '/home/hadoop/logs/aaa.log' into table t_name partiton(分区字段名='分区字段内容');
从hive表导入hive表中:
通过查询导入数据
create table t_name(name string,age int,sex string);
insert into table t_name select a_name,a_age,a_sex from 数据库名 group by 字段;
案例1:如果表有数据,删除在插入
insert overwrite table 表名
[partition(year=2014,month=12)]
select 子句
案例2:使用insert into追加记录
hive> insert into table mytemp
> select empno,ename from emp limit 5;
向person表不同分区插入数据------静态分区
from tp
insert overwrite table person
partition(xb='male')
select * where sex='男'
insert overwrite table person
partition(xb='female')
select * where sex='女' ;
动态分区插入
要求原始表有分区字段;
动态分区插入语句,会将原始表按分区在新表中插入数据
语法:
insert overwrite table 新表(字段名,不包含分区名)
partition(新表分区字段)
select 原始表字段,别名.分区字段 from 原始表
执行成功后,
会根据原始表分区字段在新表中按对应分区插入数据;
注意:
动态分区要求在使用时必须设置set hive.exec.dynamic.partition.mode=nonstrict;
属性。
动态和静态结合
语法:
insert overwrite table 新表 (字段名,不包含分区名)
partition(静态分区='初始值',动态分区名)
select 原始表字段,别名.对应静态分区字段,别名.对应动态分区字段 from 原始表 别名
where 别名.静态分区字段=初始值
通过select子句创建新表
语法 :
create table 新表名称
as
select 列名,... from 源表;
总结:
hive不支持行级别 更新,删除
导出hive数据
导出hive表数据到本地目录
语法:
insert overwrite local directory '/本地路径' select 子句
insert [overwrit] local directory '本地路径' select 字段名..... from 表名 where 条件;
注意:
导出数据前目录要存在;
将表中不同分区的数据导出到本地不同的目录下
from 源表
insert overwrite local directory '/本地目录的绝对路径'
select 字段名 where 分区字段1=值1
insert overwrite local directory '/本地目录的绝对路径'
select 字段名 where 分区字段1=值2
insert overwrite local directory '/本地目录的绝对路径'
select 字段名 where 分区字段1=值3