SQL SPARK-使用2个数据帧创建查询
我有2个数据帧,加载了以下结构 我的第一个数据帧称为dfBooksSQL SPARK-使用2个数据帧创建查询,sql,apache-spark,pyspark,Sql,Apache Spark,Pyspark,我有2个数据帧,加载了以下结构 我的第一个数据帧称为dfBooks +----------+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+--------------------+ | ISBN| Book-Title| Book-Author|Yea
+----------+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+--------------------+
| ISBN| Book-Title| Book-Author|Year-Of-Publication| Publisher| Image-URL-S| Image-URL-M| Image-URL-L|
+----------+--------------------+--------------------+-------------------+--------------------+--------------------+--------------------+--------------------+
|0195153448| Classical Mythology| Mark P. O. Morford| 2002|Oxford University...|http://images.ama...|http://images.ama...|http://images.ama...|
|0002005018| Clara Callan|Richard Bruce Wright| 2001|HarperFlamingo Ca...|http://images.ama...|http://images.ama...|http://images.ama...|
我的第二个是DFU评级
+-------+----------+-----------+
|User-ID| ISBN|Book-Rating|
+-------+----------+-----------+
| 276725|034545104X| 0|
| 276726|0155061224| 5|
| 276727|0446520802| 0|
| 276729|052165615X| 3|
| 276729|0521795028| 6|
| 276733|2080674722| 0|
| 276736|3257224281| 8|
| 276737|0600570967| 6|
| 276744|038550120X| 7|
| 276745| 342310538| 10|
| 276746|0425115801| 0|
| 276746|0449006522| 0|
| 276746|0553561618| 0|
| 276746|055356451X| 0|
| 276746|0786013990| 0|
| 276746|0786014512| 0|
| 276747|0060517794| 9|
| 276747|0451192001| 0|
| 276747|0609801279| 0|
| 276747|0671537458| 9|
+-------+----------+-----------+
问题,获取每个发布者的最大速率
我计算它的想法是使用groupBy函数按ISBN对每个数据帧进行分组,但我认为这应该是一种更好的方法
这是我加载dfs的代码
def crearDataFrame(nombre_fichero):
df = spark.read.format("csv").option("header", "true").option("delimiter", ";").load(nombre_fichero)
return df
数据帧加载:
dfUser = crearDataFrame("BX-Users.csv")
dfBooks = crearDataFrame("BX-Book.csv")
我的想法是将每个查询分组以创建正确的查询
df_ = dfUser.join(dfBooks, dfUser.ISBN == dfBooks.ISBN, 'inner').show()
您知道是否可以同时使用两个df创建查询吗?
谢谢大家。这应该计算出每个出版商的最高评级
dfBooks
.select("ISBN", "Publisher")
.join(dfBooks_Rating, Seq("ISBN"))
.groupBy("Publisher")
.agg(max($"Book-Rating") as "maxRating")
.show
你的问题是什么?为什么不使用
groupBy
进行聚合?我刚刚更新了我的问题,我有一个错误,对不起