In [2]:
import org.apache.spark.sql.SparkSession
val spark = SparkSession.builder().master("local[2]")
.appName("Nested")
.getOrCreate()
spark
Out[2]:
In [8]:
import spark.implicits._
import org.apache.spark.sql.functions._
Out[8]:
In [6]:
val df = Seq(
(8, "bat"),
(64, "mouse"),
(-27, "horse")
).toDF("number", "words")
df.show
Out[6]:
In [9]:
df.withColumn("n2", {
val tempCol = df("number") / 4
when(tempCol > 0, tempCol).otherwise(0)
}).show
In [10]:
df.withColumn("n2", {
val tempCol = $"number" / 4
when(tempCol > 0, tempCol).otherwise(0)
}).show
In [11]:
df.withColumn("n2", {
val tempCol = $"number" / 4
when(tempCol > 0, tempCol).otherwise(0)
}).withColumn("n3", {
val tempCol = $"n2" / 2
when(tempCol < 2, tempCol + 1).otherwise(tempCol)
}).show
In [ ]: