#数据的导入
load方式:
load data [local] inpath '/path' [overwrite] into table tableName partition(type='');
注:
1. Load 操作只是单纯的复制/移动操作,将数据文件移动到 Hive 表对应的位置
2. 指定了 local 会从本地文件系统中加载数据, 如果没有指定 local, 会从hdfs上加载数据
3. 指定了 overwrite 关键字,首先将目标表下的数据删除后,然后将新数据添加到表中
4. 在加载数据时, hive与mysql的区别
hive是读时模式,也就是说在加载数据时,hive不会检查加载的数据是否符合规范;
关系型数据库(mysql)是严格写时模式,如果写入的数据有误,会报错;
insert方式:
insert into table tablename [partition(type='')] select * from xxx ;
insert overwrite table tablename [partition(type='')] select * from xxx ;
as方式 既能创建表,还同时具备导数据功能:
create table if not exists 要创建的表名称
as
select 字段a,字段b from 已经存在的表 where xxx ;
#导出数据:
1、从hive表导出到本地目录
2、从hive表导出到hdfs目录
3、 > 重定向到文件中
1、
insert overwrite local directory '/home/hivedata/exp2'
row format delimited fields terminated by '\t'
select * from aa7;
2、
insert overwrite directory '/hivedata/exp2'
row format delimited fields terminated by ','
select * from aa7;
3、
hive -e "use 数据库名;select * from 表名" > /home/hivedata/exp3;
#修改表
动态添加分区:
可以在select语句里面通过使用分区值来动态指明分区
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert into table 表名 partition (age) select id, name, tel, age from 表名;
修改分区中的数据路径:
ALTER TABLE 表名 PARTITION (date='2016') SET LOCATION "数据路径" ;
修改分区名称:
ALTER TABLE 表名 PARTITION (date='原来的分区字段名称') RENAME TO PARTITION ( date='要修改后的字段名称') ;
添加列:
ALTER TABLE 表名 ADD COLUMNS ( 添加字段的名称 数据类型 );
//在所有存在的列后面,但是在分区列之前添加一列
例如给hive表temptable 添加字段 a,b
alter table temptable add columns ( a string, b string );
表的重命名:
alter table 旧表名 rename to 新表名;