Project: Flink (App Definition and Development / Streaming & Messaging)
File: use.md

title: "USE Statements" weight: 10 type: docs aliases: /dev/table/sql/use.html <!-- 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. --> USE statements are used to set the current database or catalog, or change the resolution order and enabled status of module. {{< tabs "use" >}} {{< tab "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. The following examples show how to run a USE statement in `TableEnvironment`. {{< /tab >}} {{< tab "Scala" >}} 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. The following examples show how to run a USE statement in `TableEnvironment`. {{< /tab >}} {{< tab "Python" >}} 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. The following examples show how to run a USE statement in `TableEnvironment`. {{< /tab >}} {{< tab "SQL CLI" >}} USE statements can be executed in . The following examples show how to run a USE statement in SQL CLI. {{< /tab >}} {{< /tabs >}} {{< tabs "5d0314ba-9ac3-418e-9f40-1379fd516c85" >}} {{< tab "Java" >}} ```java 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

// +-+-+ // | module name | used | // +-+-+ // | hive | true | // | core | false | // +-+-+ ``` {{< /tab >}} {{< tab "Scala" >}} ```scala val env = StreamExecutionEnvironment.getExecutionEnvironment() val 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 | // +-+-+ ``` {{< /tab >}} {{< tab "Python" >}} ```python table_env = StreamTableEnvironment.create(...) tableenv.executesql("CREATE CATALOG cat1 WITH (...)") tableenv.executesql("SHOW CATALOGS").print() tableenv.executesql("USE CATALOG cat1") tableenv.executesql("SHOW DATABASES").print() tableenv.executesql("CREATE DATABASE db1 WITH (...)") tableenv.executesql("SHOW DATABASES").print() tableenv.executesql("USE db1") tableenv.executesql("USE MODULES hive") tableenv.executesql("SHOW FULL MODULES").print() ``` {{< /tab >}} {{< tab "SQL CLI" >}} ```sql 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 ``` {{< /tab >}} {{< /tabs >}} {{< top >}} ```sql 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`. ```sql USE MODULES modulename1[, modulename2, ...] ``` 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`. ```sql USE [catalogname.]databasename ``` 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`.