2017-10-31 153 views
5

BuDiğer sütunları türetmek için bir sütunu json nesnesi olarak geçici olarak nasıl depolarım?

likes=dogs;hates=birds;likes=sports;eats=cheese 

gibi anahtar değer çiftleri ile bir veri kümesi var ben o zaman, ben bir dizeye çevrim olmadan bu json veri tipleri de tutabilir bir yolu var mı json

{"likes": ["dogs","sports"], "hates": ["birds"], "eats": ["cheese"]} 

çevirmek Satır bazında ondan daha fazla sütun türetebilir miyim? Her şeye eklenmiş olan jsonu dizgeden çözmek zorunda kalmadan böyle bir şeye bakmak istiyorum. Eğer orijinal dosyayı

likes=dogs;hates=birds;likes=sports;eats=cheese 

kapalı çalışıyorsanız

 Dataset<Row> df1 = df.withColumn("interests", callUDF("to_json", col("interests"))) 
         .withColumn("likes", callUDF("extract_from_json", "likes", col("interests"))) 
         .withColumn("hates", callUDF("extract_from_json", "hates", col("interests"))) 
         .withColumn("hates", callUDF("extract_from_json", "eats", col("interests"))); 

cevap

3

o zaman o zaman bazı basit RDD manipülasyonlar yapmak sc.textFile işin detayına okuyabiliyordu.

val df = sc.textFile(file) 
    .flatMap(x => x.split(";")) 
    .map(x => (x.split("=")(0), x.split("=")(1))) 
    .toDF("interest","value") 

df.withColumn("tmp",lit(1)).groupBy("tmp").pivot("interest").agg(collect_list("value")) 

+---+--------+-------+--------------+ 
|tmp| eats| hates|   likes| 
+---+--------+-------+--------------+ 
| 1|[cheese]|[birds]|[dogs, sports]| 
+---+--------+-------+--------------+