我有这段代码:
val df = resultsDf
.withColumn("data_exploded", explode(col("data_item")))
.groupBy("data_id","data_count")
.agg(
count(lit(1)).as("aggDataCount"),
sum(when(col("data_exploded.amount")==="A",col("data_exploded.positive_amount")).otherwise(lit(0))).as("aggAmount")
)
lit(0)
是指位置 0 处的索引,还是数字 0
的字面值?我在 https://mungingdata.com/apache-spark/spark-sql-functions/#:~:text=The%20lit()%20function%20creates,spanish_hi%20column%20to%20the%20DataFrame.&text=The%20lit()%20function%20is%20especially%20useful%20when%20making%20boolean%20comparisons 看到的定义说“lit()
函数根据文字值创建一个 Column 对象。”这个定义让我认为它不是指索引位置,而是指文字值,例如数字或字符串。但是,count(lit(1)).as("aggDataCount")
中的用法对我来说看起来像是指列的索引位置。谢谢。
最佳答案
lit(1)
表示文字值1
count(lit(1)).as("aggDataCount")
是一种计算行数的方法(每行都有一个值为 1
并对本栏进行总结)
关于scala - lit(0) 和 lit(1) 在 Scala/Spark 聚合函数中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69090842/