This repository was archived by the owner on Mar 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Upgrade spark 2.0 and prepare 0.4.0-SNAPSHOT #150
Closed
Closed
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
341073e
Migrate to Spark 2.0
HyukjinKwon ad6c238
Update documentation
HyukjinKwon 29acc30
Update indentation
HyukjinKwon e6c8bec
Remove unused imports
HyukjinKwon 227a652
Clean up tests for deprecated API
HyukjinKwon 67a5c31
Deprecate xmlFile in package.scala
HyukjinKwon c243192
Resolve conflicts
HyukjinKwon 268bfe9
Fetch upstream
HyukjinKwon ed9402f
Remove unused comments and change the hadoop version to test
HyukjinKwon 66bec86
Resolve conflicts
HyukjinKwon 85eb1c1
Clean up the tests
HyukjinKwon 0678014
Fix documentation again
HyukjinKwon b7f00b2
Revert documentation change for now
HyukjinKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,23 +10,25 @@ The structure and test tools are mostly copied from [CSV Data Source for Spark]( | |
|
|
||
| ## Requirements | ||
|
|
||
| This library requires Spark 1.3+ | ||
| This library requires Spark 2.0+ for 0.4.x. For Spark 1.3.+, 0.3.x version works with it. | ||
|
|
||
|
|
||
| ## Linking | ||
| You can link against this library in your program at the following coordinates: | ||
|
|
||
| ### Scala 2.10 | ||
|
|
||
| ``` | ||
| groupId: com.databricks | ||
| artifactId: spark-xml_2.10 | ||
| version: 0.3.3 | ||
| version: 0.4.0-SNAPSHOT | ||
| ``` | ||
| ### Scala 2.11 | ||
|
|
||
| ``` | ||
| groupId: com.databricks | ||
| artifactId: spark-xml_2.11 | ||
| version: 0.3.3 | ||
| version: 0.4.0-SNAPSHOT | ||
| ``` | ||
|
|
||
| ## Using with Spark shell | ||
|
|
@@ -169,7 +171,6 @@ OPTIONS (path "books.xml", rowTag "book") | |
| ``` | ||
|
|
||
| ### Scala API | ||
| __Spark 1.4+:__ | ||
|
|
||
| ```scala | ||
| import org.apache.spark.sql.SQLContext | ||
|
|
@@ -218,50 +219,7 @@ selectedData.write | |
| .save("newbooks.xml") | ||
| ``` | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
| __Spark 1.3:__ | ||
|
|
||
| ```scala | ||
| import org.apache.spark.sql.SQLContext | ||
|
|
||
| val sqlContext = new SQLContext(sc) | ||
| val df = sqlContext.load( | ||
| "com.databricks.spark.xml", | ||
| Map("path" -> "books.xml", "rowTag" -> "book")) | ||
|
|
||
| val selectedData = df.select("author", "@id") | ||
| selectedData.save("com.databricks.spark.xml", | ||
| SaveMode.ErrorIfExists, | ||
| Map("path" -> "newbooks.xml", "rootTag" -> "books", "rowTag" -> "book")) | ||
| ``` | ||
|
|
||
| You can manually specify the schema when reading data: | ||
| ```scala | ||
| import org.apache.spark.sql.SQLContext | ||
| import org.apache.spark.sql.types.{StructType, StructField, StringType, IntegerType}; | ||
|
|
||
| val sqlContext = new SQLContext(sc) | ||
| val customSchema = StructType(Array( | ||
| StructField("@id", StringType, nullable = true), | ||
| StructField("author", StringType, nullable = true), | ||
| StructField("description", StringType, nullable = true), | ||
| StructField("genre", StringType ,nullable = true), | ||
| StructField("price", DoubleType, nullable = true), | ||
| StructField("publish_date", StringType, nullable = true), | ||
| StructField("title", StringType, nullable = true))) | ||
|
|
||
| val df = sqlContext.load( | ||
| "com.databricks.spark.xml", | ||
| schema = customSchema, | ||
| Map("path" -> "books.xml", "rowTag" -> "book")) | ||
|
|
||
| val selectedData = df.select("author", "@id") | ||
| selectedData.save("com.databricks.spark.xml", | ||
| SaveMode.ErrorIfExists, | ||
| Map("path" -> "newbooks.xml", "rootTag" -> "books", "rowTag" -> "book")) | ||
| ``` | ||
|
|
||
| ### Java API | ||
| __Spark 1.4+:__ | ||
|
|
||
| ```java | ||
| import org.apache.spark.sql.SQLContext | ||
|
|
@@ -309,58 +267,8 @@ df.select("author", "@id").write() | |
| ``` | ||
|
|
||
|
|
||
|
|
||
| __Spark 1.3:__ | ||
|
|
||
| ```java | ||
| import org.apache.spark.sql.SQLContext | ||
|
|
||
| SQLContext sqlContext = new SQLContext(sc); | ||
|
|
||
| HashMap<String, String> options = new HashMap<String, String>(); | ||
| options.put("rowTag", "book"); | ||
| options.put("path", "books.xml"); | ||
| DataFrame df = sqlContext.load("com.databricks.spark.xml", options); | ||
|
|
||
| HashMap<String, String> options = new HashMap<String, String>(); | ||
| options.put("rowTag", "book"); | ||
| options.put("rootTag", "books"); | ||
| options.put("path", "newbooks.xml"); | ||
| df.select("author", "@id").save("com.databricks.spark.xml", SaveMode.ErrorIfExists, options) | ||
| ``` | ||
|
|
||
| You can manually specify schema: | ||
| ```java | ||
| import org.apache.spark.sql.SQLContext; | ||
| import org.apache.spark.sql.types.*; | ||
|
|
||
| SQLContext sqlContext = new SQLContext(sc); | ||
| StructType customSchema = new StructType(new StructField[] { | ||
| new StructField("@id", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("author", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("description", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("genre", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("price", DataTypes.DoubleType, true, Metadata.empty()), | ||
| new StructField("publish_date", DataTypes.StringType, true, Metadata.empty()), | ||
| new StructField("title", DataTypes.StringType, true, Metadata.empty()) | ||
| }); | ||
|
|
||
| HashMap<String, String> options = new HashMap<String, String>(); | ||
| options.put("rowTag", "book"); | ||
| options.put("path", "books.xml"); | ||
| DataFrame df = sqlContext.load("com.databricks.spark.xml", customSchema, options); | ||
|
|
||
| HashMap<String, String> options = new HashMap<String, String>(); | ||
| options.put("rowTag", "book"); | ||
| options.put("rootTag", "books"); | ||
| options.put("path", "newbooks.xml"); | ||
| df.select("author", "@id").save("com.databricks.spark.xml", SaveMode.ErrorIfExists, options) | ||
| ``` | ||
|
|
||
| ### Python API | ||
|
|
||
| __Spark 1.4+:__ | ||
|
|
||
| ```python | ||
| from pyspark.sql import SQLContext | ||
| sqlContext = SQLContext(sc) | ||
|
|
@@ -399,38 +307,7 @@ df.select("author", "@id").write \ | |
| ``` | ||
|
|
||
|
|
||
| __Spark 1.3:__ | ||
|
|
||
| ```python | ||
| from pyspark.sql import SQLContext | ||
| sqlContext = SQLContext(sc) | ||
|
|
||
| df = sqlContext.load(source="com.databricks.spark.xml", rowTag = 'book', path = 'books.xml') | ||
| df.select("author", "@id").save('newbooks.xml', rootTag = 'books', rowTag = 'book', path = 'newbooks.xml') | ||
| ``` | ||
|
|
||
| You can manually specify schema: | ||
| ```python | ||
| from pyspark.sql import SQLContext | ||
| from pyspark.sql.types import * | ||
|
|
||
| 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.load(source="com.databricks.spark.xml", rowTag = 'book', schema = customSchema, path = 'books.xml') | ||
| df.select("author", "@id").save('newbooks.xml', rootTag = 'books', rowTag = 'book', path = 'newbooks.xml') | ||
| ``` | ||
|
|
||
|
|
||
| ### R API | ||
| __Spark 1.4+:__ | ||
|
|
||
| Automatically infer schema (data types) | ||
| ```R | ||
|
|
@@ -492,4 +369,3 @@ This library is built with [SBT](http://www.scala-sbt.org/0.13/docs/Command-Line | |
| ## Acknowledgements | ||
|
|
||
| This project was initially created by [HyukjinKwon](https://github.com/HyukjinKwon) and donated to [Databricks](https://databricks.com). | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,9 +90,7 @@ class DefaultSource | |
| } | ||
| if (doSave) { | ||
| // Only save data when the save mode is not ignore. | ||
| val codecClass = | ||
| CompressionCodecs.getCodecClass(XmlOptions(parameters).codec) | ||
| data.saveAsXmlFile(filesystemPath.toString, parameters, codecClass) | ||
| XmlFile.saveAsXmlFile(data, filesystemPath.toString, parameters) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, I moved the codec handling inside to |
||
| } | ||
| createRelation(sqlContext, parameters, data.schema) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,12 +70,7 @@ private[xml] class XmlRecordReader extends RecordReader[LongWritable, Text] { | |
|
|
||
| override def initialize(split: InputSplit, context: TaskAttemptContext): Unit = { | ||
| val fileSplit: FileSplit = split.asInstanceOf[FileSplit] | ||
| val conf: Configuration = { | ||
| // Use reflection to get the Configuration. This is necessary because TaskAttemptContext is | ||
| // a class in Hadoop 1.x and an interface in Hadoop 2.x. | ||
| val method = context.getClass.getMethod("getConfiguration") | ||
| method.invoke(context).asInstanceOf[Configuration] | ||
| } | ||
| val conf: Configuration = context.getConfiguration | ||
| val charset = | ||
| Charset.forName(conf.get(XmlInputFormat.ENCODING_KEY, XmlOptions.DEFAULT_CHARSET)) | ||
| startTag = conf.get(XmlInputFormat.START_TAG_KEY).getBytes(charset) | ||
|
|
@@ -97,25 +92,18 @@ private[xml] class XmlRecordReader extends RecordReader[LongWritable, Text] { | |
| val codec = new CompressionCodecFactory(conf).getCodec(path) | ||
| if (null != codec) { | ||
| decompressor = CodecPool.getDecompressor(codec) | ||
| // Use reflection to get the splittable compression codec and stream. This is necessary | ||
| // because SplittableCompressionCodec does not exist in Hadoop 1.0.x. | ||
| def isSplitCompressionCodec(obj: Any) = { | ||
| val splittableClassName = "org.apache.hadoop.io.compress.SplittableCompressionCodec" | ||
| obj.getClass.getInterfaces.map(_.getName).contains(splittableClassName) | ||
| } | ||
| // Here I made separate variables to avoid to try to find SplitCompressionInputStream at | ||
| // runtime. | ||
| val (inputStream, seekable) = codec match { | ||
| case c: CompressionCodec if isSplitCompressionCodec(c) => | ||
| // At Hadoop 1.0.x, this case would not be executed. | ||
| val cIn = { | ||
| val sc = c.asInstanceOf[SplittableCompressionCodec] | ||
| sc.createInputStream(fsin, decompressor, start, | ||
| end, SplittableCompressionCodec.READ_MODE.BYBLOCK) | ||
| } | ||
| codec match { | ||
| case sc: SplittableCompressionCodec => | ||
| val cIn = sc.createInputStream( | ||
| fsin, | ||
| decompressor, | ||
| start, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big change. I just removed the reflection part that was needed for Hadoop 1.x. |
||
| end, | ||
| SplittableCompressionCodec.READ_MODE.BYBLOCK) | ||
| start = cIn.getAdjustedStart | ||
| end = cIn.getAdjustedEnd | ||
| (cIn, cIn) | ||
| in = cIn | ||
| filePosition = cIn | ||
| case c: CompressionCodec => | ||
| if (start != 0) { | ||
| // So we have a split that is only part of a file stored using | ||
|
|
@@ -124,10 +112,9 @@ private[xml] class XmlRecordReader extends RecordReader[LongWritable, Text] { | |
| codec.getClass.getSimpleName + " compressed stream") | ||
| } | ||
| val cIn = c.createInputStream(fsin, decompressor) | ||
| (cIn, fsin) | ||
| in = cIn | ||
| filePosition = fsin | ||
| } | ||
| in = inputStream | ||
| filePosition = seekable | ||
| } else { | ||
| in = fsin | ||
| filePosition = fsin | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will create a branch for
0.3.xsoon and correct this documentation.