xml - 使用 Spark 将 XML 读入数据框
问题描述
我正在尝试使用 Spark 将 XML 文件读入数据帧。
我按照 GitHub 上的这个指南工作。
由于某种原因,具有属性的列id
是null
.
我正在这个xml 文件上测试我的代码。
%pyspark
from pyspark.sql import SQLContext
from pyspark.sql.types import *
AWS_ACCESS_KEY_ID = "*********************"
AWS_SECRET_ACCESS_KEY = "*************************"
sc._jsc.hadoopConfiguration().set("fs.s3n.impl", "org.apache.hadoop.fs.s3native.NativeS3FileSystem")
sc._jsc.hadoopConfiguration().set("fs.s3n.awsAccessKeyId", AWS_ACCESS_KEY_ID)
sc._jsc.hadoopConfiguration().set("fs.s3n.awsSecretAccessKey", AWS_SECRET_ACCESS_KEY)
sqlContext = SQLContext(sc)
customSchema = StructType([ \
StructField("_id", StringType(), True), \
StructField("author", StringType(), True), \
# StructField("description", StringType(), True), \
StructField("genre", StringType(), True), \
StructField("price", DoubleType(), True), \
StructField("publish_date", StringType(), True), \
StructField("title", StringType(), True)])
df = sqlContext.read \
.format('com.databricks.spark.xml') \
.options(rowTag='book') \
.load('s3n://######/###/######/books.xml',schema = customSchema)
df.show()
+----+--------------------+---------------+-----+------------+--------------------+
| _id| author| genre|price|publish_date| title|
+----+--------------------+---------------+-----+------------+--------------------+
|null|Gambardella, Matthew| Computer|44.95| 2000-10-01|XML Developer's G...|
|null| Ralls, Kim| Fantasy| 5.95| 2000-12-16| Midnight Rain|
|null| Corets, Eva| Fantasy| 5.95| 2000-11-17| Maeve Ascendant|
|null| Corets, Eva| Fantasy| 5.95| 2001-03-10| Oberon's Legacy|
|null| Corets, Eva| Fantasy| 5.95| 2001-09-10| The Sundered Grail|
|null| Randall, Cynthia| Romance| 4.95| 2000-09-02| Lover Birds|
|null| Thurman, Paula| Romance| 4.95| 2000-11-02| Splish Splash|
|null| Knorr, Stefan| Horror| 4.95| 2000-12-06| Creepy Crawlies|
|null| Kress, Peter|Science Fiction| 6.95| 2000-11-02| Paradox Lost|
|null| O'Brien, Tim| Computer|36.95| 2000-12-09|Microsoft .NET: T...|
|null| O'Brien, Tim| Computer|36.95| 2000-12-01|MSXML3: A Compreh...|
|null| Galos, Mike| Computer|49.95| 2001-04-16|Visual Studio 7: ...|
+----+--------------------+---------------+-----+------------+--------------------+
这是 XML 文件的一部分:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.This manual describes Oracle XML DB, and how you can use it to store, generate, manipulate, manage,
and query XML data in the database.
</description>
</book>
</catalog>
解决方案
推荐阅读
- google-app-engine - Google 对“Google API”的含义是什么?App Engine 端点(例如 xxx.appspot.com)是 Google API 吗?
- javascript - 在 socket.io 中每秒发射一次不起作用
- php - PHP复选框从文件夹中删除文件
- java - 尝试使用 forge 修改 minecraft 1.12.2 的问题。无法打包和测试构建
- r - 如何强制以“:”分隔的一系列数字到该范围内的数字列表中?
- javascript - 查找多个数组中的连续数字 JavaScript
- apache-spark - 如何将字符串数组转换为 numpy 数组并将其传递给 Pyspark 中的 UDF?
- python - 在用户定义类的实例上启用列表(实例)或元组(实例)
- javascript - 二进制数组需要有一个固定的长度
- latex - 在 LaTeX 中使用 \hline 的错误不会中断编译,但也许我这样做是错误的