首页 > 解决方案 > Spark DataFrame 获取所有

每个元素的 xml 文档中的 xml 标签

问题描述

我为 Pyspark 中的数据框打印了这个模式 - 用于 xml 文件:

root
|-- _id: long (nullable = true)
 |-- _published-at: string (nullable = true)
 |-- _title: string (nullable = true)
 |-- a: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _VALUE: string (nullable = true)
 |    |    |-- _href: string (nullable = true)
 |    |    |-- _type: string (nullable = true)
 |-- p: array (nullable = true)
 |    |-- element: string (containsNull = true)

XML 数据如下所示:

在此处输入图像描述

如您所见,每篇文章内容都是带有 < P> 和 < a> 标签的 html,架构将每篇文章的内容确定为 < P> 标签数组,但实际上在显示数据时,它只采用第一个 < P> 标记直到第一个 <a> 并且剩余的内容被忽略!

articles_df.select("_id", articles_df.p.alias('content')).show(truncate=False)

17 |[Chief juvenile probation officer ] 

我怎么能保证拿起所有的内容?(下一步将是在没有 html 特殊承租人的情况下对其进行格式化: )

我尝试使用自己的自定义架构,但得到了相同的结果:

link_structure = StructType([
    StructField("_VALUE", StringType(), True),
    StructField("_href", StringType(), True),
    StructField("_type", StringType(), True)
    ])

articles_schema = StructType([
    StructField("_id", LongType(), True),
    StructField("_published-at", StringType(), True),
    StructField("_title", StringType(), True),
    StructField("a", ArrayType(link_structure), True),
    StructField("p", ArrayType(StringType()), True)])

articles_df = spark.read.format('xml'). \
    options(rootTag='articles', rowTag='article'). \
    load('data-sets/articles-validation-small.xml', schema=articles_schema)

标签: pysparkapache-spark-sqlpyspark-sqlpyspark-dataframes

解决方案


推荐阅读