2017-03-13 5 views
0

Spark 2.0を使用して実装する最良の方法は次のとおりですか?Apache Sparkカスタム集約関数

アグリゲータ? UDAF?たぶん既にsparksqlの機能に似ているのでしょうか?

select 
    x, 
    sum(case when label='1' then 1 else 0 end) as p1, 
    sum(1) as cnt 
from 
    df 
group by 
    x 

データ:

x,label 
--- 
a,1 
a,1 
a,0 
b,1 
b,0 
b,0 
c,0 
c,1 
c,0 

答えて

0
from pyspark.sql import functions as f 

df.groupBy(['x']).agg(f.sum(f.when(df['label'] == '1', 1).otherwise(0)), f.count(df['x'])) 
+3

が答えにもう少し詳しく説明してください。 (少なくとも短い)説明なしでコードを書くだけでは通常十分ではありません。 – Sefe