forked from databricks/spark-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to specify a shorten name for compression codec
databricks#66 This PR adds the support for shorten names for compression codecs and added a `CompressionCodecs` class instead of the implicit function as its use is nor recommended. Author: hyukjinkwon <[email protected]> Closes databricks#67 from HyukjinKwon/ISSUE-66-shorten-names.
- Loading branch information
1 parent
27e7714
commit c0758fa
Showing
6 changed files
with
116 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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
49 changes: 49 additions & 0 deletions
49
src/main/scala/com/databricks/spark/xml/util/CompressionCodecs.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2014 Databricks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.databricks.spark.xml.util | ||
|
||
import scala.util.control.Exception._ | ||
|
||
import org.apache.hadoop.io.compress._ | ||
|
||
private[xml] object CompressionCodecs { | ||
private val shortCompressionCodecNames: Map[String, String] = { | ||
val codecMap = collection.mutable.Map.empty[String, String] | ||
allCatch toTry(codecMap += "bzip2" -> classOf[BZip2Codec].getName) | ||
allCatch toTry(codecMap += "gzip" -> classOf[GzipCodec].getName) | ||
allCatch toTry(codecMap += "lz4" -> classOf[Lz4Codec].getName) | ||
allCatch toTry(codecMap += "snappy" -> classOf[SnappyCodec].getName) | ||
codecMap.toMap | ||
} | ||
|
||
/** | ||
* Return the codec class of the given name. | ||
*/ | ||
def getCodecClass: String => Class[_ <: CompressionCodec] = { | ||
case null => null | ||
case codec => | ||
val codecName = shortCompressionCodecNames.getOrElse(codec.toLowerCase, codec) | ||
try { | ||
// scalastyle:off classforname | ||
Class.forName(codecName).asInstanceOf[Class[CompressionCodec]] | ||
// scalastyle:on classforname | ||
} catch { | ||
case e: ClassNotFoundException => | ||
throw new IllegalArgumentException(s"Codec [$codecName] is not " + | ||
s"available. Known codecs are ${shortCompressionCodecNames.keys.mkString(", ")}.") | ||
} | ||
} | ||
} |
This file contains 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
43 changes: 43 additions & 0 deletions
43
src/test/scala/com/databricks/spark/xml/util/CompressionCodecsSuite.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2014 Databricks | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.databricks.spark.xml.util | ||
|
||
import org.apache.hadoop.io.compress._ | ||
import org.apache.hadoop.util.VersionInfo | ||
import org.scalatest.FunSuite | ||
|
||
class CompressionCodecsSuite extends FunSuite { | ||
|
||
test("Get classes of compression codecs") { | ||
assert(CompressionCodecs.getCodecClass(classOf[GzipCodec].getName) == classOf[GzipCodec]) | ||
assert(CompressionCodecs.getCodecClass(classOf[BZip2Codec].getName) == classOf[BZip2Codec]) | ||
assert(CompressionCodecs.getCodecClass(classOf[SnappyCodec].getName) == classOf[SnappyCodec]) | ||
assume(VersionInfo.getVersion.take(1) >= "2", | ||
"Lz4 codec was added from Hadoop 2.x") | ||
val codecClassName = "org.apache.hadoop.io.compress.Lz4Codec" | ||
assert(CompressionCodecs.getCodecClass(codecClassName).getName == codecClassName) | ||
} | ||
|
||
test("Get classes of compression codecs with short names") { | ||
assert(CompressionCodecs.getCodecClass("GzIp") == classOf[GzipCodec]) | ||
assert(CompressionCodecs.getCodecClass("bZip2") == classOf[BZip2Codec]) | ||
assert(CompressionCodecs.getCodecClass("Snappy") == classOf[SnappyCodec]) | ||
assume(VersionInfo.getVersion.take(1) >= "2", | ||
"Lz4 codec was added from Hadoop 2.x") | ||
val codecClassName = "org.apache.hadoop.io.compress.Lz4Codec" | ||
assert(CompressionCodecs.getCodecClass("lz4").getName == codecClassName) | ||
} | ||
} |