1.创建数据库
hive > create database db_hive;
# 或者
hive > create database if not exists db_hive;
数据库在HDFS上的默认存储路径是/usr/hive/warehouse/数据库名.db
hive > show databases;
hive > show databases like ‘db_hive*‘;
hive > desc database db_hive;
#或者
hive > desc database extended db_hive;
hive > use db_hive;
#删除空数据库
hive > drop database db_hive;
#如果删除数据库不存在,最好采用 if exists 判断数据库是否存在
hive > drop database if exists db_hive;
#如果数据库中有表存在,这里需要使用cascade强制删除数据库
hive > drop database if exists db_hive cascade;
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] 分区
[CLUSTERED BY (col_name, col_name, ...) 分桶
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format] row format delimited fields terminated by “分隔符”
[STORED AS file_format]
[LOCATION hdfs_path]
use myhive;
create table stu(id int,name string);
insert into stu(id,name) values(1,"zhangsan");
#查询
select * from stu;
create table if not exists myhive.stu1 as select id,name from stu;
#查询
select * from stu1;
create table if not exists myhive2.stu2 like stu;
#查询
select * from stu2;
* hql标准语句创建表
```shell
use myhive;
create table if not exists stu3(
id int,
name string
)row format delimited fields terminated by ‘\t‘
stored as textfile
location ‘/user/stu3‘
create external table myhive.teacher(t_id string,t_name string)
row format delimited fields terminated by ‘\t‘;
#内部表转换为外部表
alter table stu set tblproperties(‘EXTERNAL‘=‘TRUE‘);
#外部表转换为内部表
alter table teacher set tblproperties(‘EXTERNAL‘=‘TRUE‘);
内部表和外部表的区别
内部表和外部表的使用时机
原文:https://www.cnblogs.com/tenic/p/14728470.html