USE Statements

    Java

    USE statements can be executed with the 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.

    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.

    Java

    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. // | database name |
    17. // +---------------+
    18. // create a database
    19. tEnv.executeSql("CREATE DATABASE db1 WITH (...)")
    20. tEnv.executeSql("SHOW DATABASES").print()
    21. // +---------------+
    22. // | database name |
    23. // +---------------+
    24. // | db1 |
    25. // +---------------+
    26. // change default database
    27. tEnv.executeSql("USE db1")
    28. // change module resolution order and enabled status
    29. tEnv.executeSql("USE MODULES hive")
    30. tEnv.executeSql("SHOW FULL MODULES").print()
    31. // +-------------+-------+
    32. // | module name | used |
    33. // +-------------+-------+
    34. // | hive | true |
    35. // | core | false |
    36. // +-------------+-------+

    Python

    SQL CLI

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

    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.

      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.