将 xml 字符串写入 Azure 数据湖存储

问题描述 投票:0回答:2

当我尝试将 xml 字符串写入 azure datalake 存储时,我收到错误,因为找不到文件。我正在使用带有 python 的突触笔记本来写入文件。 Synapse Notebook 和 Datalake 存储位于同一资源组中

我尝试使用 to_xml({file_path/output.xml})。 但这不适用于 xml 字符串

python xml azure-synapse azure-data-lake xmlwriter
2个回答
0
投票

spark.sparkContext.parallelize([xml_string], 1)
xml_string 转换为分布式集合 (RDD) 并指定应将其存储为一个分区。

.saveAsTextFile(adls_path)
将 RDD 的内容作为文本文件保存到指定的 ADLS Gen2 路径。

我在 Pyspark 中尝试了以下方法:

xml_string = """
<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>28</age>
  </person>
</root>
"""
adls_path = "abfss://[email protected]/output.xml"
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("WriteXMLToADLS").getOrCreate()
spark.sparkContext.parallelize([xml_string], 1).saveAsTextFile(adls_path)
print("XML data has been written to ADLS Gen2.")

enter image description here

enter image description here

  • 上面的代码将XML字符串转换为RDD,然后将其作为文本文件保存到您指定的ADLS Gen2路径。
  • 这是一种使用 Azure Synapse 中 PySpark 提供的分布式数据处理功能将数据写入 ADLS Gen2 的方法。

0
投票

如果你使用 pandas,我假设你使用它:

import pandas as pd
import io
xml = '''<data><row><tex>text example</tex></row></data>'''

df = pd.read_xml(io.StringIO(xml))
print(df)

# Output in file
out ='StringXML.xml'
df.to_xml(f'{out}', index=False)

这将写入文件:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <tex>text example</tex>
  </row>
</data>
© www.soinside.com 2019 - 2024. All rights reserved.