2016-11-23 8 views
6

나는 스파크 1.6.1을 사용하고 있으며, 나는 그러한 데이터 프레임을 가지고있다.스파크 데이터 프레임의 특정 필드에만 "큐브"를 사용하는 방법은 무엇입니까?

+-------------+-----------+-----------------+-------+-------+-------+----------+-------+-------+-------+-------+ 
|  scene_id| action_id|  classifier|os_name|country|app_ver| p0value|p1value|p2value|p3value|p4value| 
+-------------+-----------+-----------------+-------+-------+-------+----------+-------+-------+-------+-------+ 
| test_home|scene_enter|  test_home|android|  KR| 5.6.3|__OTHERS__| false| test| test| test| 
...... 

그리고 큐브 작업을 사용하여 다음과 같은 데이터 프레임을 얻고 싶습니다.

나는 다음과 같은 시도

+-------------+-----------+-----------------+-------+-------+-------+----------+-------+-------+-------+-------+---+ 
|  scene_id| action_id|  classifier|os_name|country|app_ver| p0value|p1value|p2value|p3value|p4value|cnt| 
+-------------+-----------+-----------------+-------+-------+-------+----------+-------+-------+-------+-------+---+ 
| test_home|scene_enter|  test_home|android|  KR| 5.6.3|__OTHERS__| false| test| test| test| 9| 
| test_home|scene_enter|  test_home| null|  KR| 5.6.3|__OTHERS__| false| test| test| test| 35| 
| test_home|scene_enter|  test_home|android| null| 5.6.3|__OTHERS__| false| test| test| test| 98| 
| test_home|scene_enter|  test_home|android|  KR| null|__OTHERS__| false| test| test| test|101| 
| test_home|scene_enter|  test_home| null| null| 5.6.3|__OTHERS__| false| test| test| test|301| 
| test_home|scene_enter|  test_home| null|  KR| null|__OTHERS__| false| test| test| test|225| 
| test_home|scene_enter|  test_home|android| null| null|__OTHERS__| false| test| test| test|312| 
| test_home|scene_enter|  test_home| null| null| null|__OTHERS__| false| test| test| test|521| 
...... 

(모든 필드에 의해 그룹화,하지만 "OS_NAME", "국가", "app_ver"필드는 제곱입니다)하지만, 천천히 그리고 추한 것 같다 ..

var cubed = df 
    .cube($"scene_id", $"action_id", $"classifier", $"country", $"os_name", $"app_ver", $"p0value", $"p1value", $"p2value", $"p3value", $"p4value") 
    .count 
    .where("scene_id IS NOT NULL AND action_id IS NOT NULL AND classifier IS NOT NULL AND p0value IS NOT NULL AND p1value IS NOT NULL AND p2value IS NOT NULL AND p3value IS NOT NULL AND p4value IS NOT NULL") 

더 좋은 해결책이 있습니까? 내 나쁜 영어를 제발 제발 .. ^^; 사전에

감사합니다 ..

+0

감사하지만'NULL' 값은'cube' 조작 @CarlosVilchez에 의해 생성 된 ... –

답변

4

는 당신이 문제를 완전히 피할 수 있지만 그 규모를 줄일 수있는 간단한 트릭이있다 생각합니다. 이 아이디어는 소외되어서는 안되는 모든 열을 하나의 자리 표시 자로 대체하는 것입니다. 예를 들어

당신이 경우 DataFrame :

val df = Seq((1, 2, 3, 4, 5, 6)).toDF("a", "b", "c", "d", "e", "f") 

당신은 당신이 대체를 정의 할 수 있습니다 a..c에 의해 큐브 de에 의해 소외 및 그룹화에 관심이 a..c 등 :

import org.apache.spark.sql.functions.struct 
import sparkSql.implicits._ 

// alias here may not work in Spark 1.6 
val rest = struct(Seq($"a", $"b", $"c"): _*).alias("rest") 

cube :

val cubed = Seq($"d", $"e") 

// If there is a problem with aliasing rest it can done here. 
val tmp = df.cube(rest.alias("rest") +: cubed: _*).count 

빠른 필터하고 나머지는 처리해야 선택 : 같은 결과

tmp.where($"rest".isNotNull).select($"rest.*" +: cubed :+ $"count": _*) 

:

+---+---+---+----+----+-----+ 
| a| b| c| d| e|count| 
+---+---+---+----+----+-----+ 
| 1| 2| 3|null| 5| 1| 
| 1| 2| 3|null|null| 1| 
| 1| 2| 3| 4| 5| 1| 
| 1| 2| 3| 4|null| 1| 
+---+---+---+----+----+-----+