This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/seatunnel.git
The following commit(s) were added to refs/heads/dev by this push:
new 436ddea93d [Feature][Connector-V2][Hive] Support opt-in Parquet table
compression inheritance (#13129)
436ddea93d is described below
commit 436ddea93d3816ef23e38b6850782ffdc46a5a80
Author: taowenjun <[email protected]>
AuthorDate: Thu Nov 26 13:44:52 2026 +0000
[Feature][Connector-V2][Hive] Support opt-in Parquet table compression
inheritance (#13129)
Co-authored-by: 陶文俊 <[email protected]>
---
docs/en/connectors/sink/Hive.md | 18 +-
docs/zh/connectors/sink/Hive.md | 20 +-
.../seatunnel/hive/config/HiveSinkOptions.java | 2 +-
.../connectors/seatunnel/hive/sink/HiveSink.java | 30 ++-
.../seatunnel/hive/sink/HiveSinkConfigTest.java | 238 ++++++++++++++++++++-
.../seatunnel/e2e/connector/hive/HiveIT.java | 92 ++++++++
.../resources/fake_to_hive_parquet_inherited.conf | 59 +++++
.../resources/fake_to_hive_parquet_override.conf | 60 ++++++
8 files changed, 490 insertions(+), 29 deletions(-)
diff --git a/docs/en/connectors/sink/Hive.md b/docs/en/connectors/sink/Hive.md
index 87ce535ec1..e9f98cdede 100644
--- a/docs/en/connectors/sink/Hive.md
+++ b/docs/en/connectors/sink/Hive.md
@@ -41,8 +41,8 @@ By default, we use 2PC commit to ensure `exactly-once`
|---------------------------------------|---------|----------|----------------|-------------|
| table_name | string | yes | -
| Target Hive table name, e.g. `db1.table1`. For multi-table mode, you can use
`${database_name}.${table_name}` and SeaTunnel will substitute the upstream
values. |
| metastore_uri | string | no | -
| Hive metastore URI. Required unless `hive.metastore.client.factory.class` is
configured through `hive_site_path`, `hive.hadoop.conf-path`, or
`hive.hadoop.conf`. Comma-separated values enable HA failover. |
-| compress_codec | string | no | none
| Explicit output compression codec. ORC supports `NONE`, `SNAPPY`, `ZLIB`,
`LZ4` and `LZO`. Defaults to `none`; see `use_table_compression` to inherit
compression from an existing ORC table. |
-| use_table_compression | boolean | no | false
| Inherit `orc.compress` from an existing ORC table when no explicit codec is
configured. |
+| compress_codec | string | no | none
| Explicit output compression codec. ORC supports `NONE`, `SNAPPY`, `ZLIB`,
`LZ4` and `LZO`. Defaults to `none`; see `use_table_compression` to inherit
compression from an existing ORC or Parquet table. |
+| use_table_compression | boolean | no | false
| Inherit `orc.compress` or `parquet.compression` from an existing table when
no explicit codec is configured. |
| hdfs_site_path | string | no | -
| Local path of `hdfs-site.xml`. Deprecated for new jobs — prefer
`hive.hadoop.conf` or `hive.hadoop.conf-path`. |
| hive_site_path | string | no | -
| Local path of `hive-site.xml`. |
| hive.hadoop.conf | Map | no | -
| Inline Hadoop configuration properties. |
@@ -61,18 +61,20 @@ By default, we use 2PC commit to ensure `exactly-once`
### use_table_compression [boolean]
-Default: `false`. Enable this option to inherit `orc.compress` from the
metastore when preparing an **existing ORC table**. It does not change
automatic table creation, non-ORC tables, or table metadata.
+Default: `false`. Enable this option to inherit compression from an **existing
ORC or Parquet table** when the sink is initialized. The property is
`orc.compress` for ORC and `parquet.compression` for Parquet. Table metadata is
not modified.
-For non-ORC tables, `use_table_compression` is ignored: no table compression
property is inherited or validated. If the table does not exist when the sink
is initialized and follows the automatic table-creation path, inheritance is
also skipped, including `orc.compress` declared in `save_mode_create_template`.
In both cases, enabling this option alone does not cause the job to fail;
explicit `compress_codec` handling, existing defaults, and normal configuration
validation and table-creat [...]
+Priority: explicit `compress_codec` (including `NONE`) > the format-specific
table property when enabled > the existing default `NONE`. Values are trimmed
and matched without case sensitivity. Missing, null or blank properties retain
the default. Parquet accepts `NONE`, `UNCOMPRESSED` (an alias for `NONE`),
`SNAPPY`, `GZIP`, `LZO`, `LZ4`, `BROTLI` and `ZSTD`. Inherited `ZLIB` is
rejected for Parquet because it maps to uncompressed output. Unknown or
incompatible inherited codecs fail dur [...]
-Priority: explicit `compress_codec` (including `NONE`) > table `orc.compress`
when enabled > the existing default `NONE`. Table values are trimmed and
matched without case sensitivity. Missing, null or blank properties retain the
default. Unknown values or codecs without a supported ORC mapping fail during
sink initialization instead of silently writing uncompressed files. In versions
where ORC ZSTD maps to NONE, inherited ZSTD is rejected; verify your SeaTunnel
version supports ORC ZSTD [...]
+Other formats are skipped without validating either property. When the table
does not exist at initialization and follows automatic creation, inheritance is
also skipped, including properties in `save_mode_create_template`. Enabling
this option alone does not cause these skip cases to fail; explicit codec
handling and normal configuration/table-creation validation still apply.
-For an existing table created with `TBLPROPERTIES ('orc.compress'='SNAPPY')`:
+ORC behavior remains unchanged: codecs without a supported ORC mapping are
rejected. In versions where ORC ZSTD maps to NONE, inherited ZSTD is rejected;
verify your SeaTunnel version supports ORC ZSTD before using it.
+
+For an existing table created with `STORED AS PARQUET TBLPROPERTIES
('parquet.compression'='SNAPPY')`:
```hocon
sink {
Hive {
- table_name = "default.existing_orc_table"
+ table_name = "default.existing_parquet_table"
metastore_uri = "thrift://metastore:9083"
use_table_compression = true
# Optional: compress_codec = "NONE" overrides the table property.
@@ -80,6 +82,8 @@ sink {
}
```
+The same option applies to an existing ORC table with `TBLPROPERTIES
('orc.compress'='SNAPPY')`.
+
### table_name [string]
Target Hive table name eg: db1.table1, and if the source is multiple mode, you
can use `${database_name}.${table_name}` to generate the table name, it will
replace the `${database_name}` and `${table_name}` with the value of the
CatalogTable generate from the source.
diff --git a/docs/zh/connectors/sink/Hive.md b/docs/zh/connectors/sink/Hive.md
index 6675158de9..880de265b0 100644
--- a/docs/zh/connectors/sink/Hive.md
+++ b/docs/zh/connectors/sink/Hive.md
@@ -41,8 +41,8 @@ import ChangeLog from '../changelog/connector-hive.md';
|---------------------------------------|---------|----|----------------|---------------------------------------------------------------------------------|
| table_name | string | 是 | - | 目标
Hive 表名,例如 `db1.table1`。多表模式下可使用 `${database_name}.${table_name}`,SeaTunnel
会用上游值进行替换。 |
| metastore_uri | string | 否 | - |
Hive Metastore URI。当未通过 `hive_site_path`、`hive.hadoop.conf-path` 或
`hive.hadoop.conf` 配置 `hive.metastore.client.factory.class` 时必填。多个值用逗号分隔可启用 HA
故障转移。 |
-| compress_codec | string | 否 | none |
显式指定输出压缩格式,优先于表属性。ORC 支持 `NONE`、`SNAPPY`、`ZLIB`、`LZ4` 和 `LZO`;继承表属性需开启
`use_table_compression`。 |
-| use_table_compression | boolean | 否 | false |
开启后继承已有 ORC 表的 `orc.compress` 属性;显式 `compress_codec` 优先。非 ORC 表及自动建表流程跳过继承。 |
+| compress_codec | string | 否 | none |
显式指定输出压缩格式,优先于表属性。支持的格式见下方说明;继承表属性需开启 `use_table_compression`。 |
+| use_table_compression | boolean | 否 | false |
未显式设置 codec 时继承已有 ORC/Parquet 表的压缩属性;其他格式及自动建表流程跳过继承。 |
| hdfs_site_path | string | 否 | - |
`hdfs-site.xml` 的本地路径。新作业不建议使用——优先使用 `hive.hadoop.conf` 或
`hive.hadoop.conf-path`。 |
| hive_site_path | string | 否 | - |
`hive-site.xml` 的本地路径。 |
| hive.hadoop.conf | Map | 否 | - |
内联的 Hadoop 配置属性。 |
@@ -61,22 +61,24 @@ import ChangeLog from '../changelog/connector-hive.md';
### compress_codec [string]
-显式指定输出压缩格式,默认 `none`。ORC 支持 `NONE`、`SNAPPY`、`ZLIB`、`LZ4` 和
`LZO`。配置后优先于表属性;继承表属性请参阅 `use_table_compression`。
+显式指定输出压缩格式,默认 `none`。Parquet 支持的继承压缩格式见 `use_table_compression`。ORC 支持
`NONE`、`SNAPPY`、`ZLIB`、`LZ4` 和 `LZO`。配置后优先于表属性;继承表属性请参阅 `use_table_compression`。
### use_table_compression [boolean]
-默认值:`false`。开启后,初始化**已有 ORC 表**时从 metastore 继承 `orc.compress`。此选项不改变自动建表流程、非
ORC 表或表元数据。
+默认值:`false`。开启后,在 sink 初始化时从**已有 ORC 或 Parquet 表**继承压缩属性:ORC 使用
`orc.compress`,Parquet 使用 `parquet.compression`。不会修改表元数据。
-对于非 ORC 表,`use_table_compression` 会被忽略,不会继承或校验表的压缩属性。如果 sink
初始化时表尚不存在并走自动建表流程,也会跳过压缩继承,包括 `save_mode_create_template` 中声明的
`orc.compress`。这两种情况均不会仅因开启此选项而导致任务失败;显式 `compress_codec`
的处理方式、原有默认值以及正常的配置校验和建表错误仍然适用。
+优先级:显式 `compress_codec`(包括 `NONE`)> 开启继承后的对应格式表属性 > 原默认值
`NONE`。表属性去除首尾空白后按大小写不敏感匹配;属性缺失、为 null 或为空白时保留默认值。Parquet 支持继承
`NONE`、`UNCOMPRESSED`(映射为 `NONE`)、`SNAPPY`、`GZIP`、`LZO`、`LZ4`、`BROTLI` 和
`ZSTD`。Parquet 拒绝继承 `ZLIB`,因为其映射会静默写为不压缩。未知或不兼容的继承值在 sink 初始化时报错;显式 codec
沿用原有校验行为。运行环境的原生 codec 依赖及下游读取器必须支持所选格式。
-优先级:显式 `compress_codec`(包括 `NONE`)> 开启继承后的表属性 `orc.compress` > 原默认值
`NONE`。表属性去除首尾空白后按大小写不敏感匹配;属性缺失、为 null 或为空白时保留默认值。未知值或没有有效 ORC 映射的压缩格式在 sink
初始化时明确报错,避免静默写为不压缩。在 ORC ZSTD 仍映射为 NONE 的版本中,继承 ZSTD 会报错;使用该表属性前请确认 SeaTunnel
版本支持 ORC ZSTD。下游读取器也必须支持所选压缩格式。
+其他文件格式会跳过继承,也不校验这两项表属性。初始化时表尚不存在并走自动建表流程时,也会跳过继承,包括
`save_mode_create_template` 中声明的压缩属性。这些情况不会仅因开启选项而失败;显式 codec
处理、正常配置校验和建表错误仍然适用。
-对于通过 `TBLPROPERTIES ('orc.compress'='SNAPPY')` 创建的已有表:
+ORC 行为保持不变:拒绝没有有效 ORC 映射的继承值。在 ORC ZSTD 仍映射为 NONE 的版本中,继承 ZSTD 会报错;使用前请确认
SeaTunnel 版本支持 ORC ZSTD。
+
+对于通过 `STORED AS PARQUET TBLPROPERTIES ('parquet.compression'='SNAPPY')` 创建的已有表:
```hocon
sink {
Hive {
- table_name = "default.existing_orc_table"
+ table_name = "default.existing_parquet_table"
metastore_uri = "thrift://metastore:9083"
use_table_compression = true
# 可选:compress_codec = "NONE" 覆盖表属性。
@@ -84,6 +86,8 @@ sink {
}
```
+同一选项也适用于配置 `TBLPROPERTIES ('orc.compress'='SNAPPY')` 的已有 ORC 表。
+
### table_name [string]
目标 Hive 表名,例如:`db1.table1`。如果源是多模式,您可以使用 `${database_name}.${table_name}`
来生成表名,它将用源生成的 CatalogTable 的值替换 `${database_name}` 和 `${table_name}`。
diff --git
a/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/config/HiveSinkOptions.java
b/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/config/HiveSinkOptions.java
index 61933ddc6c..dc7ac612c5 100644
---
a/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/config/HiveSinkOptions.java
+++
b/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/config/HiveSinkOptions.java
@@ -29,7 +29,7 @@ public class HiveSinkOptions extends HiveBaseOptions {
.booleanType()
.defaultValue(false)
.withDescription(
- "Use the orc.compress property of an existing ORC
table when compress_codec is not explicitly configured. "
+ "Use orc.compress for an existing ORC table or
parquet.compression for an existing Parquet table when compress_codec is not
explicitly configured. "
+ "Missing or blank table properties
retain the default compression codec.");
public static final Option<Boolean> ABORT_DROP_PARTITION_METADATA =
diff --git
a/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSink.java
b/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSink.java
index 31e6d38211..e00cc5f792 100644
---
a/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSink.java
+++
b/seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSink.java
@@ -54,6 +54,7 @@ import
org.apache.seatunnel.connectors.seatunnel.hive.utils.HiveTableUtils;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.orc.CompressionKind;
+import org.apache.parquet.hadoop.metadata.CompressionCodecName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -230,41 +231,50 @@ public class HiveSink
}
/**
- * Inherits compression only while preparing an existing ORC table.
Explicit codecs, including
- * NONE, take precedence. Rejects enum values mapped to ORC NONE to
prevent silent compression
+ * Inherits compression only while preparing an existing ORC or Parquet
table. Explicit codecs,
+ * including NONE, take precedence. Rejects incompatible mappings to
prevent silent compression
* loss. Returns the original config or an immutable replacement without
changing table
* metadata.
*/
private Config inheritTableCompression(
Config pluginConfig, FileFormat fileFormat, Table table) {
if (!readonlyConfig.get(HiveSinkOptions.USE_TABLE_COMPRESSION)
- || fileFormat != FileFormat.ORC
+ || (fileFormat != FileFormat.ORC && fileFormat !=
FileFormat.PARQUET)
|| pluginConfig.hasPath(COMPRESS_CODEC.key())) {
return pluginConfig;
}
Map<String, String> parameters = table.getParameters();
- String codec = parameters == null ? null :
parameters.get("orc.compress");
+ String property = fileFormat == FileFormat.ORC ? "orc.compress" :
"parquet.compression";
+ String codec = parameters == null ? null : parameters.get(property);
if (codec == null || codec.trim().isEmpty()) {
return pluginConfig;
}
CompressFormat compression;
+ String normalizedCodec = codec.trim().toUpperCase(Locale.ROOT);
+ if (fileFormat == FileFormat.PARQUET &&
"UNCOMPRESSED".equals(normalizedCodec)) {
+ normalizedCodec = CompressFormat.NONE.name();
+ }
try {
- compression =
CompressFormat.valueOf(codec.trim().toUpperCase(Locale.ROOT));
+ compression = CompressFormat.valueOf(normalizedCodec);
} catch (IllegalArgumentException e) {
throw new HiveConnectorException(
CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT,
String.format(
- "Unsupported orc.compress '%s' for Hive table
%s.%s",
- codec, table.getDbName(), table.getTableName()),
+ "Unsupported %s '%s' for Hive table %s.%s",
+ property, codec, table.getDbName(),
table.getTableName()),
e);
}
if (compression != CompressFormat.NONE
- && compression.getOrcCompression() == CompressionKind.NONE) {
+ && ((fileFormat == FileFormat.ORC
+ && compression.getOrcCompression() ==
CompressionKind.NONE)
+ || (fileFormat == FileFormat.PARQUET
+ && compression.getParquetCompression()
+ ==
CompressionCodecName.UNCOMPRESSED))) {
throw new HiveConnectorException(
CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT,
String.format(
- "Unsupported orc.compress '%s' for Hive table
%s.%s",
- codec, table.getDbName(), table.getTableName()));
+ "Unsupported %s '%s' for Hive table %s.%s",
+ property, codec, table.getDbName(),
table.getTableName()));
}
return pluginConfig.withValue(
COMPRESS_CODEC.key(),
ConfigValueFactory.fromAnyRef(compression.name()));
diff --git
a/seatunnel-connectors-v2/connector-hive/src/test/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSinkConfigTest.java
b/seatunnel-connectors-v2/connector-hive/src/test/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSinkConfigTest.java
index 799214f095..c6cf576a75 100644
---
a/seatunnel-connectors-v2/connector-hive/src/test/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSinkConfigTest.java
+++
b/seatunnel-connectors-v2/connector-hive/src/test/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSinkConfigTest.java
@@ -31,18 +31,39 @@ import
org.apache.seatunnel.connectors.seatunnel.hive.config.HiveSinkOptions;
import
org.apache.seatunnel.connectors.seatunnel.hive.exception.HiveConnectorException;
import org.apache.seatunnel.connectors.seatunnel.hive.utils.HiveTableUtils;
+import org.apache.avro.Schema;
+import org.apache.avro.SchemaBuilder;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.SerDeInfo;
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.orc.CompressionKind;
+import org.apache.parquet.avro.AvroParquetReader;
+import org.apache.parquet.avro.AvroParquetWriter;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.ParquetReader;
+import org.apache.parquet.hadoop.ParquetWriter;
+import org.apache.parquet.hadoop.metadata.BlockMetaData;
+import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData;
+import org.apache.parquet.hadoop.metadata.CompressionCodecName;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.OutputFile;
+import org.apache.parquet.io.PositionOutputStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
+import java.io.IOException;
+import java.io.OutputStream;
import java.lang.reflect.Field;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -51,6 +72,133 @@ import java.util.Map;
/** Unit tests for HiveSink config generation. */
public class HiveSinkConfigTest {
+ @TempDir java.nio.file.Path tempDirectory;
+
+ @Test
+ void testParquetCompressionFooterAndRows() throws Exception {
+ for (String scenario : new String[] {"inherited", "override",
"uncompressed", "gzip"}) {
+ String tableCodec =
+ "uncompressed".equals(scenario)
+ ? "UNCOMPRESSED"
+ : "gzip".equals(scenario) ? "GZIP" : "SNAPPY";
+ FileSinkConfig config =
+ parquetCompression(
+ true, "override".equals(scenario) ? "NONE" : null,
tableCodec);
+ CompressionCodecName expected =
+ "inherited".equals(scenario)
+ ? CompressionCodecName.SNAPPY
+ : "gzip".equals(scenario)
+ ? CompressionCodecName.GZIP
+ : CompressionCodecName.UNCOMPRESSED;
+ java.nio.file.Path file = tempDirectory.resolve(scenario +
".parquet");
+ Schema schema =
+ SchemaBuilder.record("test_row")
+ .fields()
+ .requiredString("id")
+ .optionalString("name")
+ .endRecord();
+ try (ParquetWriter<GenericRecord> writer =
+
AvroParquetWriter.<GenericRecord>builder(localOutputFile(file))
+ .withSchema(schema)
+ .withConf(new Configuration())
+ .withCompressionCodec(
+
config.getCompressFormat().getParquetCompression())
+ .build()) {
+ for (int id = 1; id <= 3; id++) {
+ GenericRecord row = new GenericData.Record(schema);
+ row.put("id", String.valueOf(id));
+ row.put("name", id == 2 ? null : "name-" + id);
+ writer.write(row);
+ }
+ }
+ try (ParquetFileReader reader =
+ ParquetFileReader.open(
+ HadoopInputFile.fromPath(
+ new Path(file.toUri()), new
Configuration()))) {
+
Assertions.assertFalse(reader.getFooter().getBlocks().isEmpty());
+ long rows = 0;
+ for (BlockMetaData block : reader.getFooter().getBlocks()) {
+ rows += block.getRowCount();
+ Assertions.assertFalse(block.getColumns().isEmpty());
+ for (ColumnChunkMetaData column : block.getColumns()) {
+ Assertions.assertEquals(expected, column.getCodec());
+ }
+ }
+ Assertions.assertEquals(3, rows);
+ }
+ try (ParquetReader<GenericRecord> reader =
+ AvroParquetReader.<GenericRecord>builder(new
Path(file.toUri()))
+ .withConf(new Configuration())
+ .build()) {
+ for (int id = 1; id <= 3; id++) {
+ GenericRecord row = reader.read();
+ Assertions.assertNotNull(row);
+ Assertions.assertEquals(String.valueOf(id),
row.get("id").toString());
+ Assertions.assertEquals(
+ id == 2 ? null : "name-" + id,
+ row.get("name") == null ? null :
row.get("name").toString());
+ }
+ Assertions.assertNull(reader.read());
+ }
+ }
+ }
+
+ // Avoid Hadoop's native permission helpers when writing local test files
on Windows.
+ private static OutputFile localOutputFile(java.nio.file.Path file) {
+ return new OutputFile() {
+ @Override
+ public PositionOutputStream create(long blockSizeHint) throws
IOException {
+ OutputStream stream =
+ Files.newOutputStream(file,
java.nio.file.StandardOpenOption.CREATE_NEW);
+ return new PositionOutputStream() {
+ private long position;
+
+ @Override
+ public long getPos() {
+ return position;
+ }
+
+ @Override
+ public void write(int value) throws IOException {
+ stream.write(value);
+ position++;
+ }
+
+ @Override
+ public void write(byte[] bytes, int offset, int length)
throws IOException {
+ stream.write(bytes, offset, length);
+ position += length;
+ }
+
+ @Override
+ public void flush() throws IOException {
+ stream.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ stream.close();
+ }
+ };
+ }
+
+ @Override
+ public PositionOutputStream createOrOverwrite(long blockSizeHint)
throws IOException {
+ return create(blockSizeHint);
+ }
+
+ @Override
+ public boolean supportsBlockSize() {
+ return false;
+ }
+
+ @Override
+ public long defaultBlockSize() {
+ return 0;
+ }
+ };
+ }
+
@Test
void testTableCompressionDisabledByDefault() throws Exception {
Assertions.assertEquals(
@@ -137,6 +285,92 @@ public class HiveSinkConfigTest {
private static FileSinkConfig compressionConfig(
Boolean enabled, String explicitCodec, String tableCodec,
FileFormat format)
throws Exception {
+ Map<String, String> parameters = new HashMap<>();
+ if (tableCodec != null) {
+ parameters.put("orc.compress", tableCodec);
+ }
+ return compressionConfig(enabled, explicitCodec, format, parameters);
+ }
+
+ @Test
+ void testInheritParquetCompression() throws Exception {
+ for (String codec :
+ new String[] {"SNAPPY", "GZIP", "ZSTD", "LZ4", "LZO",
"BROTLI", "NONE"}) {
+ Assertions.assertEquals(
+ CompressFormat.valueOf(codec),
+ parquetCompression(
+ true,
+ null,
+ " " +
codec.toLowerCase(java.util.Locale.ROOT) + " ")
+ .getCompressFormat());
+ }
+ Assertions.assertEquals(
+ CompressFormat.NONE,
+ parquetCompression(true, null, " uncompressed
").getCompressFormat());
+ }
+
+ @Test
+ void testParquetCompressionPrecedenceAndDefault() throws Exception {
+ Assertions.assertEquals(
+ CompressFormat.NONE, parquetCompression(null, null,
"SNAPPY").getCompressFormat());
+ Assertions.assertEquals(
+ CompressFormat.NONE,
+ parquetCompression(false, null,
"invalid").getCompressFormat());
+ Assertions.assertEquals(
+ CompressFormat.NONE,
+ parquetCompression(true, "NONE",
"SNAPPY").getCompressFormat());
+ Assertions.assertEquals(
+ CompressFormat.GZIP,
+ parquetCompression(true, "GZIP",
"invalid").getCompressFormat());
+ for (String codec : new String[] {null, "", " "}) {
+ Assertions.assertEquals(
+ CompressFormat.NONE, parquetCompression(true, null,
codec).getCompressFormat());
+ }
+ Assertions.assertEquals(
+ CompressFormat.NONE,
+ compressionConfig(true, null, FileFormat.PARQUET,
null).getCompressFormat());
+ }
+
+ @Test
+ void testInvalidParquetCompressionRejected() {
+ for (String codec : new String[] {"invalid", "ZLIB"}) {
+ HiveConnectorException error =
+ Assertions.assertThrows(
+ HiveConnectorException.class,
+ () -> parquetCompression(true, null, codec));
+
Assertions.assertTrue(error.getMessage().contains("parquet.compression"));
+
Assertions.assertTrue(error.getMessage().contains("default.test_table"));
+ }
+ }
+
+ @Test
+ void testParquetPropertyDoesNotApplyToOrcOrText() throws Exception {
+ Map<String, String> parameters = new HashMap<>();
+ parameters.put("parquet.compression", "invalid");
+ for (FileFormat format : new FileFormat[] {FileFormat.ORC,
FileFormat.TEXT}) {
+ Assertions.assertEquals(
+ CompressFormat.NONE,
+ compressionConfig(true, null, format,
parameters).getCompressFormat());
+ }
+ }
+
+ private static FileSinkConfig parquetCompression(
+ Boolean enabled, String explicitCodec, String tableCodec) throws
Exception {
+ Map<String, String> parameters = new HashMap<>();
+ parameters.put("parquet.compression", tableCodec);
+ return compressionConfig(enabled, explicitCodec, FileFormat.PARQUET,
parameters);
+ }
+
+ /**
+ * Resolves the production sink configuration against mocked
existing-table metadata without
+ * contacting a metastore; returned settings drive codec and local-file
round-trip assertions.
+ */
+ private static FileSinkConfig compressionConfig(
+ Boolean enabled,
+ String explicitCodec,
+ FileFormat format,
+ Map<String, String> parameters)
+ throws Exception {
Map<String, Object> options = new HashMap<>();
options.put(HiveSinkOptions.TABLE_NAME.key(), "default.test_table");
if (enabled != null) {
@@ -161,9 +395,7 @@ public class HiveSinkConfigTest {
.setInputFormat(
"org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat");
}
- if (tableCodec != null) {
- table.putToParameters("orc.compress", tableCodec);
- }
+ table.setParameters(parameters);
try (MockedStatic<HiveTableUtils> mocked =
Mockito.mockStatic(HiveTableUtils.class)) {
mocked.when(() ->
HiveTableUtils.getTableInfo(Mockito.any())).thenReturn(table);
mocked.when(() ->
HiveTableUtils.parseFileFormat(Mockito.any(Table.class)))
diff --git
a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/java/org/apache/seatunnel/e2e/connector/hive/HiveIT.java
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/java/org/apache/seatunnel/e2e/connector/hive/HiveIT.java
index c12dee50a6..c56c6336db 100644
---
a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/java/org/apache/seatunnel/e2e/connector/hive/HiveIT.java
+++
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/java/org/apache/seatunnel/e2e/connector/hive/HiveIT.java
@@ -31,6 +31,9 @@ import org.apache.hadoop.fs.Path;
import org.apache.orc.CompressionKind;
import org.apache.orc.OrcFile;
import org.apache.orc.Reader;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.metadata.CompressionCodecName;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
@@ -366,6 +369,10 @@ public class HiveIT extends TestSuiteBase implements
TestResource {
verifyExistingOrcTableCompression(container, "override",
CompressionKind.NONE);
}
+ /**
+ * Waits for the sink job to finish before inspecting published file
codecs and exact Hive rows.
+ * Drops the test table and removes copied files in finally, including
after assertion failures.
+ */
private void verifyExistingOrcTableCompression(
TestContainer container, String scenario, CompressionKind
expectedCodec)
throws Exception {
@@ -430,6 +437,91 @@ public class HiveIT extends TestSuiteBase implements
TestResource {
}
}
+ @TestTemplate
+ public void testExistingParquetTableCompression(TestContainer container)
throws Exception {
+ verifyExistingParquetTableCompression(container, "inherited",
CompressionCodecName.SNAPPY);
+ verifyExistingParquetTableCompression(
+ container, "override", CompressionCodecName.UNCOMPRESSED);
+ }
+
+ /**
+ * Waits for the sink job to finish before inspecting published file
codecs and exact Hive rows.
+ * Drops the test table and removes copied files in finally, including
after assertion failures.
+ */
+ private void verifyExistingParquetTableCompression(
+ TestContainer container, String scenario, CompressionCodecName
expectedCodec)
+ throws Exception {
+ String table = "test_parquet_compression_" + scenario;
+ String location = "/opt/hive/data/warehouse/" + table;
+ try (Statement statement = hiveConnection.createStatement()) {
+ statement.execute("DROP TABLE IF EXISTS default." + table);
+ statement.execute(
+ "CREATE TABLE default."
+ + table
+ + " (pk_id BIGINT, name STRING, score INT) STORED
AS PARQUET"
+ + " LOCATION '"
+ + location
+ + "'"
+ + " TBLPROPERTIES
('parquet.compression'='SNAPPY')");
+ try {
+ Container.ExecResult result =
+ container.executeJob("/fake_to_hive_parquet_" +
scenario + ".conf");
+ Assertions.assertEquals(0, result.getExitCode(),
result.getStderr());
+ Container.ExecResult files =
+ hiveServerContainer.execInContainer(
+ "find", location, "-type", "f", "!", "-name",
".*", "!", "-name",
+ "_*");
+ Assertions.assertEquals(0, files.getExitCode(),
files.getStderr());
+ Assertions.assertFalse(
+ files.getStdout().trim().isEmpty(), "No published
Parquet files");
+ long rows = 0;
+ for (String file : files.getStdout().trim().split("\\R")) {
+ java.nio.file.Path copy =
+ Files.createTempFile("hive-parquet-compression-",
".parquet");
+ try {
+ hiveServerContainer.copyFileFromContainer(file,
copy.toString());
+ try (ParquetFileReader reader =
+ ParquetFileReader.open(
+ HadoopInputFile.fromPath(
+ new Path(copy.toUri()), new
Configuration()))) {
+ Assertions.assertFalse(
+ reader.getFooter().getBlocks().isEmpty(),
+ "No Parquet row groups");
+ for
(org.apache.parquet.hadoop.metadata.BlockMetaData rowGroup :
+ reader.getFooter().getBlocks()) {
+
Assertions.assertFalse(rowGroup.getColumns().isEmpty());
+ for
(org.apache.parquet.hadoop.metadata.ColumnChunkMetaData column :
+ rowGroup.getColumns()) {
+ Assertions.assertEquals(expectedCodec,
column.getCodec(), file);
+ }
+ rows += rowGroup.getRowCount();
+ }
+ }
+ } finally {
+ Files.deleteIfExists(copy);
+ }
+ }
+ Assertions.assertEquals(3, rows);
+ try (ResultSet resultSet =
+ statement.executeQuery(
+ "SELECT pk_id, name, score FROM default."
+ + table
+ + " ORDER BY pk_id")) {
+ for (int id = 1; id <= 3; id++) {
+ Assertions.assertTrue(resultSet.next());
+ Assertions.assertEquals(id, resultSet.getLong(1));
+ Assertions.assertEquals(
+ String.valueOf((char) ('A' + id - 1)),
resultSet.getString(2));
+ Assertions.assertEquals(100, resultSet.getInt(3));
+ }
+ Assertions.assertFalse(resultSet.next());
+ }
+ } finally {
+ statement.execute("DROP TABLE IF EXISTS default." + table);
+ }
+ }
+ }
+
@TestTemplate
public void testAutoTableCreationDefaultTemplate(TestContainer container)
throws Exception {
executeJob(
diff --git
a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/resources/fake_to_hive_parquet_inherited.conf
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/resources/fake_to_hive_parquet_inherited.conf
new file mode 100644
index 0000000000..a2d7523294
--- /dev/null
+++
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/resources/fake_to_hive_parquet_inherited.conf
@@ -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.
+#
+
+env {
+ parallelism = 1
+ job.mode = "BATCH"
+}
+
+source {
+ FakeSource {
+ schema = {
+ fields {
+ pk_id = bigint
+ name = string
+ score = int
+ }
+ primaryKey {
+ name = "pk_id"
+ columnNames = [pk_id]
+ }
+ }
+ rows = [
+ {
+ kind = INSERT
+ fields = [1, "A", 100]
+ },
+ {
+ kind = INSERT
+ fields = [2, "B", 100]
+ },
+ {
+ kind = INSERT
+ fields = [3, "C", 100]
+ }
+ ]
+ }
+}
+
+sink {
+ Hive {
+ table_name = "default.test_parquet_compression_inherited"
+ metastore_uri = "thrift://metastore:9083"
+ use_table_compression = true
+ }
+}
diff --git
a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/resources/fake_to_hive_parquet_override.conf
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/resources/fake_to_hive_parquet_override.conf
new file mode 100644
index 0000000000..c4a52cc288
--- /dev/null
+++
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hive-e2e/src/test/resources/fake_to_hive_parquet_override.conf
@@ -0,0 +1,60 @@
+#
+# 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.
+#
+
+env {
+ parallelism = 1
+ job.mode = "BATCH"
+}
+
+source {
+ FakeSource {
+ schema = {
+ fields {
+ pk_id = bigint
+ name = string
+ score = int
+ }
+ primaryKey {
+ name = "pk_id"
+ columnNames = [pk_id]
+ }
+ }
+ rows = [
+ {
+ kind = INSERT
+ fields = [1, "A", 100]
+ },
+ {
+ kind = INSERT
+ fields = [2, "B", 100]
+ },
+ {
+ kind = INSERT
+ fields = [3, "C", 100]
+ }
+ ]
+ }
+}
+
+sink {
+ Hive {
+ table_name = "default.test_parquet_compression_override"
+ metastore_uri = "thrift://metastore:9083"
+ use_table_compression = true
+ compress_codec = "NONE"
+ }
+}