【博学谷学习记录】超强总结,用心分享|Hive表基础语法

发布于:2022-10-16 ⋅ 阅读:(362) ⋅ 点赞:(0)


创建数据库

create database if not exists myhive;
use  myhive;

hive的表在HDFS中默认存放位置模式是由hive-site.xml当中的一个属性指定
hive.metastore.warehouse.dir
/user/hive/warehouse

![image.png](https://img-blog.csdnimg.cn/img_convert/5014d5218b06f853cd1175e54ce7ac86.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=599&id=u0e7de6dc&margin=[object Object]&name=image.png&originHeight=599&originWidth=1252&originalType=binary&ratio=1&rotation=0&showTitle=false&size=54115&status=done&style=none&taskId=ua3385606-7c99-4c71-acaa-302e91f6134&title=&width=1252)

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

create database myhive2 location '/myhive2';

![image.png](https://img-blog.csdnimg.cn/img_convert/a1436f203f13f6607c4cb1a522708a06.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=282&id=u1332f7ed&margin=[object Object]&name=image.png&originHeight=282&originWidth=758&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29517&status=done&style=none&taskId=u4d690422-e2e9-4d2d-8cb4-0b4c3f3bd2d&title=&width=758)
![image.png](https://img-blog.csdnimg.cn/img_convert/112a15d0c80f4ea3909f7a0c79f52b16.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=482&id=u0cf405b5&margin=[object Object]&name=image.png&originHeight=482&originWidth=1267&originalType=binary&ratio=1&rotation=0&showTitle=false&size=39884&status=done&style=none&taskId=ubcabaf60-8a88-40aa-9eeb-c9158ed983f&title=&width=1267)

删除数据库

只能删除空数据库,如果数据库下面有数据表会报错

drop database myhive2;

![image.png](https://img-blog.csdnimg.cn/img_convert/3844cb4f453f50e4be18a292b660dd9a.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=358&id=uf7c05879&margin=[object Object]&name=image.png&originHeight=358&originWidth=1339&originalType=binary&ratio=1&rotation=0&showTitle=false&size=37364&status=done&style=none&taskId=ucb0aa9ad-6bff-40bd-8eb8-cdea42ab19a&title=&width=1339)

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

drop database myhive2 cascade;

删除前
![image.png](https://img-blog.csdnimg.cn/img_convert/392f6d4b191edf707f0e52fa5b491f29.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=209&id=uc3916d6b&margin=[object Object]&name=image.png&originHeight=209&originWidth=742&originalType=binary&ratio=1&rotation=0&showTitle=false&size=23710&status=done&style=none&taskId=u54e9c486-e3c9-4e15-8683-97fa4e54d26&title=&width=742)
删除后
![image.png](https://img-blog.csdnimg.cn/img_convert/d52979086cfce7caaef82ea52e9cfe08.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=315&id=u476c11a1&margin=[object Object]&name=image.png&originHeight=315&originWidth=772&originalType=binary&ratio=1&rotation=0&showTitle=false&size=32407&status=done&style=none&taskId=u1afc4a5e-3143-4166-91ea-137e506d736&title=&width=772)

创建表(内部表)

未被external修饰的是内部表(managed table),内部表又称管理表,内部表数据存储的位置由hive.metastore.warehouse.dir参数决定(默认:/user/hive/warehouse),删除内部表会直接删除元数据(metadata)及存储数据,因此内部表不适合和其他工具共享数据

create table test1(
    id int,
    name string
);

![image.png](https://img-blog.csdnimg.cn/img_convert/4c97ea31d24e4ed3d9cbfcfa03b8dc66.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=516&id=u4124ff64&margin=[object Object]&name=image.png&originHeight=516&originWidth=664&originalType=binary&ratio=1&rotation=0&showTitle=false&size=43843&status=done&style=none&taskId=ue54406d6-b156-4db0-be14-d847cd13cf6&title=&width=664)

内部表的表类型为managed_table

![image.png](https://img-blog.csdnimg.cn/img_convert/aedab41ce173842e4b69a20ac085025f.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=629&id=u37420c7e&margin=[object Object]&name=image.png&originHeight=629&originWidth=1087&originalType=binary&ratio=1&rotation=0&showTitle=false&size=68389&status=done&style=none&taskId=u6431ef3d-9587-4ad2-aa66-8a9f6ca241b&title=&width=1087)

hdfs默认的表存储位置

![image.png](https://img-blog.csdnimg.cn/img_convert/2553d83cb94c3dbac61fce008bad2911.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=724&id=u7c7b4bd1&margin=[object Object]&name=image.png&originHeight=724&originWidth=1275&originalType=binary&ratio=1&rotation=0&showTitle=false&size=39685&status=done&style=none&taskId=uf244a40f-5c87-4c56-bd48-33d9578b2f6&title=&width=1275)

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

create table if not exists test2(
    id int,
    name string
) row format delimited
    fields terminated by ' ';

根据查询结果创建表

create table test3 as select * from test1;

![image.png](https://img-blog.csdnimg.cn/img_convert/254de004b3d78ab15cc91c669f346e42.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=275&id=u80e8cfd2&margin=[object Object]&name=image.png&originHeight=275&originWidth=557&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16127&status=done&style=none&taskId=ucd22ad6b-8a31-4c46-b4b3-a3fe6a91723&title=&width=557)

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

create table test4 like test1;

![image.png](https://img-blog.csdnimg.cn/img_convert/e8dda1a69868ac01386d4efb4b113982.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=351&id=ud49e5050&margin=[object Object]&name=image.png&originHeight=351&originWidth=560&originalType=binary&ratio=1&rotation=0&showTitle=false&size=14694&status=done&style=none&taskId=u7e7d7324-5a81-440d-b939-827dbec54a3&title=&width=560)

删除表且删除表结构

drop table test4;

没有delete语法

![image.png](https://img-blog.csdnimg.cn/img_convert/fcc3c362045e522c750308c298369947.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=222&id=u66c862c5&margin=[object Object]&name=image.png&originHeight=222&originWidth=1118&originalType=binary&ratio=1&rotation=0&showTitle=false&size=15203&status=done&style=none&taskId=u3cf33b01-616b-441e-a444-00368f3bf6f&title=&width=1118)

需要清空表数据可以使用truncate

清空前
![image.png](https://img-blog.csdnimg.cn/img_convert/32ab54ee5c5341615ab2578a85b212ee.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=264&id=uc8aeea6b&margin=[object Object]&name=image.png&originHeight=264&originWidth=455&originalType=binary&ratio=1&rotation=0&showTitle=false&size=15119&status=done&style=none&taskId=ud8bcfa4e-2bcb-459f-9332-cf3d8debf6d&title=&width=455)
清空表数据且保留表结构
![image.png](https://img-blog.csdnimg.cn/img_convert/fed4218e5c7dc3fb7736fd3f2d9ec972.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=328&id=u6f85e489&margin=[object Object]&name=image.png&originHeight=328&originWidth=481&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16751&status=done&style=none&taskId=uf7b45828-9dc3-44d9-bb17-23fd30c7c8d&title=&width=481)

创建表(外部表)

在创建表的时候可以指定external关键字创建外部表,外部表对应的文件存储在location指定的hdfs目录下,向该目录添加新文件的同时,该表也会读取到该文件(当然文件格式必须跟表定义的一致)。
外部表因为是指定其他的hdfs路径的数据加载到表当中来,所以hive表会认为自己不完全独占这份数据,所以删除hive外部表的时候,数据仍然存放在hdfs当中,不会删掉。

create external table test4
(
    id   string,
    name string
) row format delimited
    fields terminated by '\t';

外部数据加载命令load

  1. 上传一个和已创建的表结构相同的数据文件到hdfs任意目录
  2. ![image.png](https://img-blog.csdnimg.cn/img_convert/bd941f69772573f891dd02d904dc18a2.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=721&id=ue20187e8&margin=[object Object]&name=image.png&originHeight=721&originWidth=1275&originalType=binary&ratio=1&rotation=0&showTitle=false&size=62497&status=done&style=none&taskId=u1c9747ad-5bf5-4a04-8829-72a31345ae5&title=&width=1275)
  3. 加载文件中的数据到hive表中

load操作实际上移动文件的操作,如果删掉test4表,hdfs的数据仍然存在,并且重新创建表之后,表中就直接存在数据了

load data inpath '/myhive/input/teacher.txt' into table test4;

执行加载命令前

文件位置: 有一个tearch.txt文件

![image.png](https://img-blog.csdnimg.cn/img_convert/b79dcb5fe7941c58f7f45cc34ce8d357.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=478&id=u78221dd2&margin=[object Object]&name=image.png&originHeight=478&originWidth=1275&originalType=binary&ratio=1&rotation=0&showTitle=false&size=34595&status=done&style=none&taskId=uba158772-9ee9-4977-bd24-55a58b0da18&title=&width=1275)

外部表目录: 无文件

![image.png](https://img-blog.csdnimg.cn/img_convert/af37ec3c13622f16fee62c2e687b39e4.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=724&id=uf3998192&margin=[object Object]&name=image.png&originHeight=724&originWidth=1275&originalType=binary&ratio=1&rotation=0&showTitle=false&size=35763&status=done&style=none&taskId=u32a25d0c-6d92-41a4-b0b5-387d421523c&title=&width=1275)

执行加载命令后

文件位置: 无文件

![image.png](https://img-blog.csdnimg.cn/img_convert/5382b9a9931105dbe799877ad29b4384.png#clientId=u900855cb-aabf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=482&id=u683ff890&margin=[object Object]&name=image.png&originHeight=482&originWidth=1275&originalType=binary&ratio=1&rotation=0&showTitle=false&size=31835&status=done&style=none&taskId=u15f432a5-8596-4945-a8a0-50ad505ccd4&title=&width=1275)

外部表目录: 有一个tearcher.txt文件

复杂类型操作

Array类型

源数据

zhangsan beijing,shanghai,tianjin,hangzhou
wangwu changchun,chengdu,wuhan,beijing

建表语句

create external table hive_array(
    name string,
    locations array<string>
) row format delimited
    fields terminated by ' '
    collection items terminated by ',';

加载数据

load data inpath '/myhive/input/array.txt' into table hive_array;

查询
![image.png](https://img-blog.csdnimg.cn/img_convert/7bf4971652bbac67c70ed49d04d31317.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=570&id=u78ab73fa&margin=[object Object]&name=image.png&originHeight=570&originWidth=619&originalType=binary&ratio=1&rotation=0&showTitle=false&size=43232&status=done&style=none&taskId=u827c5c83-2d57-48c7-b90d-e5ae000b497&title=&width=619)

map类型

源数据

1,zhangsan,father:xiaoming#mother:xiaohuang#brother:xiaoxu,28
2,lisi,father:mayun#mother:huangyi#brother:guanyu,22
3,wangwu,father:wangjianlin#mother:ruhua#sister:jingtian,29
4,mayun,father:mayongzhen#mother:angelababy,26

建表语句

create table hive_map(
    id int,
    name string,
    members map<string,string>,
    age int
) row format delimited
    fields terminated by ','
    collection items terminated by '#'
    map keys terminated by ':';

加载语句

load data inpath '/myhive/input/map.txt' into table hive_map;

查询
![image.png](https://img-blog.csdnimg.cn/img_convert/c7bfc8585fd6fef3c33fdcd1fe341f9d.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=577&id=ua078ed19&margin=[object Object]&name=image.png&originHeight=577&originWidth=920&originalType=binary&ratio=1&rotation=0&showTitle=false&size=57353&status=done&style=none&taskId=u27ffba91-05a1-429b-b627-e064185ebb6&title=&width=920)

根据键找对应的值
-- 根据键找对应的值
select id, name, members['father'] father, members['mother'] mother, age
from hive_map;

![image.png](https://img-blog.csdnimg.cn/img_convert/549688cf006a14e3d10c61f395ada139.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=334&id=u9549cec7&margin=[object Object]&name=image.png&originHeight=334&originWidth=654&originalType=binary&ratio=1&rotation=0&showTitle=false&size=30731&status=done&style=none&taskId=u4338a9cb-4d94-4873-9ad2-af669cb49c2&title=&width=654)

获取所有键
select id, name, map_keys(members) as relations
from hive_map;

![image.png](https://img-blog.csdnimg.cn/img_convert/f2ea906e5f277f6844308cdaa4fb6909.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=305&id=u352297e4&margin=[object Object]&name=image.png&originHeight=305&originWidth=524&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29554&status=done&style=none&taskId=u4aae6259-cd00-4511-af82-23ff9f3c377&title=&width=524)

获取所有值
select id,name,map_values(members) as relations
from hive_map;

![image.png](https://img-blog.csdnimg.cn/img_convert/be1b2365ef9fddee4a6d2ed191c4b193.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=316&id=udca64977&margin=[object Object]&name=image.png&originHeight=316&originWidth=576&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29361&status=done&style=none&taskId=u5f075256-6b89-4bea-b521-9f140e402fb&title=&width=576)

获取键值对大小
select map_keys(members),size(members) as map_size
from hive_map;

![image.png](https://img-blog.csdnimg.cn/img_convert/4eb9c865b6fb0703c600b383bfe0c8cf.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=318&id=u03a65906&margin=[object Object]&name=image.png&originHeight=318&originWidth=486&originalType=binary&ratio=1&rotation=0&showTitle=false&size=28628&status=done&style=none&taskId=u223f0ddc-134b-4135-a5c9-f535f60d529&title=&width=486)

获取有指定key的数据
select *
from hive_map
where array_contains(map_keys(members),'brother');

![image.png](https://img-blog.csdnimg.cn/img_convert/479d963f792f8e95fc01a52bc01f9bed.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=297&id=u75db4bab&margin=[object Object]&name=image.png&originHeight=297&originWidth=916&originalType=binary&ratio=1&rotation=0&showTitle=false&size=32503&status=done&style=none&taskId=uaafcbbbb-7f12-488d-999e-0f60f39d3c0&title=&width=916)

获取包含指定key的数据,且获取value的内容
select id,name,members['brother'] as brother
from hive_map
where array_contains(map_keys(members),'brother');

![image.png](https://img-blog.csdnimg.cn/img_convert/0ac6acdcd48790118ada0dc2810a5aa2.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=322&id=u469137dd&margin=[object Object]&name=image.png&originHeight=322&originWidth=479&originalType=binary&ratio=1&rotation=0&showTitle=false&size=33627&status=done&style=none&taskId=uf28a4880-980d-4172-bfad-5d5a31a69b5&title=&width=479)

struct类型

源数据

192.168.1.1#zhangsan:40
192.168.1.2#lisi:50
192.168.1.3#wangwu:60
192.168.1.4#zhaoliu:70

建表语句

create table hive_struct(
    ip string,
    info struct<name:string,age:int>
) row format delimited
    fields terminated by '#'
    collection items terminated by ':';

加载语句

load data inpath '/myhive/input/struct.txt' into table hive_struct;

查询
![image.png](https://img-blog.csdnimg.cn/img_convert/7471dd4fa681954c2bd24e532c68e6e7.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=279&id=ub96f5224&margin=[object Object]&name=image.png&originHeight=279&originWidth=455&originalType=binary&ratio=1&rotation=0&showTitle=false&size=25584&status=done&style=none&taskId=uc9fa8227-7b15-42a0-a171-36b1da15b6a&title=&width=455)

查询指定成员信息
select ip,info.name
from hive_struct;

![image.png](https://img-blog.csdnimg.cn/img_convert/97e965214499b65a601e7956fc846fc8.png#clientId=u5ccf33dd-9107-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=300&id=ucae15b58&margin=[object Object]&name=image.png&originHeight=300&originWidth=304&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16433&status=done&style=none&taskId=u544d5443-6fa4-4924-b9b2-ca392f66239&title=&width=304)

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到