I am trying a very basic thing in spark scala, but couldn't figure it out.
I have data like this:
| col1 | col2 |
|---|---|
| John | 1 |
| Jack | 2 |
And, I want to achieve this:
| col1 | col2 |
|---|---|
| John | 1 |
| John | 0 |
| John | 2 |
| Jack | 2 |
| Jack | 1 |
| Jack | 3 |
That is, for each row, I want to create two more rows, one is val(col2)-1 and val(col2)+1.
I tried to use explode, but couldn't figure out how to do it properly.
val exploded_df = df.withColumn("col2", explode($"col2" -1, $"col2", $"col2" +1 ))
And, got:
too many arguments for method explode: (e: org.apache.spark.sql.Column)org.apache.spark.sql.Column