Built with Tutify.io

    AIStudyLabs

    Map Functions

    Map Functions

    Introduction

    Spark SQL provides built-in standard map functions defined in the DataFrame API, which come in handy when you need to perform operations on map (MapType) columns. All these functions accept input as a map column and several other arguments based on the functions.

    Spark SQL map functions are grouped as “collection_funcs” in Spark SQL along with several array functions. These map functions are useful when you want to concatenate two or more map columns, convert arrays of StructType entries to a map column, etc.

    | Function | Description | | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | map_keys | Returns an array containing the keys of the map. | | map_values | Returns an array containing the values of the map. | | map_concat | Merges maps specified in the arguments. | | map_from_entries | Returns a map from the given array of StructType entries. | | map_entries | Returns an array of all StructType in the given map. | | explode() | Creates a new row for every key-value pair in the map by ignoring null & empty values. It creates two new columns, one for key and one for value. | | explode_outer() | Creates a new row for every key-value pair in the map, including null & empty values. It creates two new columns, one for key and one for value. | | posexplode() | Creates a new row for each key-value pair in a map by ignoring null & empty values. It also creates 3 columns: “pos” to hold the position of the map element, and “key” and “value” columns for every row. | | posexplode_outer() | Creates a new row for each key-value pair in a map, including null & empty values. It also creates 3 columns: “pos” to hold the position of the map element, and “key” and “value” columns for every row. | | transform_keys() | Transforms a map by applying functions to every key-value pair and returns a transformed map. | | transform_value() | Transforms a map by applying functions to every key-value pair and returns a transformed map. | | map_zip_with() | Merges two maps into a single map. | | element_at() | Returns a value of a key in a map. | | size() | Returns the length of a map column. |

    Example: Using map_keys and map_values

    from pyspark.sql import SparkSession
    from pyspark.sql.functions import map_keys, map_values
    
    spark = SparkSession.builder.master("local").appName("example").getOrCreate()
    
    data = [
      (1, {"key1": "value1", "key2": "value2"}),
      (2, {"key3": "value3", "key4": "value4"})
    ]
    
    df = spark.createDataFrame(data, ["id", "map"])
    df.select("id", map_keys("map").alias("keys"), map_values("map").alias("values")).show(truncate=False)
    
    +---+----------+----------+
    |id |keys      |values    |
    +---+----------+----------+
    |1  |[key1,key2]|[value1,value2]|
    |2  |[key3,key4]|[value3,value4]|
    +---+----------+----------+
    

    Example: Using map_concat

    from pyspark.sql.functions import map_concat
    
    df1 = spark.createDataFrame([
      (1, {"key1": "value1"}),
      (2, {"key2": "value2"})
    ], ["id", "map1"])
    
    df2 = spark.createDataFrame([
      (1, {"key3": "value3"}),
      (2, {"key4": "value4"})
    ], ["id", "map2"])
    
    df = df1.join(df2, "id")
    df.withColumn("map_concat", map_concat("map1", "map2")).show(truncate=False)
    
    +---+----------+----------+--------------------------+
    |id |map1      |map2      |map_concat                |
    +---+----------+----------+--------------------------+
    |1  |{key1: value1}|{key3: value3}|{key1: value1, key3: value3}|
    |2  |{key2: value2}|{key4: value4}|{key2: value2, key4: value4}|
    +---+----------+----------+--------------------------+
    

    Example: Using map_from_entries

    from pyspark.sql.functions import map_from_entries
    
    data = [
      (1, [("key1", "value1"), ("key2", "value2")]),
      (2, [("key3", "value3"), ("key4", "value4")])
    ]
    
    df = spark.createDataFrame(data, ["id", "entries"])
    df.withColumn("map", map_from_entries("entries")).show(truncate=False)
    
    +---+------------------------+-------------------------+
    |id |entries                 |map                      |
    +---+------------------------+-------------------------+
    |1  |[[key1,value1],[key2,value2]]|{key1: value1, key2: value2}|
    |2  |[[key3,value3],[key4,value4]]|{key3: value3, key4: value4}|
    +---+------------------------+-------------------------+
    

    Example: Using posexplode

    from pyspark.sql.functions import posexplode
    
    data = [
      (1, {"key1": "value1", "key2": "value2"}),
      (2, {"key3": "value3", "key4": "value4"})
    ]
    
    df = spark.createDataFrame(data, ["id", "map"])
    df.select("id", posexplode("map")).show(truncate=False)
    
    +---+---+----+------+
    |id |pos|key |value |
    +---+---+----+------+
    |1  |0  |key1|value1|
    |1  |1  |key2|value2|
    |2  |0  |key3|value3|
    |2  |1  |key4|value4|
    +---+---+----+------+
    

    Introduction to PySpark for Data Engineering — PySpark SQL Functions

    Introduction to PySpark

    Setup PySpark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Apache Spark Concepts

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    PySpark SQL Functions

    Advanced DataFrame Features

    PySpark SQL

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    What Next?

    Tutorial Not Found