如何将JSON文档转换为Parquet / ORC文件



是否可以将JSON转换为Parquet/ORC格式?

我已经在HIVE中将CSV/TSV数据转换为Parquet,

以下步骤
1: Create an external HIVE Table with TSV data source and TSV serde.
2: Create a normal HIVE table with Parquet serde.
3: INSERT INTO ParquetTable SELECT * FROM ParquetTable.

所以问题是,是否有类似的方法可以将 JSON 转换为 Parquet,还是需要先平展 JSON 数据?

这可以使用Apache Spark库轻松完成。

先决条件:数据应为单行 JSON。由于 Spark 数据源库不支持多行 JSON。

下面是使用 Spark 2.x 的示例代码:

val spark = SparkSession.builder()
.master("local")
.getOrCreate()
val inpDF = spark.read.json("<inputPath>")
// Auto schema Inference 
inpDF.printSchema()
inpDF.write.parquet("<outputPath>")

单行 JSON :{"Device":{"Brand":"Apple","Model":"Iphone11Pro"}}

多行 JSON :{ "Device": { "Brand":"Apple", "Model":"Iphone11Pro" } }

这个python库对我有用:https://github.com/noirello/pyorc。代码片段如下(借自库 README.md(

import pyorc
with open("./new_data.orc", "wb") as data:
with pyorc.Writer(data, "struct<col0:int,col1:string>") as writer:
writer.write((1, "ORC from Python"))

最新更新