将xml文件转换为pysparkDataframe

hmtdttj4  于 2021-05-19  发布在  Spark
关注(0)|答案(1)|浏览(647)

我需要将文件夹中的许多xmlf文件(zip格式)转换为Dataframe。所有关于堆栈溢出的讨论,都是基于databricks的。因为我没有使用databricks,所以我不能使用它们的方法。
有没有同样的方法?
我尝试的是:
我解压缩了文件并试图直接读取:

  1. df_schema = spark.read.format("xml").option("rowTag", "Root").load("test.xml")

错误消息:

  1. Py4JJavaError: An error occurred while calling o48.load.
  2. : java.lang.ClassNotFoundException: Failed to find data source: xml. Please find packages at http://spark.apache.org/third-party-projects.html
  3. at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:639)
  4. at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:190)
  5. at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:174)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  7. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  9. at java.lang.reflect.Method.invoke(Method.java:498)
  10. at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
  11. at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
  12. at py4j.Gateway.invoke(Gateway.java:282)
  13. at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
  14. at py4j.commands.CallCommand.execute(CallCommand.java:79)
  15. at py4j.GatewayConnection.run(GatewayConnection.java:238)
  16. at java.lang.Thread.run(Thread.java:748)
  17. Caused by: java.lang.ClassNotFoundException: xml.DefaultSource

我需要安装什么来解决这个问题吗?或者我需要通过python将xml转换成csv并读入Dataframe吗?

bvn4nwqk

bvn4nwqk1#

是的,您需要安装spark xml scala库。你可以通过maven坐标在spark作业中包含它们。https://github.com/databricks/spark-xml#scala-212

相关问题