This file contains 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 row_num_df = empsalary.withColumn("row_number", row_number().over(winSpec)) | |
row_num_df.show() |
This file contains 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 dense_rank_df = empsalary.withColumn("dense_rank", dense_rank().over(winSpec)) | |
dense_rank_df.show() |
This file contains 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 rank_df = empsalary.withColumn("rank", rank().over(winSpec)) | |
rank_df.show() |
This file contains 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 winSpec = Window.partitionBy("depName").orderBy("salary".desc) |
This file contains 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 agg_sal = empsalary | |
.withColumn("max_salary", max("salary").over(byDepName)) | |
.withColumn("min_salary", min("salary").over(byDepName)) | |
agg_sal.select("depname", "max_salary", "min_salary") | |
.dropDuplicates() | |
.show() |
This file contains 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 byDepName = Window.partitionBy("depName") |
This file contains 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
case class Salary(depName: String, empNo: Long, salary: Long) | |
val empsalary = Seq( | |
Salary("sales", 1, 5000), | |
Salary("personnel", 2, 3900), | |
Salary("sales", 3, 4800), | |
Salary("sales", 4, 4800), | |
Salary("personnel", 5, 3500), | |
Salary("develop", 7, 4200), | |
Salary("develop", 8, 6000), |
This file contains 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
import org.apache.spark.sql.expressions.Window | |
import org.apache.spark.sql.functions._ |
This file contains 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.select($"*", posexplode($"slice_col")) | |
arr_explode_df.show(truncate=false) |
This file contains 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") |