将数据框行转换为所需的json格式

时间:2019-05-29 02:25:47

标签: python json python-3.x apache-spark apache-spark-sql

我是Spark和python的新手。我正在使用这些工具将已处理的数据框加载到数据库中。我有这种格式的数据框

+---------------+------------------+--------------------+------------------+
|                idfa|latlong_confidence|            homeName|  home_coordinates|
+--------------------+------------------+--------------------+------------------+
|42a7c54b-f46d-4e5...|                 2|      Opal Damansara|[3.1489, 101.5978]|
|4dcab66c-270d-41f...|                 1|Endah Puri Condom...|[3.0651, 101.7014]|
|4de88596-aa40-425...|                 1|Taman Semenyih Me...|[2.9503, 101.8281]|
|5cc08d5b-bec3-439...|                 1|    Hartamas Regency|[3.1735, 101.6637]|
|c7779e1b-7999-4a1...|                 1|Kondominium Selay...|[3.2448, 101.6679]|
|e4c58353-e54c-4b7...|                 1|Cheras Intan Apar...|[3.0731, 101.7742]|
|08c14e2b-95d8-47b...|                 3| Sri Ixora Apartment|[2.9741, 101.7839]|
|3c0ef4b6-bddd-4bc...|                 1|Kompleks Institus...|[1.4674, 103.8928]|
|a4b8d93f-483e-42c...|                 1|Taman Kamunting Aman| [4.8983, 100.696]|
|b2a9f28d-16bc-438...|                 1|Lagenda Tasek Con...|[1.4877, 103.7233]|
|b7b32511-8ecb-440...|                 1|Apartment Belaian...|[3.0417, 101.5175]|
|e4a896ee-b810-47a...|                 1|Desa Bayan Apartment|[5.3088, 100.2669]|
|00c3bb1b-c4d6-44c...|                 1|   Pertama Residency|[3.1159, 101.7296]|
|032ba0fb-e092-4a7...|                 1|       Wisma Perwira|[3.1601, 101.7425]|
|035c5edd-bd76-4bd...|                 1|Mutiara Upper Eas...|[3.1521, 101.7383]|
|061b18b8-5987-4ad...|                 1|              Oxford|[2.9488, 101.6653]|
|5b5416d4-c15c-432...|                 1|      UBN Apartments|[3.1528, 101.7067]|
|600bc148-5929-46b...|                 1|Amadesa Resort Co...|[3.0846, 101.7022]|
|6842bd0b-1d79-449...|                 1|Nibong Indah Apar...|[5.3405, 100.3021]|
|7aaf6f77-5816-4b2...|                 2|Pangsapuri Subang...|[3.1874, 101.5343]|
+--------------------+------------------+--------------------+------------------+

现在我需要的是,以以下格式将此数据帧上传到我的数据库中:

{ 
    "idfa" : "42a7c54b-f46d-4e5",
    "address" : {
        "home" : {
            "coordinates" : {
                "coordinates":[3.1489, 101.5978], 
                "type": "Point"
            }, 
            "name" : "Opal Damansara", 
            "confidence" : "2"
        }
    }
}

请建议我该怎么做。我会很感激的。

0 个答案:

没有答案