2017-02-23 51 views
1

我试图解析使用com.databricks.spark.xml星火XML解析

Dataset<Row> df = spark.read().format("com.databricks.spark.xml") 
      .option("rowTag", "row").load("../1000.xml"); 

df.show(10); 

大型XML文件我得到的输出如下

++ ||
++
++

我这么想吗?

这是我的示例XML行。

<row Id="7" PostTypeId="2" ParentId="4" CreationDate="2008-07-31T22:17:57.883" Score="316" Body="&lt;p&gt;An explicit cast to double isn't necessary.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;double trans = (double)trackBar1.Value/5000.0;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;p&gt;Identifying the constant as &lt;code&gt;5000.0&lt;/code&gt; (or as &lt;code&gt;5000d&lt;/code&gt;) is sufficient:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code&gt;double trans = trackBar1.Value/5000.0;&#xA;double trans = trackBar1.Value/5000d;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;" /> 

非常感谢。

+0

这意味着你的XML中的数据没有映射到柱状结构,你的数据集是空的。 – FaigB

回答

0

尝试使用_符号位于模式中的XML属性名称之前。如果它不工作 - 尝试使用@符号。观看example,但它提供了旧的Spark版本。

0

问题与您的xml数据。与您的代码示例

<row id="7"> 
     <author>Corets, Eva</author> 
     <title>Maeve Ascendant</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-11-17</publish_date> 
     <description>After the collapse of a nanotechnology 
     society in England, the young survivors lay the 
     foundation for a new society.</description> 
    </row> 

:尝试它作为示例XML数据

Dataset<Row> df = spark.read().format("com.databricks.spark.xml") 
      .option("rowTag", "row").load("../1000.xml"); 

要提供自定义模式:

import org.apache.spark.sql.SQLContext 
import org.apache.spark.sql.types.{StructType, StructField, StringType, DoubleType}; 

val sqlContext = new SQLContext(sc) 
val customSchema = StructType(Array(
    StructField("_id", StringType, nullable = true), 
    StructField("author", StringType, nullable = true), 
    StructField("description", StringType, nullable = true), 
    StructField("genre", StringType ,nullable = true), 
    StructField("price", DoubleType, nullable = true), 
    StructField("publish_date", StringType, nullable = true), 
    StructField("title", StringType, nullable = true))) 


val df = sqlContext.read 
    .format("com.databricks.spark.xml") 
    .option("rowTag", "book") 
    .schema(customSchema) 
    .load("books.xml") 

val selectedData = df.select("author", "_id") 
selectedData.write 
    .format("com.databricks.spark.xml") 
    .option("rootTag", "books") 
    .option("rowTag", "book") 
    .save("newbooks.xml") 

请参考databricks xml documentation

+0

没有办法在spark.xml中传递XML属性。如果我创建一个自定义模式如何创建一个传递XML属性? – udarajag

+0

嗨FaigB感谢您的答案,我也读通过databricks文档,但我无法找出一种方法来传递XML标签的属性。因为我不能像你提到的那样改变XML格式。 – udarajag