2016-11-23 8 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") 

もっと良い解決策はありますか? 私の悪い英語をexecuseしてください.. ^^;

事前に感謝..

+0

おかげではなく、 'NULL'値は' cube'操作@CarlosVilchezによって生成されました... –

答えて

4

私はあなたが問題を完全に避けることができないと信じていますが、その規模を縮小することができ、簡単なトリックがあります。考え方は、疎外されるべきではないすべての列を1つのプレースホルダに置き換えることです。例えば

お持ちの場合は 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| 
+---+---+---+----+----+-----+ 
関連する問題