Flink SQL: USE Statements

本文介绍了如何使用Flink SQL中的USE语句来设置当前目录、数据库及模块,并展示了如何通过TableEnvironment和SQLCLI执行这些操作。

USE Statements

USE statements are used to set the current database or catalog, or change the resolution order and enabled status of module.
USE语句用于设置当前数据库或目录,或更改模块的解析顺序和启用状态。

Run a USE statement

Java
USE statements can be executed with the executeSql() method of the TableEnvironment. The executeSql() method returns ‘OK’ for a successful USE operation, otherwise will throw an exception.
可以使用TableEnvironment的executeSql()方法执行USE语句。executeSql()方法为成功的USE操作返回“OK”,否则将引发异常。

The following examples show how to run a USE statement in TableEnvironment.
以下示例显示了如何在TableEnvironment中运行USE语句。

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);

// create a catalog
tEnv.executeSql("CREATE CATALOG cat1 WITH (...)");
tEnv.executeSql("SHOW CATALOGS").print();
// +-----------------+
// |    catalog name |
// +-----------------+
// | default_catalog |
// | cat1            |
// +-----------------+

// change default catalog
tEnv.executeSql("USE CATALOG cat1");

tEnv.executeSql("SHOW DATABASES").print();
// databases are empty
// +---------------+
// | database name |
// +---------------+
// +---------------+

// create a database
tEnv.executeSql("CREATE DATABASE db1 WITH (...)");
tEnv.executeSql("SHOW DATABASES").print();
// +---------------+
// | database name |
// +---------------+
// |        db1    |
// +---------------+

// change default database
tEnv.executeSql("USE db1");

// change module resolution order and enabled status
tEnv.executeSql("USE MODULES hive");
tEnv.executeSql("SHOW FULL MODULES").print();
// +-------------+-------+
// | module name |  used |
// +-------------+-------+
// |        hive |  true |
// |        core | false |
// +-------------+-------+

SQL CLI
USE statements can be executed in SQL CLI.
可以在SQL CLI中执行USE语句。

The following examples show how to run a USE statement in SQL CLI.
以下示例显示了如何在SQL CLI中运行USE语句。

Flink SQL> CREATE CATALOG cat1 WITH (...);
[INFO] Catalog has been created.

Flink SQL> SHOW CATALOGS;
default_catalog
cat1

Flink SQL> USE CATALOG cat1;

Flink SQL> SHOW DATABASES;

Flink SQL> CREATE DATABASE db1 WITH (...);
[INFO] Database has been created.

Flink SQL> SHOW DATABASES;
db1

Flink SQL> USE db1;

Flink SQL> USE MODULES hive;
[INFO] Use modules succeeded!
Flink SQL> SHOW FULL MODULES;
+-------------+-------+
| module name |  used |
+-------------+-------+
|        hive |  true |
|        core | false |
+-------------+-------+
2 rows in set

USE CATALOG

USE CATALOG catalog_name

Set the current catalog. All subsequent commands that do not explicitly specify a catalog will use this one. If the provided catalog does not exist, an exception is thrown. The default current catalog is default_catalog.
设置当前目录。所有未明确指定目录的后续命令都将使用此目录。如果提供的目录不存在,将引发异常。默认当前目录为default_catalog。

USE MODULES

USE MODULES module_name1[, module_name2, ...]

Set the enabled modules with declared order. All subsequent commands will resolve metadata(functions/user-defined types/rules, etc.) within enabled modules and follow resolution order. A module is used by default when it is loaded. Loaded modules will become disabled if not used by USE MODULES statement. The default loaded and enabled module is core.
使用声明的顺序设置启用的模块。所有后续命令将在启用的模块中解析元数据(函数/用户定义的类型/规则等),并遵循解析顺序。模块在加载时默认使用。如果USE modules语句未使用,则加载的模块将被禁用。默认加载并启用的模块是core。

USE

USE [catalog_name.]database_name

Set the current database. All subsequent commands that do not explicitly specify a database will use this one. If the provided database does not exist, an exception is thrown. The default current database is default_database.
设置当前数据库。所有未显式指定数据库的后续命令都将使用此命令。如果提供的数据库不存在,将引发异常。默认当前数据库为default_database。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值