我需要扁平一个简单的Json文件(json行),并将其转换为Azure Synapse Analytics中星火笔记本中的Parquet格式。对于任何列,只有一个级别的嵌套对象。但是,我发现获取dataframe的模式并不返回嵌套对象的模式。我使用c#,以便其他公司开发人员不必学习其他受支持的语言。
发布于 2022-02-11 18:10:43
下面的代码将适用于上述情况。希望它能节省别人几个小时。在将子属性添加到父数据帧之后,它还会从数据帧中删除父列。
我不需要将这段代码变成递归横截,因为我们没有嵌套的对象。
using System;
using System.Collections.Generic;
using Microsoft.Spark.Sql;
using Microsoft.Spark.Sql.Types;
using System.Diagnostics;
var df = spark.Read().Json("{Your source file path here}");
//get the schema of the data frame
var dfSchema = df.Schema() ;
// traverse the schema of the dataframe
foreach(var parentSchemaField in dfSchema.Fields) {
if (parentSchemaField.DataType is StructType) {
// get a new dataframe that just contains the child data from the parent
var childFrame = df.Select($"{parentSchemaField.Name}.*") ;
// traverse the schema of the child dataframe
foreach(var childSchemaField in childFrame.Schema().Fields) {
//make a new column in the parent dataframe for each parents child property
df = df.WithColumn($"{parentSchemaField.Name}.{childSchemaField.Name}",Col($"{parentSchemaField.Name}.{childSchemaField.Name}")) ;
}
// drop the parent column from the data frame its no longer needed
df = df.Drop(parentSchemaField.Name) ;
}
}
df.Write().Parquet("{Your sink file path here}") ;https://stackoverflow.com/questions/71084684
复制相似问题