Hive 基本操作(创建数据库与创建数据库表)

您所在的位置:网站首页 hive是什么类型数据库 Hive 基本操作(创建数据库与创建数据库表)

Hive 基本操作(创建数据库与创建数据库表)

#Hive 基本操作(创建数据库与创建数据库表)| 来源: 网络整理| 查看: 265

二、Hive基本操作

2.1、创建数据库与创建数据库表 创建数据库操作 创建数据库

create database if not exists myhive; use myhive;

Hive 基本操作(创建数据库与创建数据库表)_Hive

说明:hive的表存放位置模式是由hive-site.xml当中的一个属性指定的

hive.metastore.warehouse.dir /user/hive/warehouse

创建数据库并指定hdfs存储位置

create database myhive2 location '/myhive2';

修改数据库 可以使用alter database 命令来修改数据库的一些属性。但是数据库的元数据信息是不可更改的,包括数据库的名称以及数据库所在的位置

修改数据库的创建日期

alter database myhive2 set dbproperties('createtime'='20880611');

查看数据库详细信息 查看数据库基本信息

desc database myhive2;

查看数据库更多详细信息

desc database extended myhive2;

Hive 基本操作(创建数据库与创建数据库表)_Hive_02 删除数据库 删除一个空数据库,如果数据库下面有数据表,那么就会报错

drop database myhive2;

强制删除数据库,包含数据库下面的表一起删除

drop database myhive 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] [STORED AS file_format] [LOCATION hdfs_path]

说明: 1、CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。 2、EXTERNAL关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION),Hive 创建内部表时,会将数据移动到数据仓库指向的路径;

若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。

3、LIKE 允许用户复制现有的表结构,但是不复制数据。 4、ROW FORMAT DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, …)] 用户在建表的时候可以自定义 SerDe 或者使用自带的 SerDe。如果没有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,将会使用自带的 SerDe。在建表的时候,用户还需要为表指定列,用户在指定表的列的同时也会指定自定义的 SerDe,Hive通过 SerDe 确定表的具体的列的数据。 5、STORED AS SEQUENCEFILE|TEXTFILE|RCFILE 如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。

6、CLUSTERED BY 对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分。Hive也是 针对某一列进行桶的组织。Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中。 把表(或者分区)组织成桶(Bucket)有两个理由: (1)获得更高的查询处理效率。桶为表加上了额外的结构,Hive 在处理有些查询时能利用这个结构。具体而言,连接两个在(包含连接列的)相同列上划分了桶的表,可以使用 Map 端连接 (Map-side join)高效的实现。比如JOIN操作。对于JOIN操作两个表有一个相同的列,如果对这两个表都进行了桶操作。那么将保存相同列值的桶进行JOIN操作就可以,可以大大较少JOIN的数据量。 (2)使取样(sampling)更高效。在处理大规模数据集时,在开发和修改查询的阶段,如果能在数据集的一小部分数据上试运行查询,会带来很多方便。

管理表 hive建表初体验

use myhive; create table stu(id int,name string); insert into stu values (1,"zhangsan"); select * from stu;

Hive建表时候的字段类型 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

分类 类型 描述 字面量示例

原始类型 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 日期 ‘2016-03-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)

⭐⭐

创建表并指定字段之间的分隔符

create table if not exists stu2(id int ,name string) row format delimited fields terminated by '\t' stored as textfile location '/user/stu2'; insert into stu2 values (1,"zhangsan"); insert into stu2 values (2,"lisi"); insert into stu2 values (3,"wangwu");

根据查询结果创建表

create table stu3 as select * from stu2;

根据已经存在的表结构创建表

create table stu4 like stu2;

查询表的类型

desc formatted stu2;

外部表: 外部表说明: 外部表因为是指定其他的hdfs路径的数据加载到表当中来,所以hive表会认为自己不完全独占这份数据,所以删除hive表的时候,数据仍然存放在hdfs当中,不会删掉

管理表和外部表的使用场景: 每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表。

操作案例 分别创建老师与学生表外部表,并向表中加载数据 创建老师表:

create external table techer (t_id string,t_name string) row format delimited fields terminated by '\t';

创建学生表:

create external table student (s_id string,s_name string,s_birth string , s_sex string ) row format delimited fields terminated by '\t';

从本地文件系统向表中加载数据

load data local inpath '/export/servers/hivedatas/student.csv' into table student;

加载数据并覆盖已有数据

load data local inpath '/export/servers/hivedatas/student.csv' overwrite into table student;

从hdfs文件系统向表中加载数据(需要提前将数据上传到hdfs文件系统,其实就是一个移动文件的操作)

cd /export/servers/hivedatas hdfs dfs -mkdir -p /hivedatas hdfs dfs -put techer.csv /hivedatas/ load data inpath '/hivedatas/techer.csv' into table techer;

如果删掉techer表,hdfs的数据仍然存在,并且重新创建表之后,表中就直接存在数据了,因为我们的techer表使用的是外部表,drop table之后,表当中的数据依然保留在hdfs上面了

⭐⭐ 分区表: 在大数据中,最常用的一种思想就是分治,我们可以把大的文件切割划分成一个个的小的文件,这样每次操作一个小的文件就会很容易了,同样的道理,在hive当中也是支持这种思想的,就是我们可以把大的数据,按照每天,或者每小时进行切分成一个个的小的文件,这样去操作小的文件就会容易得多了

创建分区表语法

create table score(s_id string,c_id string, s_score int) partitioned by (month string) row format delimited fields terminated by '\t';

创建一个表带多个分区

