database
相当于一个目录或者命名空间,用来更好地进行表的管理
在hdfs 的目录位置大致如下:
[root@iZbp12vtv76y9q3d633bh6Z /]# hadoop fs -ls /user/hive/warehouse/
Found 14 items
drwxrwxrwx - admin supergroup 0 2018-04-16 11:14 /user/hive/warehouse/action.db
drwxrwxrwx - hdfs supergroup 0 2018-06-06 02:09 /user/hive/warehouse/clean.db
drwxrwxrwx - hdfs supergroup 0 2018-05-14 14:22 /user/hive/warehouse/da.db
drwxrwxrwx - admin supergroup 0 2018-06-06 11:25 /user/hive/warehouse/dimension.db
drwxrwxrwx - admin supergroup 0 2018-03-20 14:14 /user/hive/warehouse/dp_life
drwxrwxrwx - admin supergroup 0 2018-05-27 21:29 /user/hive/warehouse/dw.db
drwxrwxrwx - admin supergroup 0 2018-05-04 16:14 /user/hive/warehouse/emp
drwxrwxrwx - admin supergroup 0 2018-05-04 16:57 /user/hive/warehouse/fab_sama.db
drwxrwxrwx - admin supergroup 0 2018-06-06 01:50 /user/hive/warehouse/final.db
drwxrwxrwx - admin supergroup 0 2018-03-08 20:16 /user/hive/warehouse/label.db
drwxrwxrwx - admin supergroup 0 2018-06-05 16:51 /user/hive/warehouse/location.db
drwxrwxrwx - hdfs supergroup 0 2018-06-02 14:02 /user/hive/warehouse/ods.db
drwxrwxrwx - admin supergroup 0 2018-05-28 11:34 /user/hive/warehouse/pp_download.db
drwxrwxrwx - admin supergroup 0 2018-05-29 14:23 /user/hive/warehouse/yunong_test.db
[root@iZbp12vtv76y9q3d633bh6Z /]#
创建语法:
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];
DATABASE 和 SCHEMA 是一样的概念。
例子:
create database if not exists demo
comment 'demo db'
lacation '/user/hive/financials.db'
with with dbproperties('creator' = 'lidiliang', 'date' = '2018-05-15');
删除语法
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
注意,默认drop table 时候是restrict 的,即数据库里面存在表格的时候,如果删除,会报错。
需要强制删除,请加上cascade
修改
语法:
ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);
-- (Note: SCHEMA added in Hive 0.14.0)
ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role;
-- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0)
ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path;
-- (Note: Hive 2.2.1, 2.4.0 and later)
database 创建删除修改语句例子
hive> create database mydatabase; ## 创建数据库
hive> show databases; ## 展示数据库
OK
default
mydatabase
Time taken: 0.131 seconds, Fetched: 2 row(s)
hive> create database mydatabase; ### 如果数据库已经存在则会报错
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask.
Database mydatabase already exists
hive> create database if not exists mydatabase; ### 避免数据库已经存在的时候,提示数据库已经存在
OK
Time taken: 0.011 seconds
hive> show databases; ## 展示数据库
OK
default
mydatabase
Time taken: 0.008 seconds, Fetched: 2 row(s)
hive> create database if not exists helloworld; ## 创建数据库
OK
Time taken: 0.04 seconds
hive> show databases like ".*"; ## 展示数据库
OK
default
helloworld
mydatabase
Time taken: 0.014 seconds, Fetched: 3 row(s)
hive> show databases like "h.*"; ## 展示数据库
OK
helloworld
Time taken: 0.008 seconds, Fetched: 1 row(s)
hive> create database financails location 'user/hive/financials.db'; ## locaation 后面应该跟的是绝对路径
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: hdfs://localhost:9000./user/hive/financials.db)
hive> create database financails location '/user/hive/financials.db'; ## location 指定db 目录。
OK
Time taken: 0.037 seconds
hive> create database financailsv1 location '/ldl/hive/financialsv1.db'; ## 如果目录不存在会在hdfs 上进行自动创建
OK
Time taken: 0.035 seconds
hive> create database demo comment 'this is a demo databases'; ## 添加提示符号信息
OK
Time taken: 0.031 seconds
hive> describe database demo; ## 描述数据库创建信息
OK
demo this is a demo databases hdfs://localhost:9000/user/hive/warehouse/demo.db root USER
Time taken: 0.011 seconds, Fetched: 1 row(s)
hive> create database demov1 with dbproperties('creator' = 'lidiliang', 'date' = '2018-05-15');
OK #### 创建数据库的时候,指定额外的信息
Time taken: 0.049 seconds
hive> describe database demov1; ### 描述数据的创建流程
OK
demov1 hdfs://localhost:9000/user/hive/warehouse/demov1.db root USER
Time taken: 0.013 seconds, Fetched: 1 row(s)
hive> describe database extended demov1; ## 如果需要看到dbproperties 设置的信息,需要添加extended 信息。
OK
demov1 hdfs://localhost:9000/user/hive/warehouse/demov1.db root USER {date=2018-05-15, creator=lidiliang}
Time taken: 0.011 seconds, Fetched: 1 row(s)
hive> use default;
OK
Time taken: 0.037 seconds
hive> set hive.cli.print.current.db=true; ## 在当前命令行里面显示当前使用的数据库名称
hive (default)> use demov1;
OK
Time taken: 0.01 seconds
hive (demov1)> set hive.cli.print.current.db=false;
hive> drop database hehe; ## 删除数据库
FAILED: SemanticException [Error 10072]: Database does not exist: hehe
hive> drop database if exists hehe; ## 删除数据库,可以避免数据库不存在的时候的报错。
OK
Time taken: 0.005 seconds
hive> drop database if exists hehe cascade; ## 删除数据库的
##前提是数据库里面没有表格,如果有表格,则会报错,可以加上cascade 来进行新删除数据库里面的表格。
OK
Time taken: 0.005 seconds
## 以下是数据库修改
hive> alter database demov1 set dbproperties('edited-by' = 'lidiliang', 'edit-date'='2018-05-15-09-17');
OK
Time taken: 0.034 seconds
hive>