6

我正在尝试使用 spark-xml jar 在 pyspark 中读取 xml/嵌套 xml。

df = sqlContext.read \
  .format("com.databricks.spark.xml")\
   .option("rowTag", "hierachy")\
   .load("test.xml"

当我执行时,数据框没有正确创建。

    +--------------------+
    |                 att|
    +--------------------+
    |[[1,Data,[Wrapped...|
    +--------------------+

下面提到了我拥有的 xml 格式:

在此处输入图像描述

4

3 回答 3

11

heirarchy应该是rootTag并且att应该是rowTag

df = spark.read \
    .format("com.databricks.spark.xml") \
    .option("rootTag", "hierarchy") \
    .option("rowTag", "att") \
    .load("test.xml")

你应该得到

+-----+------+----------------------------+
|Order|attval|children                    |
+-----+------+----------------------------+
|1    |Data  |[[[1, Studyval], [2, Site]]]|
|2    |Info  |[[[1, age], [2, gender]]]   |
+-----+------+----------------------------+

schema

root
 |-- Order: long (nullable = true)
 |-- attval: string (nullable = true)
 |-- children: struct (nullable = true)
 |    |-- att: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- Order: long (nullable = true)
 |    |    |    |-- attval: string (nullable = true)

查找有关databricks xml的更多信息

于 2018-05-20T02:44:43.287 回答
3

Databricks 已发布新版本以将 xml 读取到 Spark DataFrame

<dependency>
     <groupId>com.databricks</groupId>
     <artifactId>spark-xml_2.12</artifactId>
     <version>0.6.0</version>
 </dependency>

我在此示例中使用的输入 XML 文件可在GitHub存储库中获得。

val df = spark.read
      .format("com.databricks.spark.xml")
      .option("rowTag", "person")
      .xml("persons.xml")

架构

root
 |-- _id: long (nullable = true)
 |-- dob_month: long (nullable = true)
 |-- dob_year: long (nullable = true)
 |-- firstname: string (nullable = true)
 |-- gender: string (nullable = true)
 |-- lastname: string (nullable = true)
 |-- middlename: string (nullable = true)
 |-- salary: struct (nullable = true)
 |    |-- _VALUE: long (nullable = true)
 |    |-- _currency: string (nullable = true)

输出:

+---+---------+--------+---------+------+--------+----------+---------------+
|_id|dob_month|dob_year|firstname|gender|lastname|middlename|         salary|
+---+---------+--------+---------+------+--------+----------+---------------+
|  1|        1|    1980|    James|     M|   Smith|      null|  [10000, Euro]|
|  2|        6|    1990|  Michael|     M|    null|      Rose|[10000, Dollor]|
+---+---------+--------+---------+------+--------+----------+---------------+

请注意,Spark XML API 有一些限制,并在此处讨论Spark-XML API 限制

希望这可以帮助 !!

于 2019-08-20T14:41:09.917 回答
3

您可以使用 Databricks jar 将 xml 解析为数据框。您可以使用 maven 或 sbt 编译依赖项,也可以直接使用带有 spark submit 的 jar。

pyspark --jars /home/sandipan/Downloads/spark_jars/spark-xml_2.11-0.6.0.jar

df = spark.read \
    .format("com.databricks.spark.xml") \
    .option("rootTag", "SmsRecords") \
    .option("rowTag", "sms") \
    .load("/home/sandipan/Downloads/mySMS/Sms/backupinfo.xml")

Schema>>> df.printSchema()
root
 |-- address: string (nullable = true)
 |-- body: string (nullable = true)
 |-- date: long (nullable = true)
 |-- type: long (nullable = true)

>>> df.select("address").distinct().count()
530 

按照这个 http://www.thehadoopguy.com/2019/09/how-to-parse-xml-data-to-saprk-dataframe.html

于 2019-09-15T18:10:49.540 回答