這在PySpark,這裏有雲:
創建DF:
sqlContext = SQLContext(sc)
data = [(1,'animation'),(1,'pixar'),(1,'animation'),(2,'comedy')]
RDD = sc.parallelize(data)
orders_df = sqlContext.createDataFrame(RDD,["movieid","tag"])
orders_df.show()
+-------+---------+
|movieid| tag|
+-------+---------+
| 1|animation|
| 1| pixar|
| 1|animation|
| 2| comedy|
+-------+---------+
計算:
orders_df.groupBy(['movieid','tag']).count().show() #count for each movie id how many times each tags are applied
+-------+---------+-----+
|movieid| tag|count|
+-------+---------+-----+
| 1| pixar| 1|
| 1|animation| 2|
| 2| comedy| 1|
+-------+---------+-----+
orders_df.groupBy(['movieid']).count().show() #number of tags applied to each movie
+-------+-----+
|movieid|count|
+-------+-----+
| 1| 3|
| 2| 1|
+-------+-----+
感謝help.It工作罰款。相當於Scala代碼是 - orders_df.groupBy( 「movieid」, 「標籤」)。COUNT()。節目() – sasmita