Dataframe is unreachable outside the if clause. how to access Dataframe outside if/else block
Requirement: DataSource id SQL Server and from structured data prepare JSON Scenario: Based on region value will populate data and will fit it within main dataframe. Problem occurred when I am trying to access dataframe outside the if/else clause. Below is the code sample that I have tried:
if(region == "UK")
{
val dfUK = "select * from tablename"
}
dfuk.withColumnRenamed("ColumnName1","ColumnName2")
**Error: dfuk is unreachable**
CodePudding user response:
Resolution:
- Import the library - import org.apache.spark.sql.DataFrame
- Create blank dataframe:
var outputdf: DataFrame = _
if(region == "UK")
{
val dfUK = "select * from tablename"
outputdf = dfUK
}
outputdf.withColumnRenamed("ColumnName1","ColumnName2")
CodePudding user response:
define it as an expression
val dfUK = if(region == "UK") {
spark.sql("select * from tablename")
} //else something other definition for the df
dfUK.withColumn...