2016-06-21 273 views

回答

18

countDistinct可能是第一選擇:

import org.apache.spark.sql.functions.countDistinct 

df.agg(countDistinct("some_column")) 

如果速度比精度更重要的,你可以考慮approxCountDistinct

import org.apache.spark.sql.functions.approx_count_distinct 

df.agg(approxCountDistinct("some_column")) 

爲了獲取值和計數:

df.groupBy("some_column").count() 

在SQL中(spark-sql):

SELECT COUNT(DISTINCT some_column) FROM df 

SELECT approx_count_distinct(some_column) FROM df 
5
df.select("some_column").distinct.count 
+0

這是否告訴你,每一個不同的值怎麼算?我認爲這會告訴你,你有X值,而不是Val1有A,Val2有B,ValX有C? –

相關問題