[英]Reading JSON files into Spark Dataset and adding columns from a separate Map
[英]Adding multiple columns to Spark Dataset while iterating its records
在此處Spark2.1.x。 我有一堆JSON文件(具有相同的架構),正在將它們讀入單個Spark Dataset
如下所示:
val ds = spark.read.json("some/path/to/lots/of/json/*.json")
然后,我可以打印ds
模式,看看一切都已正確讀取:
ds.printSchema()
// Outputs:
root
|-- fizz: boolean (nullable = true)
|-- moniker: string (nullable = true)
|-- buzz: string (nullable = true)
|-- foo: string (nullable = true)
|-- bar: string (nullable = true)
請注意moniker
字符串列。 我現在要:
special_date
的日期/時間列,(b)一個名為special_uuid
的UUID列,以及(c)一個名為special_phrase
的字符串列; 然后 ds
所有記錄,並為每條記錄將其moniker
值傳遞給三個后續函數:(a) deriveSpecialDate(val moniker : String) : Date
,(b) deriveSpecialUuid(val moniker : String) : UUID
和(c) deriveSpecialPhrase(val moniker : String) : String
。 然后,每個函數的輸出都必須成為相應列的記錄值。 我最好的嘗試:
val ds = spark.read.json("some/path/to/lots/of/json/*.json")
ds.foreach(record => {
val moniker : String = record.select("moniker")
val specialDate : Date = deriveSpecialDate(moniker)
val specialUuid : UUID = deriveSpecialUuid(moniker)
val specialPhrase : String = deriveSpecialPhrase(moniker)
// This doesn't work because special_* fields don't exist in the original
// schema dervied from the JSON files. We're ADDING these columns after the
// JSON read and then populating their values dynamically.
record.special_date = specialDate
record.special_uuid = specialUuid
record.special_phrase = specialPhrase
})
知道如何實現嗎?
我將使用來自Spark的udf(用戶定義的函數)將原始數據集分為3列
val deriveSpecialDate = udf((moniker: String) => // implement here)
val deriveSpecialUuid= udf((moniker: String) => // implement here)
val deriveSpecialPhrase = udf((moniker: String) => // implement here)
之后,您可以執行以下操作:
ds.withColumn("special_date", deriveSpecialDate(col("moniker)))
.withColumn("special_uuid", deriveSpecialUuid(col("moniker)))
.withColumn("special_phrase", deriveSpecialPhrase (col("moniker)))
這將為您帶來三列的新數據框。 如果需要,還可以使用map函數轉換為數據集
要創建新列,可以使用withColumn。 並且,如果您已經擁有一個功能,則需要將該功能注冊為UDF(用戶定義的功能)
val sd = sqlContext.udf.register("deriveSpecialDate",deriveSpecialDate _ )
val su = sqlContext.udf.register("deriveSpecialUuid",deriveSpecialUuid _ )
val sp = sqlContext.udf.register("deriveSpecialPhrase", deriveSpecialPhrase _)
要使用此udf,您需要withcolumn,它會創建一個新列
ds.withColumn("special_date", sd($"moniker))
.withColumn("special_uuid", su($"moniker))
.withColumn("special_phrase", sp($"moniker))
這樣,您將獲得帶有三個新添加列的原始數據集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.