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 all 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
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
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
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 |
|---|---|---|
|
|
@@ -15,23 +15,19 @@ | |
| */ | ||
| package com.databricks.spark | ||
|
|
||
| import java.io.CharArrayWriter | ||
| import javax.xml.stream.XMLOutputFactory | ||
|
|
||
| import scala.collection.Map | ||
|
|
||
| import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter | ||
| import org.apache.hadoop.io.compress.CompressionCodec | ||
|
|
||
| import org.apache.spark.sql.{DataFrame, SQLContext} | ||
| import com.databricks.spark.xml.util.XmlFile | ||
| import com.databricks.spark.xml.parsers.StaxXmlGenerator | ||
|
|
||
| package object xml { | ||
| /** | ||
| * Adds a method, `xmlFile`, to [[SQLContext]] that allows reading XML data. | ||
| */ | ||
| implicit class XmlContext(sqlContext: SQLContext) extends Serializable { | ||
| @deprecated("Use DataFrameReader.read()", "0.4.0") | ||
| def xmlFile( | ||
| filePath: String, | ||
| rowTag: String = XmlOptions.DEFAULT_ROW_TAG, | ||
|
|
@@ -82,69 +78,16 @@ package object xml { | |
| // </fieldA> | ||
| // | ||
| // Namely, roundtrip in writing and reading can end up in different schema structure. | ||
| @deprecated("Use DataFrameWriter.write()", "0.4.0") | ||
| def saveAsXmlFile( | ||
| path: String, parameters: Map[String, String] = Map(), | ||
| compressionCodec: Class[_ <: CompressionCodec] = null): Unit = { | ||
| val options = XmlOptions(parameters.toMap) | ||
| val startElement = s"<${options.rootTag}>" | ||
| val endElement = s"</${options.rootTag}>" | ||
| val rowSchema = dataFrame.schema | ||
| val indent = XmlFile.DEFAULT_INDENT | ||
| val rowSeparator = XmlFile.DEFAULT_ROW_SEPARATOR | ||
|
|
||
| val xmlRDD = dataFrame.rdd.mapPartitions { iter => | ||
| val factory = XMLOutputFactory.newInstance() | ||
| val writer = new CharArrayWriter() | ||
| val xmlWriter = factory.createXMLStreamWriter(writer) | ||
| val indentingXmlWriter = new IndentingXMLStreamWriter(xmlWriter) | ||
| indentingXmlWriter.setIndentStep(indent) | ||
|
|
||
| new Iterator[String] { | ||
| var firstRow: Boolean = true | ||
| var lastRow: Boolean = true | ||
|
|
||
| override def hasNext: Boolean = iter.hasNext || firstRow || lastRow | ||
|
|
||
| override def next: String = { | ||
| if (iter.nonEmpty) { | ||
| val xml = { | ||
| StaxXmlGenerator( | ||
| rowSchema, | ||
| indentingXmlWriter, | ||
| options)(iter.next()) | ||
| writer.toString | ||
| } | ||
| writer.reset() | ||
|
|
||
| // Here it needs to add indentations for the start of each line, | ||
| // in order to insert the start element and end element. | ||
| val indentedXml = indent + xml.replaceAll(rowSeparator, rowSeparator + indent) | ||
| if (firstRow) { | ||
| firstRow = false | ||
| startElement + rowSeparator + indentedXml | ||
| } else { | ||
| indentedXml | ||
| } | ||
| } else { | ||
| indentingXmlWriter.close() | ||
| if (!firstRow) { | ||
| lastRow = false | ||
| endElement | ||
| } else { | ||
| // This means the iterator was initially empty. | ||
| firstRow = false | ||
| lastRow = false | ||
| "" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| compressionCodec match { | ||
| case null => xmlRDD.saveAsTextFile(path) | ||
| case codec => xmlRDD.saveAsTextFile(path, codec) | ||
| } | ||
| val mutableParams = collection.mutable.Map(parameters.toSeq: _*) | ||
| val safeCodec = mutableParams.get("codec") | ||
| .orElse(Option(compressionCodec).map(_.getCanonicalName)) | ||
| .orNull | ||
|
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. I moved below codes to |
||
| mutableParams.put("codec", safeCodec) | ||
| XmlFile.saveAsXmlFile(dataFrame, path, mutableParams.toMap) | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -85,7 +85,6 @@ private[xml] object StaxXmlGenerator { | |
| case (ByteType, v: Byte) => writer.writeCharacters(v.toString) | ||
| case (BooleanType, v: Boolean) => writer.writeCharacters(v.toString) | ||
| case (DateType, v) => writer.writeCharacters(v.toString) | ||
| case (udt: UserDefinedType[_], v) => writeElement(udt.sqlType, udt.serialize(v)) | ||
|
|
||
|
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. I had to remove this because it was hidden from 2.0. |
||
| // For the case roundtrip in reading and writing XML files, [[ArrayType]] cannot have | ||
| // [[ArrayType]] as element type. It always wraps the element with [[StructType]]. So, | ||
|
|
||
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
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.
Here, I moved the codec handling inside to
saveAsXmlFileso thatcodecoption inparameterscan be also concerned.