Skip to content

Instantly share code, notes, and snippets.

// explode the array column "slice_col"
val arr_explode_df = temp_df.withColumn("result", explode($"slice_col"))
arr_explode_df.show()
// create an array column with few values to explode.
val temp_df = df.withColumn("slice_col", slice($"array_col2", 1, 2))
.drop("array_col2")
temp_df.show()
val arr_sort_df = df.withColumn("result", sort_array($"array_col2", asc=false))
arr_sort_df.show()
val arr_slice_df = df.withColumn("result", slice($"array_col2", 2, 3))
arr_slice_df.show()
val arr_size_df = df.withColumn("result", size($"array_col2"))
arr_size_df.show()
val arr_shuffle_df = df.withColumn("result", shuffle($"array_col2"))
arr_shuffle_df.show()
val arr_reverse_df = df.withColumn("result", reverse($"array_col2"))
arr_reverse_df.show()
val map_from_arr_df = full_df
.withColumn("result", map_from_arrays($"array_col1", $"array_col2"))
.drop("col1")
map_from_arr_df.show(truncate=false)
// Generate the nested array using the function "array_repeat".
val arr_repeat_df = df.withColumn("repeat", array_repeat($"array_col2", 2))
// flatten the nested array.
val arr_flat_df = arr_repeat_df
.withColumn("result", flatten($"repeat")).select("repeat", "result")
.select("repeat", "result")
arr_flat_df.show(truncate=false)
val arr_element_at_df = df.withColumn("result", element_at($"array_col2", 1))
arr_element_at_df.show()