Kontext Kontext

Spark SQL - Create Map from Arrays via map_from_arrays Function

event 2022-07-09 visibility 1,970 comment 0 insights toc
more_vert
insights Stats
toc Table of contents

Spark SQL function map_from_arrays(col1, col2) returns a new map from two arrays. The two arrays can be two columns of a table. The two columns need to be array data type. 

Code snippets

The following are some examples using this function in Spark SQL:

spark-sql> SELECT map_from_arrays(t.key, t.value) as map FROM VALUES
         > (array('a','b'),array(1,2)),
         > (array('a','b'),array(100,200))
         > AS t(key, value);
{"a":1,"b":2}
{"a":100,"b":200}

The above script creates a table from values with columns named as key and value

The result prints out two rows of MapType object. 

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts