Spark SQL - Create Map from Arrays via map_from_arrays Function

Kontext Kontext 0 2379 2.19 index 7/9/2022

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.

spark-sql-function

Join the Discussion

View or add your thoughts below

Comments