Spark SQL - Create Map from Arrays via map_from_arrays Function

visibility 433 event 2022-07-09 access_time 6 months ago language English
more_vert

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. 

copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts