USE Statements

USE statements are used to set the current database or catalog.

Run a USE statement

USE statements can be executed with the executeSql() method of the TableEnvironment, or executed in SQL CLI. 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 and in SQL CLI.

  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");
  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")
  1. settings = EnvironmentSettings.new_instance()...
  2. table_env = StreamTableEnvironment.create(env, settings)
  3. # create a catalog
  4. table_env.execute_sql("CREATE CATALOG cat1 WITH (...)")
  5. table_env.execute_sql("SHOW CATALOGS").print()
  6. # +-----------------+
  7. # | catalog name |
  8. # +-----------------+
  9. # | default_catalog |
  10. # | cat1 |
  11. # +-----------------+
  12. # change default catalog
  13. table_env.execute_sql("USE CATALOG cat1")
  14. table_env.execute_sql("SHOW DATABASES").print()
  15. # databases are empty
  16. # +---------------+
  17. # | database name |
  18. # +---------------+
  19. # +---------------+
  20. # create a database
  21. table_env.execute_sql("CREATE DATABASE db1 WITH (...)")
  22. table_env.execute_sql("SHOW DATABASES").print()
  23. # +---------------+
  24. # | database name |
  25. # +---------------+
  26. # | db1 |
  27. # +---------------+
  28. # change default database
  29. table_env.execute_sql("USE db1")
  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;

Back to top

USE CATLOAG

  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

  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.