pyspark 如何在Azure Synapse Workspace中使用Pyplot保存文件?

isr3a4wc  于 2024-01-06  发布在  Spark
关注(0)|答案(1)|浏览(117)

我正在使用Azure Synapse Analytics工作区,在工作区的“笔记本”选项卡中运行PYSpark笔记本。
该工作区还连接到Azure Data Lake Storage Gen 2,后者有一个名为filesystem的容器。
通常,为了保存文件,我在导出DataFrame后使用ABFSS路径将它们写入Spark。

  1. UC_export=spark.createDataFrame(Error_Report.astype(str), verifySchema=False)
  2. UC_export.coalesce(1).write.format('csv').mode('overwrite').save('abfss://[Container Name].dfs.core.windows.net/Assets_DQ/Error_Report.csv', header=True)

字符串
但是对于下面的代码,我不知道如何保存数字。我如何运行一个循环,可以将 *.png文件导出到ABFSS容器?

  1. for error in Unique_Conso_df_check['Rule Broken'].unique():
  2. for column in suitable_columns:
  3. fig = plt.figure()
  4. ax = plt.subplot(111)
  5. pd.crosstab(Unique_Conso_df_Error[column],Unique_Conso_df_Error[error]).plot(kind='bar', rot = 90, ax=ax, figsize=[20,10])
  6. ax.get_figure().savefig(savedirectory+'/densityplot_'+str(column)+'.png')
  7. plt.show()

kuhbmx9i

kuhbmx9i1#

您需要挂载存储帐户,然后可以使用plt.savefig(),如this post中所述
只有当我保存图像之前没有使用plt.show()时,它才起作用。

  1. mssparkutils.fs.mount("abfss://Container_name@account_name.dfs.core.windows.net/file_path/", "/mount_name", {"linkedService":"Your_linked_service"})
  2. #here is my graph
  3. plt.plot(x,y)
  4. # retrieve the job-id
  5. jobId=mssparkutils.env.getJobId()
  6. # now save the image using the mounted path
  7. plt.savefig(f"/synfs/{jobId}/mount_name/figure_name.png")

字符串

相关问题