[OK]を、テストデータ:
import org.apache.spark.sql.types._
import org.apache.spark.sql._
import sqlContext.implicits._
val list = Seq((1, "topic1", Array("a", "b", "c", "b")), (2, "topic1", Array("b", "c", "r")), (3, "topic2", Array("e", "b", "c", "e")), (4, "topic2", Array("b", "c", "e", "r")))
val df = sc.parallelize(list).toDF("id", "topic", "entities");
は、各トピックに、各タイプのものであるか多くの企業の計算:
df
.withColumn("entity", explode('entities))
.groupBy('topic, 'entity)
.count()
.show();
結果は次のようになります。
+------+------+-----+
| topic|entity|count|
+------+------+-----+
|topic1| a| 1|
|topic1| b| 3|
|topic1| c| 2|
|topic1| r| 1|
|topic2| b| 2|
|topic2| c| 2|
|topic2| e| 3|
|topic2| r| 1|
+------+------+-----+
どのように多くの異なるエンティティ各トピックがある:
df
.withColumn("entity", explode('entities))
.groupBy('topic)
.agg(countDistinct('entity))
.show();
結果は次のようになります。あなたは、各エンティティの列の多くの行を作成する機能を爆発使うべき答え
の
+------+-------------+
| topic|count(entity)|
+------+-------------+
|topic1| 4|
|topic2| 4|
+------+-------------+
キー。エンティティは、Bは= Cは確かに3行
が作成されます。すなわち、ID = 1、エンティティは、B、Cを=のでエンティティは、リストです。 id = 2、エンティティ:b、e、f、r? –
はい、正確には – amaik