USE Statements

USE statements are used to set the current database or catalog, or change the resolution order and enabled status of module.

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.

The following examples show how to run a USE statement in TableEnvironment.

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.

Python

USE statements can be executed with the execute_sql() method of the TableEnvironment. The execute_sql() 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.

SQL CLI

USE statements can be executed in SQL CLI.

The following examples show how to run a USE statement in SQL CLI.

Java

  1. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  2. StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
  3. // create a catalog
  4. tEnv.executeSql("CREATE CATALOG cat1 WITH (...)");
  5. tEnv.executeSql("SHOW CATALOGS").print();
  6. // +-----------------+
  7. // | catalog name |
  8. // +-----------------+
  9. // | default_catalog |
  10. // | cat1 |
  11. // +-----------------+
  12. // change default catalog
  13. tEnv.executeSql("USE CATALOG cat1");
  14. tEnv.executeSql("SHOW DATABASES").print();
  15. // databases are empty
  16. // +---------------+
  17. // | database name |
  18. // +---------------+
  19. // +---------------+
  20. // create a database
  21. tEnv.executeSql("CREATE DATABASE db1 WITH (...)");
  22. tEnv.executeSql("SHOW DATABASES").print();
  23. // +---------------+
  24. // | database name |
  25. // +---------------+
  26. // | db1 |
  27. // +---------------+
  28. // change default database
  29. tEnv.executeSql("USE db1");
  30. // change module resolution order and enabled status
  31. tEnv.executeSql("USE MODULES hive");
  32. tEnv.executeSql("SHOW FULL MODULES").print();
  33. // +-------------+-------+
  34. // | module name | used |
  35. // +-------------+-------+
  36. // | hive | true |
  37. // | core | false |
  38. // +-------------+-------+

Scala

  1. val env = StreamExecutionEnvironment.getExecutionEnvironment()
  2. val tEnv = StreamTableEnvironment.create(env)
  3. // create a catalog
  4. tEnv.executeSql("CREATE CATALOG cat1 WITH (...)")
  5. tEnv.executeSql("SHOW CATALOGS").print()
  6. // +-----------------+
  7. // | catalog name |
  8. // +-----------------+
  9. // | default_catalog |
  10. // | cat1 |
  11. // +-----------------+
  12. // change default catalog
  13. tEnv.executeSql("USE CATALOG cat1")
  14. tEnv.executeSql("SHOW DATABASES").print()
  15. // databases are empty
  16. // +---------------+
  17. // | database name |
  18. // +---------------+
  19. // +---------------+
  20. // create a database
  21. tEnv.executeSql("CREATE DATABASE db1 WITH (...)")
  22. tEnv.executeSql("SHOW DATABASES").print()
  23. // +---------------+
  24. // | database name |
  25. // +---------------+
  26. // | db1 |
  27. // +---------------+
  28. // change default database
  29. tEnv.executeSql("USE db1")
  30. // change module resolution order and enabled status
  31. tEnv.executeSql("USE MODULES hive")
  32. tEnv.executeSql("SHOW FULL MODULES").print()
  33. // +-------------+-------+
  34. // | module name | used |
  35. // +-------------+-------+
  36. // | hive | true |
  37. // | core | false |
  38. // +-------------+-------+

Python

  1. table_env = StreamTableEnvironment.create(...)
  2. # create a catalog
  3. table_env.execute_sql("CREATE CATALOG cat1 WITH (...)")
  4. table_env.execute_sql("SHOW CATALOGS").print()
  5. # +-----------------+
  6. # | catalog name |
  7. # +-----------------+
  8. # | default_catalog |
  9. # | cat1 |
  10. # +-----------------+
  11. # change default catalog
  12. table_env.execute_sql("USE CATALOG cat1")
  13. table_env.execute_sql("SHOW DATABASES").print()
  14. # databases are empty
  15. # +---------------+
  16. # | database name |
  17. # +---------------+
  18. # +---------------+
  19. # create a database
  20. table_env.execute_sql("CREATE DATABASE db1 WITH (...)")
  21. table_env.execute_sql("SHOW DATABASES").print()
  22. # +---------------+
  23. # | database name |
  24. # +---------------+
  25. # | db1 |
  26. # +---------------+
  27. # change default database
  28. table_env.execute_sql("USE db1")
  29. # change module resolution order and enabled status
  30. table_env.execute_sql("USE MODULES hive")
  31. table_env.execute_sql("SHOW FULL MODULES").print()
  32. # +-------------+-------+
  33. # | module name | used |
  34. # +-------------+-------+
  35. # | hive | true |
  36. # | core | false |
  37. # +-------------+-------+

SQL CLI

  1. Flink SQL> CREATE CATALOG cat1 WITH (...);
  2. [INFO] Catalog has been created.
  3. Flink SQL> SHOW CATALOGS;
  4. default_catalog
  5. cat1
  6. Flink SQL> USE CATALOG cat1;
  7. Flink SQL> SHOW DATABASES;
  8. Flink SQL> CREATE DATABASE db1 WITH (...);
  9. [INFO] Database has been created.
  10. Flink SQL> SHOW DATABASES;
  11. db1
  12. Flink SQL> USE db1;
  13. Flink SQL> USE MODULES hive;
  14. [INFO] Use modules succeeded!
  15. Flink SQL> SHOW FULL MODULES;
  16. +-------------+-------+
  17. | module name | used |
  18. +-------------+-------+
  19. | hive | true |
  20. | core | false |
  21. +-------------+-------+
  22. 2 rows in set

USE CATALOG

  1. 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.

USE MODULES

  1. 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

  1. 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.