Updated on 2026-11-06 GMT+08:00

USE Statement

Overview

The USE statement is used to switch the default catalog of the current session. After the switching, you can directly use the table name in subsequent SQL statements to reference the table, without the need to use the catalog name prefix.

Application Scenarios

  • Operations on multiple catalogs: Switches between the Hive catalog and the default catalog to access different metadata sources.
  • Multi-tenant scenarios: Different databases isolate data of different services. The USE statement can be used to quickly switch the working context.

Notes and Constraints

  • The target catalog of the USE CATALOG statement must have been registered using the CREATE CATALOG statement.
  • The USE statement affects only the current session and does not affect other sessions or global configurations.

Syntax Structure

-- Switch the current catalog.

USE CATALOG catalog_name;

Usage Example

Example: Switching the catalog using USE CATALOG

CREATE TABLE test_kafka (
  user_id varchar,
  item_id varchar,
  cat_id varchar,
  test_time timestamp
) WITH (
  'properties.bootstrap.servers' = '<IP-address-of-Kafka-broker-instance>:<Kafka-port>',
  'format' = 'json',
  'topic' = 'zw_test_kafka',
  'connector' = 'kafka',
  'scan.startup.mode' = 'latest-offset'
);
CREATE CATALOG myhive WITH (
  'type' = 'hive',
  'hive-version' = '3.1.0',
  'default-database' = 'default',
  'hive-conf-dir' = 'xxxxxxx'
);
use catalog myhive;
set table.sql-dialect=hive;
CREATE TABLE IF NOT EXISTS user_behavior_hive_tbl (
    user_id STRING,
    item_id STRING,
    cat_id STRING,
    ts timestamp
  ) PARTITIONED BY (dy STRING, ho STRING, mi STRING) stored as textfile TBLPROPERTIES (
    'partition.time-extractor.timestamp-pattern' = '$dy $ho:$mi:00',
    'sink.partition-commit.trigger' = 'process-time',
    'sink.partition-commit.delay' = '0S',
    'sink.partition-commit.policy.kind' = 'metastore,success-file'
  );
set table.sql-dialect=default;
use catalog default_catalog;
INSERT into
  myhive.`default`.user_behavior_hive_tbl
SELECT
  user_id,
  item_id,
  cat_id,
  test_time,
  DATE_FORMAT(test_time, 'yyyy-MM-dd'),
  DATE_FORMAT(test_time, 'HH'),
  DATE_FORMAT(test_time, 'mm')
FROM
  test_kafka;