Aarti Agarwal
Aarti Agarwal

Reputation: 49

Type Mismatch while converting string to int in spark sql

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

Upvotes: 0

Views: 1457

Answers (1)

notNull
notNull

Reputation: 31540

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|
+---------+-------------+-----------------+
*/

Upvotes: 1

Related Questions