create table score2 (s_id string,c_id string, s_score int) partitioned by (year string,month string,day string) row format delimited fields terminated by '\t';

加载数据到分区表中

load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='201806');

加载数据到一个多分区的表中去

load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition(year='2018',month='06',day='01');

多分区联合查询使用union all来实现

select * from score where month = '201806' union all select * from score where month = '201806';

查看分区

show partitions score;

添加一个分区

alter table score add partition(month='201805');

同时添加多个分区

alter table score add partition(month='201804') partition(month = '201803');

注意:添加分区之后就可以在hdfs文件系统当中看到表下面多了一个文件夹

删除分区

alter table score drop partition(month = '201806');

外部分区表综合练习: 需求描述:现在有一个文件score.csv文件,存放在集群的这个目录下/scoredatas/month=201806,这个文件每天都会生成,存放到对应的日期文件夹下面去,文件别人也需要公用,不能移动。需求,创建hive对应的表,并将数据加载到表中,进行数据统计分析,且删除表之后,数据不能删除

需求实现: 数据准备:

hdfs dfs -mkdir -p /scoredatas/month=201806 hdfs dfs -put score.csv /scoredatas/month=201806/

创建外部分区表,并指定文件数据存放目录

create external table score4(s_id string, c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t' location '/scoredatas';

进行表的修复,就是建立表与数据文件之间的一个关系映射

msck repair table score4;

修复成功之后即可看到数据已经全部加载到表当中去了

select * from score4;

第二种实现方式,上传数据之后手动添加分区即可 数据准备:

hdfs dfs -mkdir -p /scoredatas/month=201805 hdfs dfs -put score.csv /scoredatas/month=201805

修改表,进行手动添加方式

alter table score4 add partition(month='201805');

⭐⭐

分桶表 将数据按照指定的字段进行分成多个桶中去,说白了就是将数据按照字段进行划分,可以将数据按照字段划分到多个文件当中去 开启hive的桶表功能

set hive.enforce.bucketing=true;

设置reduce的个数

set mapreduce.job.reduces=3;

创建桶表

create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t';

桶表的数据加载,只能通过insert overwrite。hdfs dfs -put文件或者通过load data无法加载

创建普通表,并通过insert overwrite的方式将普通表的数据通过查询的方式加载到桶表当中去

创建普通表:

create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';

普通表中加载数据

load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;

通过insert overwrite给桶表中加载数据

insert overwrite table course select * from course_common cluster by(c_id);

修改表 表重命名 基本语法: alter table old_table_name rename to new_table_name;

把表score4修改成score5 alter table score4 rename to score5;

Hive 基本操作(创建数据库与创建数据库表)_Hive_03

增加/修改列信息 (1)查询表结构

desc score5;

(2)添加列

alter table score5 add columns (mycol string, mysco string);

(3)查询表结构

desc score5;

(4)更新列

alter table score5 change column mysco mysconew int;

(5)查询表结构

desc score5;

删除表

drop table score5;

hive表中加载数据 直接向分区表中插入数据

create table score3 like score; insert into table score3 partition(month ='201807') values ('001','002','100');

通过查询插入数据 通过load方式加载数据

load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');

通过查询方式加载数据

create table score4 like score; insert overwrite table score4 partition(month = '201806') select s_id,c_id,s_score from score;

注: 关键字overwrite 必须要有 多插入模式 常用于实际生产环境当中,将一张表拆开成两部分或者多部分 给score表加载数据

load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');

创建第一部分表:

create table score_first( s_id string,c_id string) partitioned by (month string) row format delimited fields terminated by '\t' ;

创建第二部分表:

create table score_second(c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t';

分别给第一部分与第二部分表加载数据

from score insert overwrite table score_first partition(month='201806') select s_id,c_id insert overwrite table score_second partition(month = '201806') select c_id,s_score;

Hive 基本操作(创建数据库与创建数据库表)_Hive_04 查询语句中创建表并加载数据(as select) 将查询的结果保存到一张表当中去

create table score5 as select * from score;

创建表时通过location指定加载数据路径 1)创建表,并指定在hdfs上的位置

create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/myscore6';

2)上传数据到hdfs上

hdfs dfs -mkdir -p /myscore6 hdfs dfs -put score.csv /myscore6;

3)查询数据

select * from score6;

export导出与import 导入 hive表数据(内部表操作)

create table techer2 like techer; export table techer to '/export/techer'; import table techer2 from '/export/techer';

hive表中的数据导出(了解) 将hive表中的数据导出到其他任意目录,例如linux本地磁盘,例如hdfs,例如mysql等等 insert导出 1)将查询的结果导出到本地

insert overwrite local directory '/export/servers/exporthive/a' select * from score;

2)将查询的结果格式化导出到本地

insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from student;

collection items terminated by ‘#’ 对集合类型使用#来进行分割

3)将查询的结果导出到HDFS上(没有local)

insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from score;

注: 对于集合类型我们使用#来进行分割,因为这个表里面没有集合类型,所以加不加这个结果都一样

Hadoop命令导出到本地

dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;

Hive 基本操作(创建数据库与创建数据库表)_Hive_05

hive shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)

bin/hive -e "select * from myhive.score;" > /export/servers/exporthive/score.txt

Hive 基本操作(创建数据库与创建数据库表)_Hive_06

export导出到HDFS上

export table score to '/export/exporthive/score';

Hive 基本操作(创建数据库与创建数据库表)_Hive_07

sqoop导出 后续单独讲。

清空表数据 只能清空管理表,也就是内部表

truncate table score5;


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3