Dynamic partitioned hive table can help to store raw data into partitioned form which may be helpful in further querying.
Select query is generally faster when executed on a partitioned table. Its always adviced to create an external table on raw file in HDFS and then insert that data into partitioned table.
But when we try to insert really large file into a dynamically partitioned table it many a times fails due to many files being opened at mapper stage.
Following steps can solve the issue of massive data insert into hive dynamically partitioned table :
Lets say table is like :
CREATE TABLE IF NOT EXISTS stocks_main(
PARTITIONed BY (ymd STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,’ LOCATION ‘/user/impadmin/NYSE_PARTITIONED’;
and Stocks is an external table created over raw data in HDFS with same schema.
Now we need to pull data from Stocks table into partitioned Stocks_Main table.
We need to set following properties to enable inserts :
Execute following query to insert into table :
insert into table stocks_main partition(ymd) select exchange,symbol,price_open,price_high,price_low,price_close,volume,price_adj_close,ymd from stocks;
This results in only mapper programs and in case of many files being opened, the insert operation fails.
This can be sorted out by moving the file writing operation at reducer stage by executing the above query with “distribute by” clause :
insert into table stocks_main partition(ymd) select exchange,symbol,price_open,price_high,price_low,price_close,volume,price_adj_close,ymd from stocks distribute by ymd;
This will convert only mappers to map reduce and fix problem of huge file writing to hive table.