如何将 Azure Synapse Dataframe 转换为 Databricks 上的 JSON?
How to convert Azure Synapse Dataframe into JSON on Databricks?
我可以将我的 Azure Synapse Dataframe 转换成 JSON 吗?因为当我尝试它时,它出错了。我将脚本用作 Pandas DataFrame 函数 df.to_json()
,因为我假设 Azure Synapse Dataframe 与 Pandas DataFrame 相同。
这是我的突触脚本:
class UtilAzSynapse(UtilAzSynapse):
@staticmethod
def write_to_synapse(df, table, write_mode, url, tempDir):
log_msg = {
"table": table,
"url": url,
"tempDir": tempDir
}
UtilInfo.pnt("UtilAzSynapse.write_to_synapse log:\n" +
json.dumps(log_msg, indent=4))
(df.write
.format("com.databricks.spark.sqldw") # Commented at 20200121 Sql dw connetion exception (email keyword: Databricks cannot access the DW)
# .format("jdbc") # Added at 20200121
.option("tableOptions", "CLUSTERED COLUMNSTORE INDEX, DISTRIBUTION = ROUND_ROBIN") # Added at 20200121
.option("url", url)
.option("dbtable", table)
.option("forward_spark_azure_storage_credentials","True")
.option("tempdir", tempDir)
.mode(write_mode)
.save()
)
这是我 select 我的 table
temp_write_dir = azBlob.get_blob_path(
container = '03-analyse',
folder_path = f"{params['working_dir']}/sqlDwWriteTempDirs"
)
print(f"temp_write_dir = {temp_write_dir}")
df_dim_store = azSynapse._read_from_synapse(fact_sales_sql, tempDir=temp_read_dir)
df_dim_store = df_dim_store.to_json()
错误:
AttributeError: 'DataFrame' object has no attribute 'to_json'
为什么我需要将我的 DataFrame 转换成 JSON 是因为当我尝试使用我的 write_to_synapse
函数时,它解释说 DataFrame 需要转换成 JSON
格式。
pyspark 数据框与 pandas 数据框不同。
在 pyspark 中你应该能够做到:
df.toJSON()
您可以在此处找到更多信息:pyspark.sql.DataFrame.toJSON
我可以将我的 Azure Synapse Dataframe 转换成 JSON 吗?因为当我尝试它时,它出错了。我将脚本用作 Pandas DataFrame 函数 df.to_json()
,因为我假设 Azure Synapse Dataframe 与 Pandas DataFrame 相同。
这是我的突触脚本:
class UtilAzSynapse(UtilAzSynapse):
@staticmethod
def write_to_synapse(df, table, write_mode, url, tempDir):
log_msg = {
"table": table,
"url": url,
"tempDir": tempDir
}
UtilInfo.pnt("UtilAzSynapse.write_to_synapse log:\n" +
json.dumps(log_msg, indent=4))
(df.write
.format("com.databricks.spark.sqldw") # Commented at 20200121 Sql dw connetion exception (email keyword: Databricks cannot access the DW)
# .format("jdbc") # Added at 20200121
.option("tableOptions", "CLUSTERED COLUMNSTORE INDEX, DISTRIBUTION = ROUND_ROBIN") # Added at 20200121
.option("url", url)
.option("dbtable", table)
.option("forward_spark_azure_storage_credentials","True")
.option("tempdir", tempDir)
.mode(write_mode)
.save()
)
这是我 select 我的 table
temp_write_dir = azBlob.get_blob_path(
container = '03-analyse',
folder_path = f"{params['working_dir']}/sqlDwWriteTempDirs"
)
print(f"temp_write_dir = {temp_write_dir}")
df_dim_store = azSynapse._read_from_synapse(fact_sales_sql, tempDir=temp_read_dir)
df_dim_store = df_dim_store.to_json()
错误:
AttributeError: 'DataFrame' object has no attribute 'to_json'
为什么我需要将我的 DataFrame 转换成 JSON 是因为当我尝试使用我的 write_to_synapse
函数时,它解释说 DataFrame 需要转换成 JSON
格式。
pyspark 数据框与 pandas 数据框不同。
在 pyspark 中你应该能够做到:
df.toJSON()
您可以在此处找到更多信息:pyspark.sql.DataFrame.toJSON