databricks/spark-xml
Scala
Captured source
source ↗databricks/spark-xml
Description: XML data source for Spark SQL and DataFrames
Language: Scala
License: Apache-2.0
Stars: 513
Forks: 223
Open issues: 0
Created: 2015-11-26T02:46:09Z
Pushed: 2024-08-11T22:56:29Z
Default branch: master
Fork: no
Archived: yes
README:
XML Data Source for Apache Spark 3.x
- A library for parsing and querying XML data with Apache Spark, for Spark SQL and DataFrames.
The structure and test tools are mostly copied from CSV Data Source for Spark.
- This package supports to process format-free XML files in a distributed way, unlike JSON datasource in Spark restricts in-line JSON format.
- Compatible with Spark 3.0 and later with Scala 2.12, and also Spark 3.2 and later with Scala 2.12 or 2.13. Scala 2.11 and Spark 2 support ended with version 0.13.0.
- Currently,
spark-xmlis planned to become a part of Apache Spark 4.0. This library will remain in maintenance mode for Spark 3.x versions.
Linking
You can link against this library in your program at the following coordinates:
groupId: com.databricks artifactId: spark-xml_2.12 version: 0.18.0
Using with Spark shell
This package can be added to Spark using the --packages command line option. For example, to include it when starting the spark shell:
$SPARK_HOME/bin/spark-shell --packages com.databricks:spark-xml_2.12:0.18.0
Features
This package allows reading XML files in local or distributed filesystem as Spark DataFrames.
When reading files the API accepts several options:
path: Location of files. Similar to Spark can accept standard Hadoop globbing expressions.rowTag: The row tag of your xml files to treat as a row. For example, in this xml..., the appropriate value would bebook. Default isROW.samplingRatio: Sampling ratio for inferring schema (0.0 ~ 1). Default is 1. Possible types areStructType,ArrayType,StringType,LongType,DoubleType,BooleanType,TimestampTypeandNullType, unless user provides a schema for this.excludeAttribute: Whether you want to exclude attributes in elements or not. Default is false.treatEmptyValuesAsNulls: (DEPRECATED: usenullValueset to"") Whether you want to treat whitespaces as a null value. Default is falsemode: The mode for dealing with corrupt records during parsing. Default isPERMISSIVE.PERMISSIVE:- When it encounters a corrupted record, it sets all fields to
nulland puts the malformed string into a new field configured bycolumnNameOfCorruptRecord. - When it encounters a field of the wrong datatype, it sets the offending field to
null. DROPMALFORMED: ignores the whole corrupted records.FAILFAST: throws an exception when it meets corrupted records.inferSchema: iftrue, attempts to infer an appropriate type for each resulting DataFrame column, like a boolean, numeric or date type. Iffalse, all resulting columns are of string type. Default istrue.columnNameOfCorruptRecord: The name of new field where malformed strings are stored. Default is_corrupt_record.
Note: this field should be present in the dataframe schema if it is passed explicitly, like this:
schema = StructType([StructField("my_field", TimestampType()), StructField("_corrupt_record", StringType())])
spark.read.format("xml").options(rowTag='item').schema(schema).load("file.xml")If schema is infered, this field is added automatically.
attributePrefix: The prefix for attributes so that we can differentiate attributes and elements. This will be the prefix for field names. Default is_. Can be empty, but only for reading XML.valueTag: The tag used for the value when there are attributes in the element having no child. Default is_VALUE.charset: Defaults to 'UTF-8' but can be set to other valid charset namesignoreSurroundingSpaces: Defines whether or not surrounding whitespaces from values being read should be skipped. Default is false.wildcardColName: Name of a column existing in the provided schema which is interpreted as a 'wildcard'.
It must have type string or array of strings. It will match any XML child element that is not otherwise matched by the schema. The XML of the child becomes the string value of the column. If an array, then all unmatched elements will be returned as an array of strings. As its name implies, it is meant to emulate XSD's xs:any type. Default is xs_any. New in 0.11.0.
rowValidationXSDPath: Path to an XSD file that is used to validate the XML for each row individually. Rows that fail to
validate are treated like parse errors as above. The XSD does not otherwise affect the schema provided, or inferred. Note that if the same local path is not already also visible on the executors in the cluster, then the XSD and any others it depends on should be added to the Spark executors with `SparkContext.addFile`:Unit). In this case, to use local XSD /foo/bar.xsd, call addFile("/foo/bar.xsd") and pass just "bar.xsd" as rowValidationXSDPath.
ignoreNamespace: If true, namespaces prefixes on XML elements and attributes are ignored. Tags `and` would,
for example, be treated as if both are just `. Note that, at the moment, namespaces cannot be ignored on the rowTag element, only its children. Note that XML parsing is in general not namespace-aware even if false. Defaults to false`. New in 0.11.0.
timestampFormat: Specifies an additional timestamp format that will be tried when parsing values asTimestampType
columns. The format is specified as described in DateTimeFormatter. Defaults to try several formats, including ISO_INSTANT, including variations with offset timezones or no timezone (defaults to UTC). New in 0.12.0. As of 0.16.0, if a custom format pattern is used without a timezone, the default Spark timezone specified by spark.sql.session.timeZone will be used.
timezone: identifier of timezone to be used when reading timestamps without a timezone specified. New in 0.16.0.
*…
Excerpt shown — open the source for the full document.