HIVE的基础知识内部表与外部表

本文介绍了HIVE的基础知识,重点讲解了内部表和外部表的区别。内部表的数据在HDFS中存储为数据库目录的子文件夹,删除表会连同数据一起删除;外部表的数据保存在指定HDFS路径,删除表时数据依然保留。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HIVE:将结构化的数据文件映射为数据库表
查询语句简便话,类SQL语言;
表在hdfs中表现为文件夹

准备工作(要先启动hadoop和zookeeper)

[root@hadoop151 hive]# jps
15105 Jps
7715 NodeManager
2855 QuorumPeerMain
7303 DataNode
7179 NameNode
7455 SecondaryNameNode
7599 ResourceManager

启动之后应该要有的程序

启动HIVE

[root@hadoop151 hive]# nohup hive --service hiveserver2 &


[root@hadoop151 hive]# hive                  //hive命令行
[root@hadoop151 hive]# beeline -u "jdbc:hive2://localhost:10000"                //Beeline

上面两种启动的方式选其一即可,beeline里面显示表格有框

HIVE表里的基本数据类型

int 
double
decimal
boolean
string             //字符串
date            
timestamp           //时间戳

数据表

内部表

hdfs中为所属数据库目录下的子文件夹
删除表删除数据
示例:

hive>create database myhive;
hive> use myhive;
hive> create table if not exists user(id int,name string);
hive> insert into user values(1,"yang");

在这里插入图片描述
这是插入成功的状态,数据存放在表的目录下

hive> drop table user;

删除表之后,发现数据也被删除

外部表

数据保存在指定位置的hdfs路径中
删除表不会删除数据

实例:

现有如下数据

2020-7-716:11:47|12345|{enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:11:53|12345|{enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:12:0|12345|{enventName:click_img_cat,pageName:http://localhost:8080/index.html}

把它放到.log文件中

!hdfs dfs -mkdir /customers
!hdfs dfs -put action.log /customers
!hdfs dfs -chmod 777 /customers
hive> create external table if not exists usernew(
    > date string,
    > id string,
    > msg string
    > )
    > row format delimited fields terminated by "|"
    > location "/customers";
OK
Time taken: 0.115 seconds
hive> select * from usernew;
OK
2020-7-716:11:47        12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:11:53        12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:12:0 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:12:1 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:12:3 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:12:5 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:12:10        12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:20:59        12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:21:0 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:21:3 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:21:4 12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:21:24        12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
2020-7-716:21:25        12345   {enventName:click_img_cat,pageName:http://localhost:8080/index.html}
Time taken: 0.106 seconds, Fetched: 13 row(s)

hive> drop table usernew;

删除之后文件还在

退出

!q
quit;    //都可以
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值