Based on the following tutorial, Hive has a map type. However, there does not seem to be a documented way to insert a new key-value pair into a Hive map, via a SELECT
with some UDF or built-in function. Is this possible?
As a clarification, suppose I have a table called foo
with a single column, typed map
, named column_containing_map
.
Now I want to create a new table that also has one column, typed map
, but I want each map (which is contained within a single column) to have an additional key-value pair.
A query might look like this:
CREATE TABLE IF NOT EXISTS bar AS
SELECT ADD_TO_MAP(column_containing_map, "NewKey", "NewValue")
FROM foo;
Then the table bar
would contain the same maps as table foo
except each map in bar
would have an additional key-value pair.
Consider you have a student table which contains student marks in various subjects.
You can insert values directly to table as below.
Here 'empinfo' table can be any table in your database. And Results are:
I'm sorry, I didn't quite get this. What do you mean by with some UDF or built-in function?If you wish to insert into a table which has a Map field it's similar to any other datatype. For example :
I have a table called complex1, created like this :
I also have a file, called com.txt, which contains this : Mohammad-Tariq,007:Bond
Now, i'll load this data into the above created table :
So this table contains :
I have one more table, called complex2 :
Now, to select data from complex1 and insert into complex2 i'll do this :
Scan the table to cross check :
HTH
I think the combine function from brickhouse will do what you need. Slightly modifying the query in your original question, it would look something like this
The limitation with this example is that str_to_map creates a MAP< STRING,STRING >. If your hive map contains other primitive types for the keys or values, this won't work.