Hive Catalog

Updated at:

A Hive catalog is an external catalog that lets you access Hive data directly from StarRocks. Supported since v2.3.

Use cases

Scenario Description
Query acceleration Query Hive data on HDFS or Object Storage Service (OSS) directly through StarRocks without creating tables manually.
Data import Transform Hive data and load it into StarRocks internal tables using INSERT INTO or asynchronous materialized views (v3.1 and later).
Data write-back Create Hive databases and tables in StarRocks, and write query results back to Hive in Parquet (v3.2 and later), ORC, or TextFile (v3.3 and later) format.

Supported environments

The StarRocks cluster must be able to reach both the Hive storage system and the metadata service.

Storage systems: HDFS, Alibaba Cloud OSS

Metadata services: Hive Metastore (HMS) and Data Lake Formation DLF 1.0 (Legacy).

Create a Hive catalog

Syntax

CREATE EXTERNAL CATALOG <catalog_name>
[COMMENT <comment>]
PROPERTIES
(
    "type" = "hive",                                    -- required
    GeneralParams,
    MetastoreParams
)

Parameters

General parameters

Parameter Required Description
catalog_name Yes Catalog name. Must start with a letter and can contain letters (a-z, A-Z), numbers (0-9), and underscores (_). Maximum 64 characters.
comment No Catalog description.
type Yes Data source type. Set to hive.
enable_recursive_listing No Specifies whether to recursively read files in subdirectories of a table or partition directory. Default: true. Set to false to read only the current directory level.

Metastore parameters

Choose the parameter set that matches your metadata service.

Use HMS

Parameter Description
hive.metastore.type Metastore type. Leave blank or omit this parameter to use HMS. Default: empty.
hive.metastore.uris HMS URI in the format thrift://<IP_address>:<port>. Default port: 9083.

Use DLF 1.0 (Legacy)

Parameter Description
hive.metastore.type Metastore type. Set to dlf.
dlf.catalog.id ID of an existing data catalog in DLF 1.0. If not specified, the default DLF Catalog is used.

Examples

The following examples create a Hive catalog named hive_catalog with HDFS as the storage system.

Use HMS

CREATE EXTERNAL CATALOG hive_catalog
PROPERTIES
(
    "type" = "hive",
    "hive.metastore.uris" = "thrift://xx.xx.xx.xx:9083"
);

Use DLF 1.0 (Legacy)

CREATE EXTERNAL CATALOG hive_catalog
PROPERTIES
(
    "type" = "hive",
    "hive.metastore.type" = "dlf",
    "dlf.catalog.id" = "sr_dlf"
);

View and switch catalogs

Query all catalogs in the current StarRocks cluster:

SHOW CATALOGS;

View the creation statement of a catalog:

SHOW CREATE CATALOG hive_catalog;

Switch to a Hive catalog, then select a database:

-- Switch to the target catalog.
SET CATALOG <catalog_name>;

-- Select a database.
USE <db_name>;

Or switch directly to a specific database:

USE <catalog_name>.<db_name>;

Drop a catalog

DROP CATALOG hive_catalog;

Create a Hive database

CREATE DATABASE and DROP DATABASE for Hive catalogs require v3.2 or later. Use GRANT and REVOKE to manage permissions.

Switch to the target Hive catalog, then run:

CREATE DATABASE <database_name>
[PROPERTIES ("location" = "<prefix>://<path_to_database>/<database_name.db>")]

If location is omitted, the database is created at <warehouse_location>/<database_name.db>.

Location prefixes:

Storage system Prefix
HDFS hdfs
OSS or OSS-HDFS oss

Drop a Hive database

Switch to the target Hive catalog, then run:

DROP DATABASE <database_name>;

Only empty databases can be dropped. The corresponding file path on HDFS or OSS is preserved.

Create a Hive table

CREATE TABLE for Hive catalogs requires v3.2 or later (Parquet only). ORC and TextFile support was added in v3.3. Use GRANT and REVOKE to manage permissions.

Switch to the target Hive catalog and database, then create a Managed Table using CREATE TABLE, CREATE TABLE AS SELECT (CTAS), or CREATE TABLE LIKE.

