假设我在 ipython notebook 会话中在 Oracle JDK 1.8(内部版本 1.8.0_65-b17)上运行 Spark 1.6.0,该会话从以下行开始:
PYSPARK_DRIVER_PYTHON=ipython PYSPARK_DRIVER_PYTHON_OPTS=notebook pyspark --packages com.databricks:spark-xml_2.10:0.3.1z
所以我已经包含了 databricks spark-xml 包(https://github.com/databricks/spark-xml)。接下来我将对 pyspark 运行以下代码:
dmoz = '/Users/user/dummy.xml'
v=sqlContext.read.format('com.databricks.spark.xml').options(rowTag='Topic', failFast=True).load(dmoz)
print v.schema
其中 dummy.xml 包含 DMOZ 转储的这个小片段(http://rdf.dmoz.org/):
<?xml version="1.0" encoding="UTF-8"?>
<RDF xmlns:r="http://www.w3.org/TR/RDF/" xmlns:d="http://purl.org/dc/elements/1.0/" xmlns="http://dmoz.org/rdf/">
<!-- Generated at 2016-01-24 00:05:51 EST from DMOZ 2.0 -->
<Topic r:id="">
<catid>1</catid>
</Topic>
</RDF>
这可以针对我能够找到的任何验证器进行验证。结果是:
...
Py4JJavaError: An error occurred while calling o82.load.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1.0 failed 1 times, most recent failure: Lost task 0.0 in stage 1.0 (TID 1, localhost): java.lang.RuntimeException: Malformed row (failing fast): <Topic r:id=""> <catid>1</catid> </Topic>
at com.databricks.spark.xml.util.InferSchema$$anonfun$3$$anonfun$apply$2.apply(InferSchema.scala:101)
at com.databricks.spark.xml.util.InferSchema$$anonfun$3$$anonfun$apply$2.apply(InferSchema.scala:83)
...
它指的是这行代码:https ://github.com/databricks/spark-xml/blob/master/src/main/scala/com/databricks/spark/xml/util/InferSchema.scala#L101 。这显然是上面一些 javax.xml.stream 类引发的 XMLStreamException 的情况。
不幸的是,异常的细节被处理程序忽略了,所以我不知道该行到底出了什么问题。但是,从属性中删除名称空间(即r:id
变为 just id
)会使它消失。我觉得我遇到了一些常见的陷阱,只需要知道是哪一个。
UPD:我用调试语句编译了我自己的databricks lib jar,结果是,它是关于未绑定前缀的:
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1.0 failed 1 times, most recent failure: Lost task 0.0 in stage 1.0 (TID 1, localhost): javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,16]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?Topic&r:id&r
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:596)
是什么原因,我该如何解决?