将XML数据从Google Earth KML文件上传到DataBricks

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

我正在设置DataBricks来比较和对比来自多个来源的数据。部分数据采用CSV文件,部分采用JSON格式,其他数据采用Google Earth KML文件。最后一个真的是一个挑战;我正在尝试使用数据上载功能上传XML数据,但DataBricks无法从XML字符串创建表。将XML插入DataBricks表的过程是什么?

python xml kml google-earth databricks
1个回答
0
投票

将spark-xml库用于工作区的最佳方法。

在maven / spark包部分中搜索spark-xml,然后按照此步骤将其添加到库https://docs.databricks.com/user-guide/libraries.html#create-a-library

将库附加到群集

https://docs.databricks.com/user-guide/libraries.html#attach-a-library-to-a-cluster

最后使用以下代码读取数据库中的xml数据

xmldata = spark.read.format('xml').option("rootTag","note").load('dbfs:/mnt/mydatafolder/xmls/note.xml')

这里也是python代码做同样的事情:

import xml.etree.ElementTree as ET
xmlfiles = dbutils.fs.ls(storage_mount_name)

##Get attribute names (for now I took all leafs of the xml structure)
firstfile = xmlfiles[0].path.replace('dbfs:','/dbfs')
root = ET.parse(firstfile).getroot()
attributes = [node.tag for node in root.iter() if len(node)==0]
clean_attribute_names = [re.sub(r'\{.*\}', '', a) for a in attributes]

#Create Dataframe and save it as csv
df = pd.DataFrame(columns=clean_attribute_names, index=xmlfiles)
for xf in xmlfiles:
    afile = xf.path.replace('dbfs:','/dbfs')
    root = ET.parse(afile).getroot()
    df.loc[afile] = [node.text for node in root.iter() if node.tag in attributes]
© www.soinside.com 2019 - 2024. All rights reserved.