HIVE的基础知识内部表与外部表
程序员文章站
2022-04-28 23:46:37
...
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 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; //都可以