In [10]:
#!/usr/bin/python
#coding=utf-8

Using Spark processing OSM data.

visit: http://apache-spark-user-list.1001560.n3.nabble.com/Parsing-a-large-XML-file-using-Spark-td19239.html
by wangerqi@supermap.com,2016-03-21.

Spark-XML解析器

Old project: https://github.com/HyukjinKwon/spark-xml.git
已迁移到:https://github.com/databricks/spark-xml
安装、编译:git clone https://github.com/databricks/spark-xml.git

启动时执行:

$SPARK_HOME/bin/spark-shell --packages com.databricks:spark-xml_2.11:0.3.2


In [1]:
import pyspark
from pyspark import SparkConf, SparkContext
sc = None

In [2]:
conf = (SparkConf()
         .setMaster("local")
         .setAppName("MyApp")
         .set("spark.executor.memory", "1g")
         .set("packages","com.databricks:spark-xml_2.11:0.3.2"))
print(conf)


<pyspark.conf.SparkConf object at 0x7fdba856b5c0>

In [3]:
if sc is None:
    sc = SparkContext(conf = conf)
    
print(type(sc))
print(sc)


<class 'pyspark.context.SparkContext'>
<pyspark.context.SparkContext object at 0x7fdba856b550>

In [9]:
from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)

df = sqlContext.read.format('com.databricks.spark.xml').options(rowTag='book').load('books.xml')
df.select("author", "@id").write \
    .format('com.databricks.spark.xml') \
    .options(rowTag='book', rootTag='books') \
    .save('newbooks.xml')


---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
<ipython-input-9-8f0d9ff8256d> in <module>()
      2 sqlContext = SQLContext(sc)
      3 
----> 4 df = sqlContext.read.format('com.databricks.spark.xml').options(rowTag='book').load('books.xml')
      5 df.select("author", "@id").write     .format('com.databricks.spark.xml')     .options(rowTag='book', rootTag='books')     .save('newbooks.xml')

/home/supermap/anaconda3/envs/GISpark/lib/python3.5/site-packages/pyspark/sql/readwriter.py in load(self, path, format, schema, **options)
    135                     self._jreader.load(self._sqlContext._sc._jvm.PythonUtils.toSeq(path)))
    136             else:
--> 137                 return self._df(self._jreader.load(path))
    138         else:
    139             return self._df(self._jreader.load())

/home/supermap/anaconda3/envs/GISpark/lib/python3.5/site-packages/py4j/java_gateway.py in __call__(self, *args)
    536         answer = self.gateway_client.send_command(command)
    537         return_value = get_return_value(answer, self.gateway_client,
--> 538                 self.target_id, self.name)
    539 
    540         for temp_arg in temp_args:

/home/supermap/anaconda3/envs/GISpark/lib/python3.5/site-packages/pyspark/sql/utils.py in deco(*a, **kw)
     43     def deco(*a, **kw):
     44         try:
---> 45             return f(*a, **kw)
     46         except py4j.protocol.Py4JJavaError as e:
     47             s = e.java_exception.toString()

/home/supermap/anaconda3/envs/GISpark/lib/python3.5/site-packages/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    298                 raise Py4JJavaError(
    299                     'An error occurred while calling {0}{1}{2}.\n'.
--> 300                     format(target_id, '.', name), value)
    301             else:
    302                 raise Py4JError(

Py4JJavaError: An error occurred while calling o51.load.
: java.lang.ClassNotFoundException: Failed to find data source: com.databricks.spark.xml. Please find packages at http://spark-packages.org
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.lookupDataSource(ResolvedDataSource.scala:77)
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.apply(ResolvedDataSource.scala:102)
	at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:119)
	at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:109)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
	at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:381)
	at py4j.Gateway.invoke(Gateway.java:259)
	at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
	at py4j.commands.CallCommand.execute(CallCommand.java:79)
	at py4j.GatewayConnection.run(GatewayConnection.java:209)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.databricks.spark.xml.DefaultSource
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$$anonfun$4$$anonfun$apply$1.apply(ResolvedDataSource.scala:62)
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$$anonfun$4$$anonfun$apply$1.apply(ResolvedDataSource.scala:62)
	at scala.util.Try$.apply(Try.scala:161)
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$$anonfun$4.apply(ResolvedDataSource.scala:62)
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$$anonfun$4.apply(ResolvedDataSource.scala:62)
	at scala.util.Try.orElse(Try.scala:82)
	at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.lookupDataSource(ResolvedDataSource.scala:62)
	... 14 more

In [ ]: