//用户可以用 IF NOT EXISTS 选项来忽略这个异常。
create database [ if not exists ] myhive ;
create database myhive2 location '/myhive2';
alter database myhive2 set dbproperties('createtime'='202004090');
①查看数据库基本信息
desc database myhive2;
②查看数据库更多详细信息
desc database extended myhive2;
① 删除一个空数据库,如果数据库下面有数据表,那么就会报错
drop database myhive2;
②强制删除数据库,包含数据库下面的表一起删除
drop database myhive cascade;
//包含数据库下面的表一起删除; 不要执行,危险动作
use myhive(自己的数据库名称);
分类 | 类型 | 描述 | 字面量实例 |
---|---|---|---|
原始类型 | BOOLEAN | true/false | TRUE |
TINYINT | 1字节的有符号整数 -128~127 | 1Y | |
SMALLINT | 2个字节的有符号整数,-32768~32767 | 1S | |
INT | 4个字节的带符号整数 | 1 | |
BIGINT | 8字节带符号整数 | 1L | |
FLOAT | 4字节单精度浮点数1.0 | ||
DOUBLE | 8字节双精度浮点数 | 1.0 | |
DEICIMAL | 任意精度的带符号小数 | 1.0 | |
STRING | 字符串,变长 | “a”,’b’ | |
VARCHAR | 变长字符串 | “a”,’b’ | |
CHAR | 固定长度字符串 | “a”,’b’ | |
BINARY | 字节数组 | 无法表示 | |
TIMESTAMP | 时间戳,毫秒值精度 | 122327493795 | |
DATE | 日期 | ‘2020-04-29’ | |
INTERVAL | 时间频率间隔 | ||
复杂类型 | ARRAY | 有序的的同类型的集合 | array(1,2) |
MAP | key-value,key必须为原始类型,value可以任意类型 | map(‘a’,1,’b’,2) | |
STRUCT | 字段集合,类型可以不同 | struct(‘1’,1,1.0), named_stract(‘col1’,’1’,’col2’,1,’clo3’,1.0) | |
UNION | 在有限取值范围内的一个值 | create_union(1,’a’,63) |
1、创建基本数据表(内部表)
create table tableName(字段名称 字段类型,字段名称 字段类型)
ROW FORMAT DELIMITED IELDS TERMINATED BY char(char分隔符)
指定数据中字段与字段的分隔符 ‘\t’ 或 ‘,’ 或 ‘|’ 或其他
create table if not exists stu2(id int ,name string) row format delimited fields terminated by '\t' stored as textfile location '/user/stu2';
create table stu3 as select * from stu2;
create table stu4 like stu2;
1
desc formatted stu2;
1. 外部表说明 外部表因为是指定其他的hdfs路径的数据加载到表当中来,所以hive表会认为自己不完全独占这份数据,所以删除hive表的时候,数据仍然存放在hdfs当中,不会删掉。 2. 管理表和外部表的使用场景 每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表。
create EXTERNAL table tableName(字段名称 字段类型,字段名称 字段类型) 建外部表需要指定数据的存储路径。通过LOCATION进行指定。
①创建老师表:
create external table techer (t_id string,t_name string) row format delimited fields terminated by '\t';
1
②创建学生表:
create external table student (s_id string,s_name string,s_birth string , s_sex string ) row format delimited fields terminated by '\t';
1
load data local inpath ‘文件路径’ into table 表名;
1
load data local inpath ‘文件路径’ overwrite into table 表名;
cd /export/servers/hivedatas
hdfs dfs -mkdir -p /hivedatas
hdfs dfs -put techer.csv /hivedatas/
load data inpath '/hivedatas/techer.csv' into table techer;
alter table old_table_name rename to new_table_name;
//把表score4修改成score5
alter table score4 rename to score5;
desc score5;
alter table score5 add columns (mycol string, mysco string);
desc score5;
alter table score5 change column mysco mysconew int;
desc score5;
truncate table 表名;
drop table score5;