나는 불꽃 스칼라를 사용 dataframes에 다음과 같은 시나리오를 구현해야합니다스파크 Dataframe GROUPBY 및 복잡한 경우 문 유도
Scenarios-1: If the "KEY" exist one time, take the "TYPE_VAL" as is .
Eg: KEY=66 exist once so take the TYPE_VAL=100
Scenarios-2: If the "KEY" exist more than one time, Check for the same TYPE_VAL, if it is same, then take TYPE_VAL once .
Eg: for KEY=68,so TYPE_VAL=23
Scenarios-3: If the "KEY" exist more than one time, Check for the same TYPE_VAL and subtract the other TYPE_VAL.
Eg: for KEY=67 , TYPE_VAL=10 exists twice,so subtract 2 & 4 from 10, finally TYPE_VAL=4
나는 같은 키에 의해 그룹을 사용했지만, 모든 시나리오를 도출 할 수없는 한
//Sample Input Values
val values = List(List("66","100") ,
List("67","10") , List("67","10"),List("67","2"),List("67","4")
List("68","23"),List("68","23")).map(x =>(x(0), x(1)))
import spark.implicits._
//created a dataframe
val df1 = values.toDF("KEY","TYPE_VAL")
df1.show(false)
------------------------
KEY |TYPE_VAL |
------------------------
66 |100 |
67 |10 |
67 |10 |
67 |2 |
67 |4 |
68 |23 |
68 |23 |
-------------------------
예상 출력 :
df2.show(false)
------------------------
KEY |TYPE_VAL |
------------------------
66 |100 | -------> [single row ,so 100]
67 |4 | -------> [four rows,out of which two are same & rest are diffrent, so (10 - 2 - 4) = 4 ]
68 |23 | -------> [two rows with same values, so 23]
-------------------------