0

i am trying to add values of two column after converting from string to int using withcolumn in spark sql.

withColumn("totalDowntimetime",("zenerTime").cast(int)+("avalancheTime").cast(int)).show(truncate = false)

but it is throwing error as "

 Error:(36, 44) type mismatch;
     found   : String("zenerTime")
     required: org.apache.spark.sql.Column
        withColumn("totalDowntimetime",("zenerTime").cast(int)+("avalancheTime").cast(int)).show(truncate = false)

Very Much Appreciate your suggestion

1 Answer 1

1

Try refering your columns zenerTime,avalancheTime with col(<column_name>)

  • As required type is column

withColumn("totalDowntimetime",col("zenerTime").cast(int) + col("avalancheTime").cast(int)).show(truncate = false)

Example:

val df=Seq(("1","2")).toDF("zenerTime","avalancheTime")


import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._

df.withColumn("totalDowntimetime",col("zenerTime").cast("int") + col("avalancheTime").cast("int")).show()
/*
+---------+-------------+-----------------+
|zenerTime|avalancheTime|totalDowntimetime|
+---------+-------------+-----------------+
|        1|            2|                3|
+---------+-------------+-----------------+
*/
Sign up to request clarification or add additional context in comments.

2 Comments

not sure why i am getting error as cannot resolve overload method cast.... any specific library , do i need to import.
I have added Example section in the answer try by importing functions and types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.