hive中有个严格模式,即set hive.mapred.mode=strict,如果select语句中不包含限制性字句时就会报错,可根据情况进行使用,如果不想使用可设为nonstrict(注:本人一开始不知道默认值是多少,随便尝试了下district,还真可以,应该是只要不是strict就可以了吧哈哈),严格模式有助于提高写sql语句的效率,防止乱写sql语句。
hive的分区表:
hive中提供了分区表来提高查找的效率
create table test(...) partitioned by (...) 创建分区表
show partitions test partition(...) 列出分区
指定存储格式,两种方式:一种是直接通过语句来指定,另一种可以是可以指定java中的一些类来指定(这种方式比较复杂,需要花时间去详细了解)。
drop table if exists test; 删除表
更改表
alter table log_messages rename to logmsgs;
alter table log_messages add if not exists partition(...) location '...';
alter table log_messages partition(...) set location '...';
alter table log_messages set TBLPROPERTIES(...=...);
hive不支持行的insert,delete,update,将数据入表只能通过批量载入(bulk load)
1.load data local inpath '${env:HOME}/california-employees'
overwrite into table employees
partition (...); 分区表
2.insert overwrite table employees
partition (...)
select * from staged_employees se
where se.cnty='US' and se.st='OR'; 分区表
3.from staged_employees se
insert overwrite table employees
partition(...)
select * where se.cnty='US' and se.st='OR'
insert overwrite table employees
partition(...)
select * where se.cnty='US' and se.st='CA'
insert overwrite table employees
partition(...)
select * where se.cnty='US' and se.st='IL'; 非分区表分区,也可以使用动态分区插入数据
导出数据
由于数据本身是文本明文,所以可以直接使用hdfs的拷贝文件导出,如果需要改动数据格式,可以使用insert overwrite
insert overwrite local directory '/...'
select name,salary,address
from employees
where se.state='CA';