How to get the values in DataFrame with the correct DataType?

前端 未结 2 593
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 05:49

When I tried to get some values in a DataFrame, like:

df.select(\"date\").head().get(0) // type: Any

The result type is Any<

2条回答
  •  野性不改
    2021-01-27 06:54

    Scala is a statically typed language. so the get method defined on the Row can only return values with a single type because the return type of the get method is Any. It cannot return Int for one call and a String for another.

    you should be calling the getInt, getDate and other get methods provided for each type. Or the getAs method in which you can pass the type as a parameter (for example row.getAs[Int](0)).

    As mentioned in the comments other options are

    • use Dataset instead of a DataFrame.
    • use Spark SQL

提交回复
热议问题