Search code examples
hivehiveqlhadoop2

NULL column names in Hive query result


I have downloaded the weather .txt files from NOAA, which looks like:

WBAN,Date,Time,StationType,SkyCondition,SkyConditionFlag,Visibility,VisibilityFlag,WeatherType,WeatherTypeFlag,DryBulbFarenheit,DryBulbFarenheitFlag,DryBulbCelsius,DryBulbCelsiusFlag,WetBulbFarenheit,WetBulbFarenheitFlag,WetBulbCelsius,WetBulbCelsiusFlag,DewPointFarenheit,DewPointFarenheitFlag,DewPointCelsius,DewPointCelsiusFlag,RelativeHumidity,RelativeHumidityFlag,WindSpeed,WindSpeedFlag,WindDirection,WindDirectionFlag,ValueForWindCharacter,ValueForWindCharacterFlag,StationPressure,StationPressureFlag,PressureTendency,PressureTendencyFlag,PressureChange,PressureChangeFlag,SeaLevelPressure,SeaLevelPressureFlag,RecordType,RecordTypeFlag,HourlyPrecip,HourlyPrecipFlag,Altimeter,AltimeterFlag
00102,20150101,0001,0,OVC043, ,10.00, , , ,27, ,-2.8, ,26, ,-3.1, ,25, ,-3.9, , 92, , 0, ,000, , , ,30.05, , , , , ,30.36, ,AA, , , ,30.23, 
00102,20150101,0101,0,OVC045, ,10.00, , , ,27, ,-2.8, ,26, ,-3.1, ,25, ,-3.9, , 92, , 6, ,080, , , ,30.07, , , , , ,30.37, ,AA, , , ,30.25, 
00102,20150101,0201,0,OVC047, ,10.00, , , ,26, ,-3.3, ,25, ,-3.7, ,24, ,-4.4, , 92, , 6, ,090, , , ,30.08, , , , , ,30.39, ,AA, , , ,30.26, 
00102,20150101,0301,0,OVC049, ,10.00, , , ,26, ,-3.3, ,25, ,-3.7, ,24, ,-4.4, , 92, , 7, ,100, , , ,30.09, , , , , ,30.40, ,AA, , , ,30.27, 

Then I have created the following table:

CREATE EXTERNAL TABLE weather(WBAN STRING, `Date` STRING, Time STRING, StationType INT, SkyCondition STRING, SkyConditionFlag STRING, Visibility INT, VisibilityFlag STRING, WeatherType STRING, WeatherTypeFlag STRING, DryBulbFarenheit INT, DryBulbFarenheitFlag STRING, DryBulbCelsius DECIMAL, DryBulbCelsiusFlag INT, WetBulbFarenheit INT, WetBulbFarenheitFlag INT, WetBulbCelsius DECIMAL, WetBulbCelsiusFlag INT, DewPointFarenheit INT, DewPointFarenheitFlag INT, DewPointCelsius DECIMAL, DewPointCelsiusFlag INT, RelativeHumidity INT, RelativeHumidityFlag INT, WindSpeed INT, WindSpeedFlag INT, WindDirection INT, WindDirectionFlag INT, ValueForWindCharacter INT, ValueForWindCharacterFlag INT, StationPressure DECIMAL, StationPressureFlag INT, PressureTendency INT, PressureTendencyFlag INT, PressureChange INT, PressureChangeFlag INT, SeaLevelPressure DECIMAL, SeaLevelPressureFlag INT, RecordType STRING, RecordTypeFlag STRING, HourlyPrecip DECIMAL, HourlyPrecipFlag INT, Altimeter DECIMAL, AltimeterFlag INT) 
    COMMENT 'Our weather table in HIVE!' 
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
     LINES TERMINATED BY '\n' 
     LOCATION '/data/Weather';

Now if I try a simple query like:

hive> select * from weather limit 10;

I get a result like the following, and with Null replacing some column's names!

WBAN    Date    Time    NULL    SkyCondition    SkyConditionFlag    NULL    VisibilityFlag  WeatherType WeatherTypeFlag NULL    DryBulbFarenheitFlag    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULLNULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    RecordType  RecordTypeFlag  NULL    NULL    NULL    NULL
00102   20150101    0001    0   OVC043      10              27      -3  NULL    26  NULL    -3  NULL25  NULL    -4  NULL    NULL    NULL    NULL    NULL    0   NULL    NULL    NULL    30  NULL    NULL    NULL    NULL    NULL    30  NULL    AA      NULL    NULL    30  NULL
00102   20150101    0101    0   OVC045      10              27      -3  NULL    26  NULL    -3  NULL25  NULL    -4  NULL    NULL    NULL    NULL    NULL    80  NULL    NULL    NULL    30  NULL    NULL    NULL    NULL    NULL    30  NULL    AA      NULL    NULL    30  NULL
00102   20150101    0201    0   OVC047      10  

as you may noticed, the fourth and the seventh columns (and many after those) are tilted NULL when they should be StationType, and Visibility... etc respectively!

Even if I tried:

hive> select Visibility from weather limit 10;

I will get the correct result, but with NULL column title/name!!!

Why the NULL column names/titles?!


Solution

  • Interesting question, it took me a minute to realize what is going on but with the right knowledge of hive it is actually obvious!

    1. The first thing to note here is that the NULL values occur in columns that are not of type string.
    2. The second thing to realize is that hive (unlike beeline for example) normally does NOT print column headers above your selection.

    So, putting 1 and 2 together:

    • The column names are fine, as you will see from a query like Describe Weather.
    • The file that you use as datasource, appears to have had column names on the first row. These are now making up the first row of your hive table. Of course the columns of type string have no problem dealing with this data, but columns of type int will show NULL when they are asked to handle strings that cannot be cast to int properly.

    Suggestion:

    Try to get rid of the first row, preferably before creating the external table.