Spark 2.1 and Scala 2.11 here. I have a large Map[String,Date] that has 10K key/value pairs in it. I also have 10K JSON files living on a file system that is accessible to Spark:
mnt/
some/
path/
data00001.json
data00002.json
data00003.json
...
data10000.json
Each KV pair in the map corresponds to its respective JSON file (hence the 1st map KV pair corresponds to data00001.json, etc.)
I want to read all these JSON files into 1 large Spark Dataset and, while I'm at it, add two new columns to this dataset (that don't exist in the JSON files). Each map key will be the value for the first new column, and each key's value will be the value for the second new column:
val objectSummaries = getScalaList()
val dataFiles = objectSummaries.filter { _.getKey.endsWith("data.json") }
val dataDirectories = dataFiles.map(dataFile => {
val keyComponents = dataFile.getKey.split("/")
val parent = if (keyComponents.length > 1) keyComponents(keyComponents.length - 2) else "/"
(parent, dataFile.getLastModified)
})
// TODO: How to take each KV pair from dataDirectories above and store them as the values for the
// two new columns?
val allDataDataset = spark.read.json("mnt/some/path/*.json")
.withColumn("new_col_1", dataDirectories._1)
.withColumn("new_col_2", dataDirectories._2)
I've confirmed that Spark will honor the wildcard (mnt/some/path/*.json) and read all the JSON files into a single Dataset when I remove the withColumn methods and do a allData.show(). So I'm all good there.
What I'm struggling with is: how do I add the two new columns and then pluck out all the key/value map elements correctly?