默认数据库"default"
可以显式切换数据库:hive> use 数据库名;
创建
hive>CREATE DATABASE[IF NOT EXISTS] mydb
[LOCATION] '/.......'
[COMMENT] '....';
实例
hive (default)> create database test_db comment 'test database';
OK
Time taken: 0.33 seconds
hive (default)> create database if not exists test_db comment 'test database';
OK
Time taken: 0.013 seconds
hive (default)> create database if not exists test2_dblocation'/home/hive2.1/test_database';
OK
Time taken: 0.054 seconds
注意:location 不是linux的文件系统,是hdfs的。location是指定目录,
如果不指定会默认放到/user/hive/warehouse 下。下面查看可知。
hive (test2_db)> describe database test2_db;
OK
db_name comment location owner_name owner_type parameters
test2_db hdfs://hello110:9000/home/hive2.1/test_database hadoop USER
Linux文件系统里没有
[hadoop@hello110 test_database]$ pwd
/home/hive2.1/test_database
[hadoop@hello110 test_database]$ ll
total 0
显示
hive>SHOW DATABASES;描述
hive>DESCRIBE DATABASE [extended] mydb;删除
hive>DROP DATABASE [IF EXISTS] mydb [CASCADE];
实例
hive (test2_db)> drop database test2_db;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database test2_db is not empty. One or more tables exist.)
cascade关键字,强制删除,hive会把文件移动hdfs回收站
hive (test2_db)> drop database test2_db cascade;
Moved: 'hdfs://hello110:9000/home/hive2.1/test_database/t1' to trash at: hdfs://hello110:9000/user/hadoop/.Trash/Current
Moved: 'hdfs://hello110:9000/home/hive2.1/test_database' to trash at: hdfs://hello110:9000/user/hadoop/.Trash/Current
OK
Time taken: 1.384 seconds
hive (test2_db)>