【问题标题】:Export parquet file schema to JSON or CSV将 parquet 文件模式导出为 JSON 或 CSV
【发布时间】:2020-03-05 11:30:41
【问题描述】:

我需要将 parquet 文件的架构提取为 JSON、TXT 或 CSV 格式。 这应该包括列名、parquet 文件中的数据类型。

例如:

{"id", "type" : "integer" },
 {"booking_date""type" : "timestamp", "format" : "%Y-%m-%d %H:%M:%S.%f" }

【问题讨论】:

    标签: pyspark schema metadata parquet


    【解决方案1】:

    我们可以使用.schemaparquet文件中读取schema,并转换成json格式最后保存为textfile。 p>

    input parquet file:

    spark.read.parquet("/tmp").printSchema()
     #root
     #|-- id: integer (nullable = true)
     #|-- name: string (nullable = true)
     #|-- booking_date: timestamp (nullable = true)
    

    Extract the schema and write to HDFS/local filesystem:

    spark.sparkContext.parallelize( #converting from string to rdd
    [spark.read.parquet("/tmp").schema.json()] #read schema of parquetfile
     ).repartition(1).\
    saveAsTextFile("/tmp_schema/") #saving the file into HDFS
    

    Read the output file from hdfs:

    $ hdfs dfs -cat /tmp_schema/part-00000
    {"fields":[{"metadata":{},"name":"id","nullable":true,"type":"integer"},{"metadata":{},"name":"name","nullable":true,"type":"string"},{"metadata":{},"name":"booking_date","nullable":true,"type":"timestamp"}],"type":"struct"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2016-03-01
      • 2020-01-28
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2023-03-24
      相关资源
      最近更新 更多