I try to execute following piece of code but end up getting error again and again.
My code is as given below. I am trying to pass key value pair while joining tables but fail to pass value.
val mainDF = Seq(("1","acv","34","a"),("2","fbg","56","b"),("3","rty","78","c"))
.toDF("id","name","age","DBName")
val deltaDF = Seq(("1","gbj","67","a"),("2","gbj","67","a"),("2","jku","88","b"),("4","jku","88","b"),("5","uuu","12","c"))
.toDF("id","name","age","DBName")
val nameMap = Map("TT" -> "id")
for ((k,i) <- nameMap) {
val updatedDF1 = mainDF.as("main")
.join(deltaDF.as("delta"), $"main.$i" === $"delta.$i" && $"main.DBName" === $"delta.DBName", "outer")
.select(mainDF.columns.map(c => coalesce($"delta.$c", $"main.$c") as c): _*)
println(s"key: $k, value: $i")
}
updatedDF1.show()
And the error:
Error : <console>:30: error: not found: value updatedDF1
updatedDF1.show()
If anyone can help me or suggest different way to do the same.
updatedDF1.show()is out of scope.updateDFis defined inside the loop body, and is not visible outside of it. That is one of several reasons loops are rarely used in scala. There is almost always a better way. I could suggest one in this case, but have trouble understanding what you are trying to do in the first place. What do you want yourupdatedDFto be after the loop?