This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // explode the array column "slice_col" | |
| val arr_explode_df = temp_df.withColumn("result", explode($"slice_col")) | |
| arr_explode_df.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val arr_sort_df = df.withColumn("result", sort_array($"array_col2", asc=false)) | |
| arr_sort_df.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val arr_slice_df = df.withColumn("result", slice($"array_col2", 2, 3)) | |
| arr_slice_df.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val arr_size_df = df.withColumn("result", size($"array_col2")) | |
| arr_size_df.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val arr_shuffle_df = df.withColumn("result", shuffle($"array_col2")) | |
| arr_shuffle_df.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val arr_reverse_df = df.withColumn("result", reverse($"array_col2")) | |
| arr_reverse_df.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val arr_element_at_df = df.withColumn("result", element_at($"array_col2", 1)) | |
| arr_element_at_df.show() |