我有一个包含混合内容的XML文档,我正在使用Dataframe中的自定义模式来解析它。我遇到了一个问题,模式只会提取"Measure“的文本。
XML如下所示
<QData>
<Measure> some text here
<Answer>Answer1</Answer>
<Question>Question1</Question>
</Measure>
<Measure> some text here
<Answer>Answer1</Answer>
<Question>Question1</Question>
</Meaure>
</QData>我的模式如下:
def getCustomSchema():StructType = {StructField("QData",
StructType(Array(
StructField("Measure",
StructType( Array(
StructField("Answer",StringType,true),
StructField("Question",StringType,true)
)),true)
)),true)}当我尝试访问测量中的数据时,我只能得到“这里的一些文本”,当我试图从Answer获取信息时,它失败了。我也只得到了一个指标。
编辑:这就是我尝试访问数据的方式
val result = sc.read.format("com.databricks.spark.xml").option("attributePrefix", "attr_").schema(getCustomSchema)
.load(filename.toString)
val qDfTemp = result.mapPartitions(partition =>{val mapper = new QDMapper();partition.map(row=>{mapper(row)}).flatMap(list=>list)}).toDF()
case class QDMapper(){
def apply(row: Row):List[QData]={
val qDList = new ListBuffer[QData]()
val qualData = row.getAs[Row]("QData") //When I print as list I get the first Measure text and that is it
val measure = qualData.getAs[Row]("Measure") //This fails
}
}发布于 2021-02-16 17:49:49
您可以使用行标记作为根标记,并访问其他元素:-
df_schema = sqlContext.read.format('com.databricks.spark.xml').options(rowTag='<xml_tag_name>').load(schema_path)有关简要代码,请访问https://github.com/harshaltaware/Pyspark/blob/main/Spark-data-parsing/xmlparsing.py
https://stackoverflow.com/questions/48063362
复制相似问题