hive中的null和' '
string类型
在对string类型的字段进行查询过滤空值(这里包括NULL和’’)的时候我们一般以:
select name from student where name is not null and name != '';
要特别注意的是,我们在工作的过程当中,处理的字段可能会有很多空格存在,一般都有trim函数配合length函数去除空格来判断是否为空:
select length(trim(name)) from student;
int类型
在对int类型(包括其他数值类型)的字段进行查询过滤空值(这里包括NULL和’’)的时候我们一般以:
select age from student where age is not null;
用null还是’'更好呢?
Hive中默认将null存为\N,如果表中存在大量的NULL值,则在Hive的数据文件中会产生大量的\N数据,浪费存储空间,那我们可以将serialization.null.format设置为’’
(说明一下:在Hive中是通过serialization.null.format参数来保存和标识NULL,通过将表的该参数修改为NULL表示为空值)
那么,如何将底层NULL的存储格式修改为’’
//建表时使用
create table student2(
id int,
name string,
age int
)
row format delimited fields terminated by ','
NULL DEFINED AS '' --只要加这一句就可以
stored as textfile
location '/user/hive/warehouse/student';
// 修改已有的表
alter table student set serdeproperties('serialization.null.format' = '');