-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-45711][SQL] Introduce a mapper for avro compression codecs
### What changes were proposed in this pull request? Currently, Spark supported all the avro compression codecs, but the avro supported compression codecs and spark supported are not completely one-on-one due to Spark introduce the compression codecs `UNCOMPRESSED`. On the other hand, there are a lot of magic strings copy from avro compression codecs. This issue lead to developers need to manually maintain its consistency. It is easy to make mistakes and reduce development efficiency. ### Why are the changes needed? Let developers easy to use avro compression codecs. ### Does this PR introduce _any_ user-facing change? 'No'. Introduce a new class. ### How was this patch tested? Exists test cases. ### Was this patch authored or co-authored using generative AI tooling? 'No'. Closes #43562 from beliefer/SPARK-45711. Authored-by: Jiaan Geng <[email protected]> Signed-off-by: Jiaan Geng <[email protected]>
- Loading branch information
Showing
4 changed files
with
81 additions
and
15 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
connector/avro/src/main/java/org/apache/spark/sql/avro/AvroCompressionCodec.java
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,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.spark.sql.avro; | ||
|
||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.avro.file.DataFileConstants; | ||
|
||
/** | ||
* A mapper class from Spark supported avro compression codecs to avro compression codecs. | ||
*/ | ||
public enum AvroCompressionCodec { | ||
UNCOMPRESSED(DataFileConstants.NULL_CODEC), | ||
DEFLATE(DataFileConstants.DEFLATE_CODEC), | ||
SNAPPY(DataFileConstants.SNAPPY_CODEC), | ||
BZIP2(DataFileConstants.BZIP2_CODEC), | ||
XZ(DataFileConstants.XZ_CODEC), | ||
ZSTANDARD(DataFileConstants.ZSTANDARD_CODEC); | ||
|
||
private final String codecName; | ||
|
||
AvroCompressionCodec(String codecName) { | ||
this.codecName = codecName; | ||
} | ||
|
||
public String getCodecName() { | ||
return this.codecName; | ||
} | ||
|
||
private static final Map<String, String> codecNameMap = | ||
Arrays.stream(AvroCompressionCodec.values()).collect( | ||
Collectors.toMap(codec -> codec.name(), codec -> codec.name().toLowerCase(Locale.ROOT))); | ||
|
||
public String lowerCaseName() { | ||
return codecNameMap.get(this.name()); | ||
} | ||
|
||
public static AvroCompressionCodec fromString(String s) { | ||
return AvroCompressionCodec.valueOf(s.toUpperCase(Locale.ROOT)); | ||
} | ||
} |
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