0
  • 齊柏林0.6
  • 星火1.6
  • SQL

我試圖找到一些微博的前20名出現的詞。 filtered包含每個推文的單詞數組。以下:如何在Spark SQL中的爆炸字段上做GROUP BY?

select explode(filtered) AS words from tweettable 

列出了每個單詞你所期望的,但我要的是讓每個單詞的數量在所有的鳴叫,然後顯示這些排名前20位。下面的作品,但我需要爲此在SQL:

df.select(explode($"filtered").as("value")) 
    .groupBy("value") 
    .count() 
    .sort(desc("count")) 
    .show(20, false) 

我試圖GROUP BYwordsfiltered,並且explode(filtered)但所有給了錯誤。

+0

你爲什麼改變標題?現在沒有意義。 – schoon

回答

2

可以在FROM語句中使用subqueries

SELECT value, count(*) AS count 
FROM (SELECT explode(filtered) AS value 
     FROM tweettable) AS temp 
GROUP BY value 
ORDER BY count DESC 
0

下面的代碼會給你全面的瞭解,以達到你所期待的東西。測試火花(1.6)

val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc) 
import hiveContext.implicits._ 

val lst = List(Seq("Hello","Hyd","Hello","Mumbai"),Seq("Hello","Mumbai"),Seq("Hello","Delhi","Hello","Banglore")) 
case class Tweets(filtered: Seq[String]) 
val df = sc.parallelize(lst).map(x=>Tweets(x)).toDF 

import org.apache.spark.sql.functions.{explode} 
import org.apache.spark.sql.functions.count 
df.select(explode($"filtered").as("value")).groupBy("value").agg(count("*").alias("cnt")).orderBy('cnt.desc).show(20,false) 

或者,您可以使用窗口功能。

val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc) 
import hiveContext.implicits._ 

val lst = List(Seq("Hello","Hyd","Hello","Mumbai"),Seq("Hello","Mumbai"),Seq("Hello","Delhi","Hello","Banglore")) 
case class Tweets(filtered: Seq[String]) 
val df = sc.parallelize(lst).map(x=>Tweets(x)).toDF 

import org.apache.spark.sql.expressions.Window 
import org.apache.spark.sql.functions._ 
val w = org.apache.spark.sql.expressions.Window.orderBy('cnt.desc) 

df.select(explode($"filtered").as("value")).groupBy("value").agg(count("*").alias("cnt")).withColumn("filteredrank", rank.over(w)).filter(col("filteredrank") <= 20).show() 
+0

Thansk Arvind。採取了mtoto的建議升級,我設法打破了我的集羣的一半。如果我修復它,請嘗試您的解決方案。 – schoon

+0

沒有問題。這兩種解決方案也適用於Spark 2.1版本。 –

+0

@schoon我更新了1.6版本的解決方案。但是,更新仍然是一個好主意。 – mtoto