gpt4 book ai didi

apache-spark - Spark SQL 中的分组依据和排序依据

转载 作者:行者123 更新时间:2023-12-04 04:04:47 24 4
gpt4 key购买 nike

我正在尝试使用 spark 应用程序访问 s3 数据。我正在应用 Spark SQL 来检索数据。它不采用 group by 子句。

DataFrame summaryQuery=sql.sql("Select score from summary order by updationDate desc);
summaryQuery.groupBy("sessionId").count().show();
summaryQuery.show();

我也直接尝试
    DataFrame summaryQuery=sql.sql("Select score from summary group by sessionId order by updationDate desc);
summaryquery.show();

但在这两种情况下,我都收到 SQL 异常。
Exception in thread "main" org.apache.spark.sql.AnalysisException: expression 'score' is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() (or first_value) if you don't care which value you get.;

请指定我如何查询数据。

最佳答案

在 Spark SQL 中,当 group by 子句中不存在 column_name 时,我们必须将其包装在函数 first(column_name) 或 last(column_name) 或任何聚合函数中。它将分别从获取的行中获取第一个或最后一个值。

DataFrame summaryQuery=sql.sql("Select first(score) from summary group by sessionId order by updationDate desc);
summaryquery.show();

关于apache-spark - Spark SQL 中的分组依据和排序依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40154617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com