4

类似于:

order_items.groupBy("order_item_order_id").count().orderBy(desc("count")).show()

我努力了:

order_items.groupBy("order_item_order_id").sum("order_item_subtotal").orderBy(desc("sum")).show()

但这给出了一个错误:

Py4JJavaError:调用 o501.sort 时出错。: org.apache.spark.sql.AnalysisException: 给定输入列 order_item_order_id, SUM(order_item_subtotal#429);

我也试过:

order_items.groupBy("order_item_order_id").sum("order_item_subtotal").orderBy(desc("SUM(order_item_subtotal)")).show()

但我得到同样的错误:

Py4JJavaError:调用 o512.sort 时出错。:org.apache.spark.sql.AnalysisException:给定输入列order_item_order_id,SUM(order_item_subtotal#429),无法解析“SUM(order_item_subtotal)”;

执行时我得到正确的结果:

order_items.groupBy("order_item_order_id").sum("order_item_subtotal").orderBy(desc("SUM(order_item_subtotal#429)")).show()

但这是在看到 Spark 附加到总和列名称后的数字后完成的,即#429

有没有办法在不知道将附加哪个数字的情况下获得相同的结果但先验?

4

1 回答 1

8

您应该为您的列使用别名:

import pyspark.sql.functions as func

order_items.groupBy("order_item_order_id")\
           .agg(func.sum("order_item_subtotal")\
                .alias("sum_column_name"))\
           .orderBy("sum_column_name")
于 2015-12-21T12:07:42.267 回答