I need to work with this json structure;
val sample_data = """{
"Record": {
"Id": "id",
"IdsArray": [{
"Id1": 1234,
"Id2": "master_id",
"Id3": "11",
"Id4": "12",
"Id5": "13"
},
{
"Id1": 4321,
"Id2": "master_id",
"Id3": "21",
"Id4": "22",
"Id5": "23"
}],
"NameArray": [{
"Number": 1234567890,
"Date1": "some date",
"Date2": "some another date",
"FirstName": "Ron",
"MiddleName": "",
"LastName": "Swanson",
"NameSuffix": "Mr."
}]
}
}"""
I want to flatten this json and choose one of the array from two arrays that we have in IdsArray. For example, I may want to choose based on Id1 value, i.e. whichever value is larger select that block of array out of all available array elements.
val df = spark.read.json(Seq(sample_data).toDS)
df.select($"Record.*")
.select($"NameArray", $"IdsArray")
.withColumn("someColName", myFuncUDF($"IdsArray")
....
....
This is the general flow of logic I have tried.
For the UDF, I have tried like this;
val myFunc = (col_array: Array[Array[Any]]) => {
<some conditions and logic>
}
val myFuncUDF = udf(myFunc)
When I try to run with this, I get error;
myFunc: Array[Array[Any]] => Any = <function1>
java.lang.UnsupportedOperationException: Schema for type Any is not supported
My question is what is the correct way to pass the parameter to the function. Or, what is the best way to approach for these type of problem? For this particular problem .withColumn("someColName", myFuncUDF($"IdsArray") should give me value in IdsArray[1] because Id1 value in IdsArray[1] is 4321 which is greater than 1234.