CREATE DATABASE Statement
Creates a new database.
In Impala, a database is both:
- A logical construct for grouping together related tables, views, and functions within their own namespace. You might use a separate database for each application, set of related tables, or round of experimentation.
- A physical construct represented by a directory tree in HDFS. Tables (internal tables), partitions, and data files are all located under this directory. You can back it up, measure space usage, or remove it (if it is empty) with a DROP DATABASE statement.
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name[COMMENT 'database_comment'] [LOCATION hdfs_path];
Statement type: DDL
A database is physically represented as a directory in HDFS, with a filename extension .db, under the main Impala data directory. If the associated HDFS directory does not exist, it is created for you. All databases and their associated directories are top-level objects, with no physical or logical nesting.
After creating a database, to make it the current database within an impala-shell session, use the USE statement. You can refer to tables in the current database without prepending any qualifier to their names.
When you first connect to Impala through impala-shell, the database you start in (before issuing any CREATE DATABASE or USE statements) is named default.
show functions in _impala_builtins; show functions in _impala_builtins like '*substring*';
After creating a database, your impala-shell session or another impala-shell connected to the same node can immediately access that database. To access the database through the Impala daemon on a different node, issue the INVALIDATE METADATA statement first while connected to that other node.
Setting the LOCATION attribute for a new database is a way to work with sets of files in an HDFS directory structure outside the default Impala data directory, as opposed to setting the LOCATION attribute for each individual table.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed metadata has been received by all the Impala nodes. See SYNC_DDL Query Option for details.
When you create a database in Impala, the database can also be used by Hive.
create database first_db; use first_db; create table t1 (x int); create database second_db; use second_db; -- Each database has its own namespace for tables. -- You can reuse the same table names in each database. create table t1 (s string); create database temp; -- You do not have to USE a database after creating it. -- Just qualify the table name with the name of the database. create table temp.t2 (x int, y int); use database temp; create table t3 (s string); -- You cannot drop a database while it is selected by the USE statement. drop database temp; ERROR: AnalysisException: Cannot drop current default database: temp -- The always-available database 'default' is a convenient one to USE. use default; -- Before dropping a database, first drop all the tables inside it. drop database temp; ERROR: ImpalaRuntimeException: Error making 'dropDatabase' RPC to Hive Metastore: CAUSED BY: InvalidOperationException: Database temp is not empty show tables in temp; +------+ | name | +------+ | t3 | +------+ drop table temp.t3; drop database temp;
The SHOW DATABASES statement lists all databases, or the databases whose name matches a wildcard pattern.
Cancellation: Cannot be cancelled.
The user ID that the impalad daemon runs under, typically the impala user, must have write permission for the parent HDFS directory under which the database is located.