Created
January 9, 2020 09:38
-
-
Save raidery/36b647bce3cd65dc6448891dea6cf1ed to your computer and use it in GitHub Desktop.
Spark SQL: apply aggregate functions to a list of columns
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
//https://stackoverflow.com/questions/33882894/spark-sql-apply-aggregate-functions-to-a-list-of-columns | |
val Claim1 = StructType(Seq(StructField("pid", StringType, true),StructField("diag1", StringType, true),StructField("diag2", StringType, true), StructField("allowed", IntegerType, true), StructField("allowed1", IntegerType, true))) | |
val claimsData1 = Seq(("PID1", "diag1", "diag2", 100, 200), ("PID1", "diag2", "diag3", 300, 600), ("PID1", "diag1", "diag5", 340, 680), ("PID2", "diag3", "diag4", 245, 490), ("PID2", "diag2", "diag1", 124, 248)) | |
val claimRDD1 = sc.parallelize(claimsData1) | |
val claimRDDRow1 = claimRDD1.map(p => Row(p._1, p._2, p._3, p._4, p._5)) | |
val claimRDD2DF1 = sqlContext.createDataFrame(claimRDDRow1, Claim1) | |
val l = List("allowed", "allowed1") | |
val exprs = l.map((_ -> "sum")).toMap | |
claimRDD2DF1.groupBy("pid").agg(exprs) show false | |
val exprs = Map("allowed" -> "sum", "allowed1" -> "avg") | |
claimRDD2DF1.groupBy("pid").agg(exprs) show false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment