Hive: Cannot insert into table with map column

2019-08-16 06:59发布

问题:

here is my table

hive> desc test_tab;
OK
test_map    map<string,string>
test_date               string

# Partition Information
# col_name              data_type               comment

test_date               string
Time taken: 0.087 seconds, Fetched: 7 row(s)

and here is my insert statement

hive> insert into table test_tab
    > values ('2018-02-28', map("key","val"));

but i get

FAILED: ParseException line 2:0 cannot recognize input near 'values' '(' ''2018-02-28'' in select clause

I also tried

hive> insert into table test_tab partition (test_date = '2018-02-28')
    > select  map("key","val");
FAILED: NullPointerException null

what am i doing wrong?

Here is hive version info

Hive 0.13.1-SNAPSHOT

回答1:

You can load the data into the hive table in two ways
* Using HDFS command:
Your table structure should by MAP KEYS TERMINATED BY '=' (Any delimiter)

CREATE TABLE `test_sample`(
  `test_map` map<string,string>   
)
PARTITIONED BY (
  `test_date ` string)
ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ','
  MAP KEYS TERMINATED BY '='
STORED AS INPUTFORMAT
  'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'

sample input.txt
Feb=28

hadoop fs -put input.txt HDFS_dest_Path

Once you uplload the file into the Hadoop fs, then load data into the table:

load data inpath <location of the hadoop table> into table test_sample partition(test_date='2019-02-28');
  • Using Queries:

If similar data already present in the file system you can insert that into your table via hive Query.

INSERT INTO TABLE test_tab PARTITION(test_date= '2019-02-28')
select map from  test_sample;

NOTE: MAP KEYS TERMINATED BY '=' constraint not necessarily required for test_tab table.



标签: java hive hiveql