2017-05-13 1 views
0

あるスパークにおける単一のラインJSONを読み取り、私は、次の列キーが可変

{"Hotel Dream":{"Guests":20,"Address":"14 Naik Street","City":"Manila"},"Serenity Stay":{"Guests":35,"Address":"10 St Marie Road","City":"Manila"}....} 

Iコンテキストを刺激するためにJSONを読み取るために、次を使用する場合、それは

もたらすような単一のラインJSONファイルを持っています
val hotelDF = sqlContext.read.json("file").printSchema 

root 
|-- Hotel Dream: struct (nullable = true) 
| |-- Address: string (nullable = true) 
| |-- City: string (nullable = true) 
| |-- Guests: long (nullable = true) 
|-- Serenity Stay: struct (nullable = true) 
| |-- Address: string (nullable = true) 
| |-- City: string (nullable = true) 
| |-- Guests: long (nullable = true) 

私はデータフレームが正則スキーマで終わるような変化の列(ホテルドリームなどセレニティ滞在)を移調したい

Hotel: string (nullable = true) 
Guests: string (nullable = true) 
Address: string (nullable = true) 
City: string (nullable = true) 

また、jsonをtextFileまたはwholeTextFilesとして傷害を試みました。しかし、改行デリミタがないので、マップ関数でコンテンツをマップすることはできません。

このようなデータ形式の読み方についての入力はありますか?

答えて

0

私はあなたの質問(そのない完璧なソリューションが)

var newDataFrame = Seq(("test", "test", "test", "test")).toDF("Hotel", "Address", "City", "Guests") 
for(name <- hotelDF.schema.fieldNames) { 
    val tempdf = hotelDF.withColumn("Hotel", lit(name)) 
    .withColumn("Address", hotelDF(name + ".Address")) 
    .withColumn("City", hotelDF(name + ".City")) 
    .withColumn("Guests", hotelDF(name + ".Guests")) 
    val tdf = tempdf.select("Hotel", "Address", "City", "Guests") 
    newDataFrame = newDataFrame.union(tdf) 
} 
newDataFrame.filter(!(col("Hotel") === "test")).show 
から理解するものからあなたの解決策になることができます後