我正在尝试从pyspark数据帧创建JSON结构。我的数据框中有以下列-批次id、批次运行id、表名、列名、列数据类型、上次刷新时间、刷新频率、所有者
我希望它在下面的JSON结构中-
{
"GeneralInfo": {
"DataSetID": "xxxx1234Abcsd",
"Owner" : ["test1@email.com", "test2@email.com", "test3@email.com"]
"Description": "",
"BuisnessFunction": "",
"Source": "",
"RefreshRate": "Weekly",
"LastUpdate": "2020/10/15",
"InfoSource": "TemplateInfo"
},
"Tables": [
{
"TableName": "Employee",
"Columns" : [
{ "ColumnName" : "EmployeeID",
"ColumnDataType": "int"
},
{ "ColumnName" : "EmployeeName",
"ColumnDataType": "string"
}
]
}
}
}
我试图通过dataframe列索引分配JSON字符串中的值,但它给了我一个错误,因为“column类型的对象不可JSON序列化”。我用过如下方法-
{
"GeneralInfo": {
"DataSetID": df["batch_id"],
"Owner" : list(df["owner"])
"Description": "",
"BuisnessFunction": "",
"Source": "",
"RefreshRate": df["refresh_frequency"],
"LastUpdate": df["last_update_time"],
"InfoSource": "TemplateInfo"
},
"Tables": [
{
"TableName": df["table_name"],
"Columns" : [
{ "ColumnName" : df["table_name"]["column_name"],
"ColumnDataType": df["table_name"]["column_datatype"]
}
]
}
}
}
请在这方面帮助我,我刚刚开始在Pyspark中编码
尝试从您提供的示例数据中获取JSON格式,输出格式与您期望的不完全匹配。您可以进一步即兴编写下面的代码
我们可以使用toJSON函数将数据帧转换为JSON格式。在调用toJSON函数之前,我们需要使用array()、struct函数,方法是根据需要传递所需的列以匹配JSON格式
相关问题 更多 >
编程相关推荐