Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a key-value pair into a hive map?

Tags:

hive

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.

like image 302
merlin2011 Avatar asked Aug 20 '13 20:08

merlin2011


People also ask

Is insert possible in Hive?

ACID properties are supported in hive post 0.14 version.So yes insert ,update delete is possible but for single row and single condition operations.cwiki.apache.org/confluence/display/Hive/…

How do I manually add data to Hive?

Hive provides multiple ways to add data to the tables. We can use DML(Data Manipulation Language) queries in Hive to import or add data to the table. One can also directly put the table into the hive with HDFS commands. In case we have data in Relational Databases like MySQL, ORACLE, IBM DB2, etc.

What is map data type in Hive?

Map – a complex data type in Hive which can store Key-Value pairs. Values from a map can be accessed using the keys.


2 Answers

Consider you have a student table which contains student marks in various subjects.

hive> desc student;
id                      string
name                    string
class                    string
marks                   map<string,string>

You can insert values directly to table as below.

INSERT INTO TABLE student
SELECT STACK(1,
'100','Sekar','Mathematics',map("Mathematics","78")
)
FROM empinfo 
LIMIT 1;

Here 'empinfo' table can be any table in your database. And Results are:

100     Sekar   Mathematics     {"Mathematics":"78"}
like image 64
Ranjith Sekar Avatar answered Oct 25 '22 21:10

Ranjith Sekar


for key-value pairs, you can insert like following sql:

INSERT INTO TABLE student values( "id","name",'class',

map("key1","value1","key2","value2","key3","value3","key4","value4") )

please pay attention to sequence of the values in map.

like image 34
DEREK.WANG Avatar answered Oct 25 '22 23:10

DEREK.WANG