экспортировать схему файла паркета в josn или csv - PullRequest
0 голосов
/ 05 марта 2020

Мне нужно извлечь схему файла паркета в формате json, txt или csv. это должно включать имя столбца, тип данных из файла паркета,

, например:

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

что-то вроде этого.

любой указатель будет очень полезен.

1 Ответ

0 голосов
/ 05 марта 2020

Мы можем прочитать schema из файла parquet, используя .schema и преобразовать в формат json, наконец сохранить как textfile.

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"}
...