2017-11-17 1 views
0

직원, 균형, 날짜 및 직원의 이름을 포함하는 데이터 집합에 별도의 순위 번호가 있습니다.스파크의 순위에 따라 데이터 집합을 분리하십시오

df.show(); 
+------------+----------+-------+----+ 
| Employee|  date|balance|rank| 
+------------+----------+-------+----+ 
|  A  |2016-02-05| 2143| 1| 
|  A  |2016-07-05| 231| 2| 
|  A  |2016-08-05| 447| 3| 
|  A  |2017-10-05| 779| 4| 
|  A  |2018-03-05| 255| 5| 
|  A  |2018-05-05| 246| 6| 
|  A  |2018-08-05| 378| 7| 
|  A  |2018-11-05| 10635| 8| 
|  A  |2019-06-05|  49| 9| 
|  A  |2020-02-05|  0| 10| 
|  A  |2020-04-05| 244| 11| 
|  A  |2020-05-05|  0| 12| 
|  A  |2020-09-05| 424| 13| 
|  C  |2016-05-05| 1506| 1| 
|  C  |2017-06-05|  52| 2| 
|  C  |2017-09-05| 723| 3| 
|  C  |2017-11-05|  23| 4| 
+------------+----------+-------+----+ 

이 데이터 세트는 순위별로 분리해야합니다. 나의 예상 출력 내가이 순위를 얻기를위한 윈도우 함수를 사용하지만이 같은 별도의 테이블을 얻을 수있는 방법하지 않았다

table1 
+------------+----------+-------+----+ 
| Employee|  date|balance|rank| 
+------------+----------+-------+----+ 
|  A  |2016-02-05| 2143| 1| 
|  A  |2016-07-05| 231| 2| 
|  A  |2016-08-05| 447| 3| 
|  A  |2017-10-05| 779| 4| 
|  A  |2018-03-05| 255| 5| 
|  A  |2018-05-05| 246| 6| 
|  A  |2018-08-05| 378| 7| 
|  A  |2018-11-05| 10635| 8| 
|  A  |2019-06-05|  49| 9| 
|  A  |2020-02-05|  0| 10| 
|  A  |2020-04-05| 244| 11| 
|  A  |2020-05-05|  0| 12| 
|  A  |2020-09-05| 424| 13| 
+------------+----------+-------+----+ 


table2 

+------------+----------+-------+----+ 
| Employee|  date|balance|rank| 
+------------+----------+-------+----+ 
|  C  |2016-05-05| 1506| 1| 
|  C  |2017-06-05|  52| 2| 
|  C  |2017-09-05| 723| 3| 
|  C  |2017-11-05|  23| 4| 
+------------+----------+-------+----+ 

입니다. spark 2.0.0과 java를 사용하고 있습니다.

//Getting the distinct columns 
List<Row> distinctColumns = df.select("Employee").distinct().collectAsList(); 

//Initializing empty list for the new DataFrames 
ArrayList<Dataset<Row>> newDFs = new ArrayList<>(); 

WindowSpec ws = Window.orderBy("date"); 

//Filtering by the distinct column values and adding to the list. 
for (Row distinctColumn : distinctColumns) { 
    String colName = distinctColumn.getString(0); 

    newDFs.add(
      df.filter(col("Employee").$eq$eq$eq(colName)) 
        .withColumn("rank", rank().over(ws)) 
    ); 
} 

// show all the new DFs 
for (Dataset<Row> aDF : newDFs) { 
    aDF.show(); 
} 
+1

그래서 기본적으로 당신은 각각의 고유 직원에 대한 별도의 데이터 세트를 원하는을 : 여기

 WindowSpec ws = Window.partitionBy(Employee).orderBy(date); Column rowNum = functions.row_number().over(ws); data.withColumn("rank", rank().over(ws)) 

+0

나는 이것을 시험해 보도록하겠습니다. – Aaryan

+0

나는 데이터 집합 에 의해 별개의 값을 얻었다. df2 = data.select (df) .distinct(); ' 이제 이것을 df (Original dataset)로 필터링해야한다. – Aaryan

답변

1

은 직원의 고유 값에 대한 필터링하여,이를 달성하기 샘플 코드인가? 이 작업을 수행하려면 먼저 고유 한 직원 값을 가져온 다음 각 직원 값에 대해 원래 데이터 집합에서 필터를 수행하여 데이터 집합을 가져옵니다. 나중에 각 분리 된 데이터 세트에서 순위를 매길 수 있습니다.
+0

고마워요 .... !!! – Aaryan

관련 문제