如何为下面的嵌套Json pyspark编写模式

eit6fx6z  于 2022-11-16  发布在  Apache
关注(0)|答案(1)|浏览(111)

如何为下面的json编写schema:

"place_results": {
      "title": "W2A Architects",
      "place_id": "ChIJ4SUGuHw5xIkRAl0856nZrBM",
      "data_id": "0x89c4397cb80625e1:0x13acd9a9e73c5d02",
      "data_cid": "1417747306467056898",
      "reviews_link": "httpshl=en",
      "photos_link": "https=en",
      "gps_coordinates": {
        "latitude": 40.6027801,
        "longitude": -75.4701499
      },
      "place_id_search": "http",
      "rating": 3.7,

我在写下面的模式时得到了空值。如何知道要使用的正确数据类型?

StructField('place_results', StructType([
                                                                StructField('address', StringType(), True), 
                                                                StructField('data_cid', StringType(), True), 
                                                                StructField('data_id', StringType(), True), 
                                                                StructField('gps_coordinates', StringType(), True), 
                                                                StructField('open_state', StringType(), True), 
                                                                StructField('phone', StringType(), True), 
                                                                StructField('website', StringType(), True)
                                                ])),
4ioopgfo

4ioopgfo1#

这应该可行:

StructType([
  StructField('place_results', 
              StructType([
                StructField('data_cid', StringType(), True), 
                StructField('data_id', StringType(), True), 
                StructField('gps_coordinates', StructType([
                  StructField('latitude', DoubleType(), True),
                  StructField('longitude', DoubleType(), True)]), True), 
                StructField('photos_link', StringType(), True), 
                StructField('place_id', StringType(), True), 
                StructField('place_id_search', StringType(), True), 
                StructField('rating', DoubleType(), True), 
                StructField('reviews_link', StringType(), True), 
                StructField('title', StringType(), True)]), True)
])

我用这个命令得到了这个:

spark.read.option("multiLine", True).json("dbfs:/test/sample.json").schema

相关问题