Syntax

CREATE TABLE [IF NOT EXISTS] [database.]table_name
(column_definition1[, column_definition2, ...
partition_column_definition1, partition_column_definition2...])
[partition_desc]
[PROPERTIES ("key" = "value", ...)]
[AS SELECT query]
[LIKE [database.]<source_table_name>]

Column definition

col_name col_type [COMMENT 'comment']

Supported data types: TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL, DATE, DATETIME, CHAR, VARCHAR[(length)], ARRAY, MAP, STRUCT

Unsupported data types: LARGEINT, HLL, BITMAP

All non-partition key columns default to NULL. Partition key columns must be declared last and cannot be NULL.

Partition definition

PARTITION BY (par_col1[, par_col2...])

Only identity transforms are supported, meaning one partition is created for each unique partition value.

Partition key columns support all data types except FLOAT, DOUBLE, DECIMAL, and DATETIME. The order of partition keys in PARTITION BY must match the order in the column definition.

Table properties

Property Description
location File path for the Managed Table. Not required when using HMS.
file_format File format. Valid values: parquet (default), orc, textfile. ORC and TextFile require v3.3.
compression_codec Compression format. Deprecated from v3.2.3. Use the connector_sink_compression_codec session variable instead.

Examples

Create a non-partitioned table:

CREATE TABLE unpartition_tbl
(
    id int,
    score double
);

Create a partitioned table with id and dt as partition keys:

CREATE TABLE partition_tbl_1
(
    action varchar(20),
    id int NOT NULL,
    dt date NOT NULL
)
PARTITION BY (id, dt);

Create a table from a query result:

CREATE TABLE partition_tbl_2
PARTITION BY (id, dt)
AS SELECT * FROM partition_tbl_1;

View table schema

View the table schema:

DESC[RIBE] <catalog_name>.<database_name>.<table_name>;

View the table schema and file location:

SHOW CREATE TABLE <catalog_name>.<database_name>.<table_name>;

Drop a Hive table

DROP TABLE for Hive catalogs requires v3.2 or later. Only Managed Tables can be dropped. Use GRANT and REVOKE to manage permissions.

The FORCE keyword is required. This operation deletes table data on HDFS or OSS but preserves the file path. Use with caution.

Switch to the target Hive catalog and database, then run:

DROP TABLE <table_name> FORCE;

Query Hive data

Direct query

SELECT * FROM <catalog_name>.<database_name>.<table_name>;

Step-by-step query

  1. List databases in the Hive catalog:

       SHOW DATABASES FROM <catalog_name>;
  2. Switch to the target catalog and database. See View and switch catalogs.

  3. Query the target table:

       SELECT count(*) FROM <table_name> LIMIT 10;

Import Hive data into StarRocks

Transform and load Hive data into a StarRocks internal table (for example, olap_tbl):

INSERT INTO default_catalog.olap_db.olap_tbl SELECT * FROM hive_table;

Insert data into Hive tables

INSERT into Hive tables requires v3.2 or later (Parquet only). ORC and TextFile support was added in v3.3.
Writing to External Tables is disabled by default. Enable it with the ENABLE_WRITE_HIVE_EXTERNAL_TABLE system variable.
Use connector_sink_compression_codec to set the compression algorithm.
Use GRANT and REVOKE to manage permissions.

Managed Tables and External Tables are both supported. Switch to the target Hive catalog and database before running these statements.

Syntax

INSERT {INTO | OVERWRITE} <table_name>
[ (column_name [, ...]) ]
{ VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }

