use hive sql, use insert overwrite to get data into file, but NULL value is set as \N, how to get NULL value as empty string
Assistant
In Hive, when you use the INSERT OVERWRITE command to write data into files, by default Hive represents NULL values as . However, if you wish to represent values as an empty string instead, you can change this behavior by setting a Hive configuration property , which stands for Serializer/Deserializer.
\N
NULL
serde
The configuration property you need to alter is serialization.null.format. By default, this is set to \N, but you can change it to an empty string for your session or for a particular table.
Here’s how you can do it:
Globally for Your Session
If you want to change the representation of NULL values to empty strings for all tables and queries in your current Hive session, you can execute the following command at the beginning of your session:
sql
SET serde.serialization.null.format=;
By setting the serialization.null.format property to an empty string, any subsequent INSERT OVERWRITE operations you perform will represent NULL values as empty strings.
For a Specific Table
If you prefer to change the NULL representation only for a specific table, you can specify this property at the time of table creation or alter an existing table to use this property. Here’s how you can do it:
For a New Table
sql
CREATE TABLE my_table (
col1 STRING,
col2 INT
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'serialization.null.format'=''
)
STORED AS TEXTFILE;
Altering an Existing Table
sql
ALTER TABLE my_table SET SERDEPROPERTIES ('serialization.null.format'='');
By using these commands, you are instructing Hive to use an empty string instead of \N to represent NULL values for the specific table.
Important Note
Make sure that you carefully consider the implications of representing NULL values as empty strings, especially in terms of how your data is subsequently processed and analyzed. This change can impact data processing and analysis, potentially causing empty strings and NULL values to be treated as equivalent when they might not be.
Also, keep in mind that these changes might not persist across different sessions or after a cluster restart, depending on your environment’s configuration and whether you’re setting these for a session or for a specific table. Always verify your settings in a new session or after significant changes to your environment.