hive 空值的處理

2021-08-15 20:18:26 字數 1191 閱讀 9583

hive的使用中不可避免的需要對null、『』(空字串)進行判斷識別。但是hive有別於傳統的資料庫。

下面一一說明:

(1)不同資料型別對空值的儲存規則

int與string型別資料儲存,null預設儲存為 \n;

string型別的資料如果為"",儲存則是"";

另外往int型別的字段插入資料「」時,結果還是\n。

(2)不同資料型別,空值的查詢

對於int可以使用is null來判斷空;

而對於string型別,條件is null 查出來的是\n的資料;而條件 =』』,查詢出來的是""的資料。

例如:查詢1:select b.id,b.name from b

結果1:

1        lisi

2         

3   null

查詢2:select b.id,b.name from b where b.name isnull;

結果2:

3        null

查詢3:select b.id,b.name from b where b.name=』』;

結果3:

2  查詢4:select b.id,b.name from b where b.name=』』or b.name is null;

結果4:

2  3   null

查詢5:select b.id,b.name from b where b.name<>』』and b.name is not null;

結果5:

1        lisi

查詢6:select b.id,b.name from b where length(b.name)<>0and b.name is not null;

結果6:

1       lisi

結論:判斷空時要根據實際的儲存來進行判斷。在開發過程中如果需要對空進行判斷,一定得知道儲存的是哪種資料。

有個處理空的小技巧,hive給出一種並非完美的解決方法——自定義底層用什麼字元來表示null:

使用:alter table b set serdeproperties ('serialization.null.format'='');

這句話的意思是讓null和''等價,也就是讓null不顯示,因為null對開發來說不好操作,可能不同地方代表意義不同,而且轉碼可能也會有問題,所有用''代替。

空值欄位的hive處理

當遇到某個欄位的屬性值為空時,可以使用if isnull函式進行處理。hive的if函式 if expr1,expr2,expr3 if expr1 is true expr1 0 and expr1 null then if returns expr2 otherwise it returns e...

Hive基礎 hive空值判斷

hive中空值判斷基本分兩種 1 null 與 nhive在底層資料中如何儲存和標識null,是由 alter table name set serdeproperties serialization.null.format n 引數控制的比如 1 設定 alter table name set s...

hive 空值 null判斷

hive中空值判斷基本分兩種 1 null 與 n hive在底層資料中 如何儲存和標識null,是由 alter table name set serdeproperties serialization.null.format n 引數控制的 比如 1.設定 alter table name se...