2016-11-23 24 views
6

我正在使用spark 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| 
...... 

而且我想通過使用多維數據集操作獲得如下數據框。

(所有領域分組,但只有「OS_NAME」,「國家」,「app_ver」字段立方)

+-------------+-----------+-----------------+-------+-------+-------+----------+-------+-------+-------+-------+---+ 
|  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| 
...... 

我試圖像下面,但它似乎是緩慢的和醜陋..

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

謝謝,但'由'cube'操作@CarlosVilchez產生NULL'值... –

回答

4

我相信你不能完全避免的問題,但有一個簡單的技巧就可以減少其規模。這個想法是用一個佔位符替換所有不應被邊緣化的列。

例如,如果你有一個DataFrame

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

和你感興趣的立方體由de邊緣化和分組由a..c可以定義替代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") 

and 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| 
+---+---+---+----+----+-----+ 
相關問題