一、分区表
create table t_demo {
id string,
name string,
age int
}
partitioned by (year string,month string)
row format delimited fields terminated by '\t';
load data local inpath '/xxx/yyy/**.txt' into table t_demo partition(year='2018',month='02');
select * from t_demo where year='2018' and month='02'
注意事项:
- 如果对分区表,没有元数据,则可以通过msck repair table tablename修复,库中新增元数据
或者通过alter table add partition(year='2018',month='02'解决
- show partitions tablename;查看分区数 desc formatted tablename 查看表详情
二、导入数据
1.load data [local] inpath 'filepath' [overwrite] into table tablename [partition(prop1=xx,prop2=yy)]
- 原始数据的存储位置 local 或 hdfs
- 对标是否覆盖 overwrite或 追加
- 分区
2.insert into target_table select * from source_table
3.用location加载,参考 hive 外表
三、命令导入导出数据
1.insert overwrite loca directory 'filepath' select * from tablename;
2.hive -e ' select * from tablename; ' > /opt/datas/demo.txt
3.import/export
四、排序及自定义函数
1.排序说明
- order by :全局数据的排序,仅仅一个reduce
- sort by :每个reduce内部进行排序
- distribute by : 分区partition 经常跟sort by 配合使用,必须放sort by 前
- cluster by : 即是distribute by 和 sort by 字段相同时等效
2.自定函数
UDF,UDAF,UDTF 分别是相当于普通函数(一对一),聚合函数(多对一),高级应用(一对多)
- UDF继承UDF类,实现方法 ,add jar 添加或者create temporary function xx 'com.cn.xx'