AWS Glue 默认不支持使用 XML 字符串或 XML 文件作为数据源。但是可以使用 Spark SQL 的 XML 数据源库来读取 XML 数据。以下是使用 Spark SQL 读取 XML 文件的步骤。
from pyspark.sql.functions import from_xml
from pyspark.sql.types import StructType, StructField, StringType
inputPath = "s3://my-bucket/path/to/xml/file.xml"
outputPath = "s3://my-bucket/path/to/output/folder/"
customSchema = StructType([
StructField("_id", StringType(), True),
StructField("title", StringType(), True),
StructField("author", StringType(), True),
StructField("description, StringType(), True),
StructField("price", StringType(), True)
])
df = spark.read.format("xml") \
.schema(customSchema) \
.option("rootTag", "books") \
.option("rowTag", "book") \
.load(inputPath)
df.write.mode("overwrite").format("parquet").save(outputPath)
使用以上方法,即可在 AWS Glue 中读取 XML 文件并将其转换为 Parquet 文件。