I am a newbie to scala (and functional programming, basically). I am trying to loop over a list of rows (can think as strings), where each string will be passed to a different scala method, where I am doing some manipulation on the input string and then return a string to the for-loop.
below is not the working code, but this is what I am expecting to work.
val input_list = spark.read
.format("com.crealytics.spark.excel")
.option("sheetName" , "SchemaInfo")
.option("useHeader", "true")
.schema(profilerSchema)
.load(path) // this is spark dataframe, which has rows.
val columnNames : List[String] = new List("Hello" , "world");
var outputList = new ListBuffer[String]();
// Here i am iterating the input_list where i pass each ele to getString() method where
// it returns the final string which i want to add to outputList.
input_list.foreach(i => {
val res: String = getString(row, columnNames)
outputList += res;
}));
def getString(row: Row, schemaNames: List[String]) : String = {
// some implementation where it returns a string.
}
Below is the error message I am getting (discard the line number. its getting at the foreach loop.).
Error:(57, 14) overloaded method value foreach with alternatives:
(func: org.apache.spark.api.java.function.ForeachFunction[org.apache.spark.sql.Row])Unit <and>
(f: org.apache.spark.sql.Row => Unit)Unit
cannot be applied to (org.apache.spark.sql.Row => scala.collection.mutable.ListBuffer[String])
excel_df.foreach{row => (jsonStrList += convertRowToJSON(row, columnNames))};
I am having a hard time writing the logic. any help is really appreciated.
getStringasks for aRowbut you pass it aString, I assume it should takeStringin this example?i, is never used in your function. You are either omitting some important code, or else something else is going on. For starters, can you tell us the type forinput_list? It would help us debug your issue.