scala Spark - 从 DataFrame 中提取单个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31961971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:27:02  来源:igfitidea点击:

Spark - extracting single value from DataFrame

scalaapache-sparkapache-spark-sql

提问by Niemand

I have a Spark DataFrame query that is guaranteed to return single column with single Int value. What is the best way to extract this value as Int from the resulting DataFrame?

我有一个 Spark DataFrame 查询,它保证返回具有单个 Int 值的单列。从生成的 DataFrame 中将该值提取为 Int 的最佳方法是什么?

回答by kostya

You can use head

您可以使用 head

df.head().getInt(0)

or first

或者 first

df.first().getInt(0)

Check DataFramescala docs for more details

查看DataFramescala 文档以获取更多详细信息

回答by Till Rohrmann

This could solve your problem.

这可以解决您的问题。

df.map{
    row => row.getInt(0)
}.first()