The Hive connector provides read and write access to Hive data sources. Flink can use a Hive Catalog to perform unified batch and stream processing on Hive tables. This means Flink can serve as a high-performance alternative to the Hive batch engine or stream data into Hive tables for real-time Data Warehouse applications.
insert overwrite.In Flink SQL syntax, data tables are represented by a three-part identifier: Catalog.Database.Table. For a Hive data source, the catalog is a Hive Catalog.
When streaming to Hive tables, you need to set table parameters. See Parameters for stream write at table level.
Hive Catalog requires the following dependency files:
Note: For E-MapReduce clusters, you can obtain these two files from the $HIVE_CONF_DIR directory on the master node.
The upload result is as follows:
To resolve the EMR-Hive domain name, set the following two custom parameters:
containerized.master.env.ENV_SEARCH_DOMAIN:emr-volces.com containerized.taskmanager.env.ENV_SEARCH_DOMAIN:emr-volces.com
The configuration result is as follows:
Parameter | Required | Default Value | Type | Description |
|---|---|---|---|---|
type | Yes | (None) | String | The type of the catalog. This parameter must be set to |
name | Yes | (None) | String | The name of the Catalog. Required only when using a YAML file. |
hive-conf-dir | No | (None) | String | A URI pointing to the directory that contains hive-site.xml. The URI must be of a type supported by the Hadoop file system. A relative URI without a scheme defaults to the local file system. If this parameter is not specified, the system searches for hive-site.xml in the class path. tip Enter |
default-database | No | default | String | The default database to use when a catalog is set as the current one. |
hive-version | No | None | String | HiveCatalog automatically detects the Hive version in use. We recommend not setting the Hive version manually unless automatic detection fails. |
hadoop-conf-dir | No | (None) | String | The path to the Hadoop configuration directory. Only local file system paths are currently supported. We recommend using the HADOOP_CONF_DIR environment variable to specify the Hadoop configuration. Therefore, use this parameter only when the environment variable is insufficient, such as when you need to set separate Hadoop configurations for each HiveCatalog. |
Parameter | Required | Default value | Data type | Description |
|---|---|---|---|---|
table.exec.hive.fallback-mapred-reader | No | true | Boolean | Specifies whether to enable vectorized reading.
This feature is enabled by default. To disable it, set it to false. |
table.exec.hive.infer-source-parallelism | No | true | Boolean | Sets whether to enable source parallelism inference. By default, Flink infers the optimal parallelism for reading Hive based on the number of files and the number of blocks in each file. tip This parameter affects all Hive sources in the current job. |
table.exec.hive.infer-source-parallelism.max | No | 1000 | Integer | Sets the maximum inferred parallelism for the source operator. tip This parameter affects all Hive sources in the current job. |
table.exec.hive.split-max-size | No | 128mb | MemorySize | Sets the data split size when reading Hive tables. tip Applies only to Hive tables in ORC format. |
table.exec.hive.file-open-cost | No | 4mb | MemorySize | The estimated cost in bytes of opening a file when reading from a Hive table. The default is 4 MB. tip Applies only to Hive tables in ORC format. |
table.exec.hive.calculate-partition-size.thread-num | No | 3 | Integer | The number of threads used to calculate the size of all files in all partitions. |
table.exec.hive.load-partition-splits.thread-num | No | 3 | Integer | The number of threads for generating splits. |
table.exec.hive.sink.statistic-auto-gather.enable | No | true | Boolean | Specifies whether to enable automatic statistics collection. tip Automatic statistics collection is only supported in batch write mode. It is not currently supported in stream write mode. |
table.exec.hive.sink.statistic-auto-gather.thread-num | No | 3 | Integer | Sets the number of threads for gathering statistics. tip Automatic statistics collection is only supported in batch write mode. It is not currently supported in stream write mode. |
Stream writing parameters are table-level and must be added to the table parameters when creating a Hive table. These are primarily Filesystem table parameters. See Filesystem.
Additionally, some parameters are specific to stream writing to Hive:
Parameter | Required | Default value | Data type | Description |
|---|---|---|---|---|
sink.partition-commit.policy.kind | Yes | metastore,success-file | String | The partition commit policy, which notifies downstream systems that a partition is finished writing and is ready to be read. We recommend setting this to metastore,success-file.
|
The sample SQL uses default_hive as the Hive Catalog example.
CREATE CATALOG default_hive WITH ( 'type' = 'hive', -- Path to the hive-site.xml file 'hive-conf-dir' = '/opt/tiger/workdir' );
SELECT * FROM default_hive.db.table WHERE `date`='${date}'
-- Insert new data into a non-partitioned table INSERT INTO default_hive.db.mytable SELECT 'Tom', 25; -- Overwrite a non-partitioned table INSERT OVERWRITE default_hive.db.mytable SELECT 'Tom', 25; -- Insert new data into a partitioned table INSERT INTO default_hive.db.myparttable PARTITION (`date`='${date}') SELECT 'Tom', 25; -- Overwrite a partitioned table INSERT OVERWRITE default_hive.db.myparttable PARTITION (`date`='${date}') SELECT 'Tom', 25;
Create a Hive table and add parameters for streaming writes to Hive in the table properties:
CREATE TABLE db.hive_table ( user_id STRING, order_amount DOUBLE ) PARTITIONED BY (dt STRING, hr STRING) STORED AS parquet TBLPROPERTIES ( 'partition.time-extractor.timestamp-pattern'='$dt $hr:00:00', 'sink.partition-commit.trigger'='partition-time', 'sink.partition-commit.delay'='1 h', 'sink.partition-commit.policy.kind'='metastore,success-file' );
Flink SQL for streaming writes to Hive:
CREATE TABLE kafka_table ( user_id STRING, order_amount DOUBLE, log_ts TIMESTAMP(3), WATERMARK FOR log_ts AS log_ts - INTERVAL '5' SECOND -- Declare a watermark on the TIMESTAMP column. ) WITH (...); INSERT INTO TABLE default_hive.db.hive_table SELECT user_id, order_amount, DATE_FORMAT(log_ts, 'yyyy-MM-dd'), DATE_FORMAT(log_ts, 'HH') FROM kafka_table;