'How to skip the case statement if condition fails (using Java)?
I have written a code which fetches the input file as a dataframe.
I need to copy the value of the particular columns which are present in the dataframe to a new column. for creating a new column i've used 'withcolumn'.
Dataset<Row> df1 = spark.read().format("com.databricks.spark.csv").option("header", "true").load(FilePath);
List<String> h1 = new ArrayList<String>();
h1 = Arrays.asList(df1.columns());
Dataset<Row> df;
df = df1;
for(String field:h1)
{
switch (field)
{
case "A":
df = df1.withColumn("first",df1.col("A"));
case "B":
df = df.withColumn("second",df1.col("B"));
case "C":
df = df.withColumn("third",df1.col("C"));
case "D":
df = df.withColumn("fourth",df1.col("D"));
My concern is how to skip the value case statement if the particular column name is not present.
For example in the above code if case "C" is not present in the dataframe it should skip and go to the next case. In my case it is throwing error like "cannot resolve "c".
Please share some suggestion on how to skip the case or is there any other way to do so. TIA
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