-- Insert into a specific partition:
INSERT {INTO | OVERWRITE} <table_name>
PARTITION (par_col1=<value> [, par_col2=<value>...])
{ VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
Partition key columns cannot be NULL. Make sure all partition keys have values during data import.

Parameters

Parameter Description
INTO Appends data to the target table.
OVERWRITE Overwrites existing data in the target table.
column_name Target columns for the insert. Must include partition key columns. Names can differ from the source but must match in order. If omitted, data maps to all columns. Missing non-partition columns default to NULL. Mismatched types are implicitly converted; a parsing error is returned if conversion fails.
expression Expression that assigns a value to the corresponding column.
DEFAULT Assigns the default value to the corresponding column.
query A SELECT query whose result is inserted into the target table. Any StarRocks-supported SQL syntax is valid.
PARTITION Target partition for the insert. All partition key columns must be specified, but their order can differ from the table definition. Cannot be combined with an explicit column_name list.

Examples

Insert rows into partition_tbl_1:

INSERT INTO partition_tbl_1 VALUES
  ("buy", 1, "2023-09-01"),
  ("sell", 2, "2023-09-02"),
  ("buy", 3, "2023-09-03");

Insert a computed result with explicit column order:

INSERT INTO partition_tbl_1 (id, action, dt) SELECT 1+1, 'buy', '2023-09-03';

Insert from a self-referencing query:

INSERT INTO partition_tbl_1
SELECT 'buy', 1, date_add(dt, INTERVAL 2 DAY)
FROM partition_tbl_1
WHERE id = 1;

Insert into a specific partition of partition_tbl_2:

-- Method 1: Values match the partition implicitly.
INSERT INTO partition_tbl_2 SELECT 'order', 1, '2023-09-01';

-- Method 2: Specify the partition explicitly.
INSERT INTO partition_tbl_2 PARTITION(dt='2023-09-01', id=1) SELECT 'order';

Overwrite a partition in partition_tbl_1:

-- Method 1
INSERT OVERWRITE partition_tbl_1 SELECT 'close', 1, '2023-09-01';

-- Method 2
INSERT OVERWRITE partition_tbl_1 PARTITION(dt='2023-09-01', id=1) SELECT 'close';

Grant permissions on Hive tables and views

Grant query permissions on all tables and views in a Hive catalog to a role:

GRANT SELECT ON ALL TABLES IN ALL DATABASES TO ROLE <role_name>

Example: Create a role and grant it query access to all tables in hive_catalog:

-- Create the role.
CREATE ROLE hive_role_table;

-- Switch to the Hive catalog.
SET CATALOG hive_catalog;

-- Grant SELECT on all tables and views.
GRANT SELECT ON ALL TABLES IN ALL DATABASES TO ROLE hive_role_table;

Metadata cache management

StarRocks caches Hive metadata and refreshes it asynchronously to improve query performance. Since v2.5.5, periodic cache refreshing is supported. When enabled, the cache is automatically refreshed every 10 minutes by default, so manual updates are rarely needed.

Manual refresh

To force an immediate refresh after metadata changes, run:

REFRESH EXTERNAL TABLE <table_name> [PARTITION ('partition_name', ...)]

Use manual refresh when:

  • Data files in an existing partition have changed (for example, after running INSERT OVERWRITE ... PARTITION ...).

  • The Hive table schema has changed.

  • A Hive table was dropped and recreated with the same name.

  • New partitions were added in Hive while enable_cache_list_names is set to true in the catalog properties.

REFRESH EXTERNAL TABLE only updates tables and partitions already cached in the FE. A manual refresh is needed only when you query new partition data right after it is added.

Limitations

Read support

StarRocks supports querying Hive data in the following file formats:

File format Supported compression
Parquet SNAPPY, LZ4, ZSTD, GZIP, NO_COMPRESSION. LZO added in v3.1.5.
ORC ZLIB, SNAPPY, LZO, LZ4, ZSTD, NO_COMPRESSION
TextFile LZO (v3.1.5 and later)
Avro --
RCFile --
SequenceFile --

Unsupported data types for queries: INTERVAL, BINARY, UNION. For TextFile tables, MAP and STRUCT are also unsupported.

Write support

StarRocks supports writing data to Hive in the following file formats:

File format Version required Supported compression
Parquet v3.2 and later NO_COMPRESSION, SNAPPY, LZ4, ZSTD, GZIP
ORC v3.3 and later NO_COMPRESSION, SNAPPY, LZ4, ZSTD, GZIP
TextFile v3.3 and later NO_COMPRESSION

Set the compression algorithm using the connector_sink_compression_codec session variable.