에 기초한 평균 계산 I 두 dataframes 있습니다분할 스파크 dataframe 한 열 값
Class, Calculation
first, Average
Second, Sum
Third, Average
둘째 dataframe studentRecord
는 다음과 같이 약 50K 엔트리 갖는다 :
classRecord
는 다음과 같이 10 개 항목이
Name, height, Camp, Class
Shae, 152, yellow, first
Joe, 140, yellow, first
Mike, 149, white, first
Anne, 142, red, first
Tim, 154, red, Second
Jake, 153, white, Second
Sherley, 153, white, Second
클래스 유형에 따라 두 번째 데이터 프레임부터 캠프를 따로 따로 높이 (클래스 우선 : 평균, 클래스 두 번째 : 합계 등)에 대한 계산을 수행하고 싶습니다 (클래스가 전나무 평균, 흰색, 노란색, 흰색 등). 여기
//function to calculate average
def averageOnName(splitFrame : org.apache.spark.sql.DataFrame) : Array[(String, Double)] = {
val pairedRDD: RDD[(String, Double)] = splitFrame.select($"Name",$"height".cast("double")).as[(String, Double)].rdd
var avg_by_key = pairedRDD.mapValues(x => (x, 1)).reduceByKey((x, y) => (x._1 + y._1, x._2 + y._2)).mapValues(y => 1.0 * y._1/y._2).collect
return avg_by_key
}
//required schema for further modifications
val schema = StructType(
StructField("name", StringType, false) ::
StructField("avg", DoubleType, false) :: Nil)
// for each loop on each class type
classRecord.rdd.foreach{
//filter students based on camps
var campYellow =studentRecord.filter($"Camp" === "yellow")
var campWhite =studentRecord.filter($"Camp" === "white")
var campRed =studentRecord.filter($"Camp" === "red")
// since I know that calculation for first class is average, so representing calculation only for class first
val avgcampYellow = averageOnName(campYellow)
val avgcampWhite = averageOnName(campWhite)
val avgcampRed = averageOnName(campRed)
// union of all
val rddYellow = sc.parallelize (avgcampYellow).map (x => org.apache.spark.sql.Row(x._1, x._2.asInstanceOf[Number].doubleValue()))
//conversion of rdd to frame
var dfYellow = sqlContext.createDataFrame(rddYellow, schema)
//union with yellow camp data
val rddWhite = sc.parallelize (avgcampWhite).map (x => org.apache.spark.sql.Row(x._1, x._2.asInstanceOf[Number].doubleValue()))
//conversion of rdd to frame
var dfWhite = sqlContext.createDataFrame(rddWhite, schema)
var dfYellWhite = dfYellow.union(dfWhite)
//union with yellow,white camp data
val rddRed = sc.parallelize (avgcampRed).map (x => org.apache.spark.sql.Row(x._1, x._2.asInstanceOf[Number].doubleValue()))
//conversion of rdd to frame
var dfRed = sqlContext.createDataFrame(rddRed, schema)
var dfYellWhiteRed = dfYellWhite .union(dfRed)
// other modifications and final result to hive
}
내가 고민하고 있습니다 :
1.hardcoding Yellow, red and white, there may be other camp type also.
2. Filtering same dataframe many times
3. Not able to figure out how to calculate differently according to class calculation type.
도움말 감사 나는 다음 시도했다. 감사.
정확하게 이해한다면 캠프와 수업 모두에 따라 평균 높이 또는 합계를 원하십니까? 캠프/클래스의 모든 조합에 대해 두 가지를 모두 계산하고,이를 데이터 프레임에 넣은 다음 'classRecord' df를 별도로 읽는 것은 어떻습니까? – Shaido