Hive-核心概念和架构原理(第一天)

Hive-核心概念和架构原理(第一天)

一、核心概念

hive是一个基于Hadoop的数据仓库工具,可以将结构化的数据映射成一张表,并提供SQL查询的功能。
HIVE与RMDBS数据库系统的区别

对比项HIVERMDBS
查询语言HQLSQL
数据存储HDFS本地磁盘
执行器MapReduceexecutor
数据插入批处理/单条处理单条处理/批处理
数据操作覆盖/追加行级增删改查
数据规模
执行延迟
分区支持支持
索引支持支持
扩展性
数据加载(校验)读时模式写时模式
应用场景海量数据查询实时查询

hive只适合做海量数据的离线数据统计分析,即数据仓库
**优点:**使用SQL操作简单,可实现自定义函数。
**缺点:**不支持记录的增删改查、查询时延高,不支持事务。

二、架构原理

hive架构原理图
元数据metastore:使用derby或MySQL数据库存储。
数据:使用hadoop的HDFS文件系统存储。
启动hive工具进入shell交互模式

  • 1、命令hive
  • 2、启动server服务:nohup hive --server hiveserver2 &
    命令beeline
    !connect jdbc:hive2://node1:10000
三、hive命令
hive -e "sql语句"//执行sql语句
hive -f xxx.sql//执行sql文件
四、数据类型
  • 1、基本类型
类型名称描述
int4字节有符号整数
bigint8字节有符号整数
float4字节单精度浮点数
double8字节双精度浮点数
string字符串(不设定长度)
varchar字符串(1-65355)
timestamp时间戳
date日期
  • 2、复合类型
类型名称描述举例
array有序字段,字段类型必须相同Array(元素1,元素2)获取值array[索引号],索引号从0开始
map无序键值对Map(k1,b1,k2,v2) 获取值map[‘key’]
struct命名的字段,字段类型可以不相同Struct(a:type1,b:type2,c:type3) 获取值 字段名.a 字段名.b 字段名.c
  • 3、数据类型转换
    **隐式类型转换:**系统自动实现类型转换
    **手动类型转换:**cast(字段 as 类型),如果类型转换失败,返回空值null。
五、DDL操作
  • 1、数据库DDL操作
create database db_hive;//创建数据库
create database if not exists db_hive;

show databases;//显示数据库
show databases like "db*";

desc database db_hive;//查看数据库详细信息
desc database extented db_hive;

use db_hive;//切换数据库

drop database db_hive;//删除数据库
drop database if exists db_hive;

drop database if exists db_hive cascade;//强制删除
  • 2、数据表DDL操作
create [external] table [if not exists] table_name
[(col_name data_type [comment col_name])]
[comment table_comment]
[partitioned by (col_name data_type [comment col_name],...)]//分区
[clustered by [col_name,col_name,...]]//分桶
[sorted by (col_name [asc][dasc],...) into num_buckets BUCKETS]//排序
[row format row_format]row format delimited fields terminated by "分隔符"
[collection items terminated by "分隔符"]
[map keys terminated by "分隔符"]
[lines terminated by "分隔符"]
[stored as file_format]
[location hdfs_path] 

file_format: sequencefile(序列化) textfile(纯文本) rcfile(列式存储)

  • 3、创建内部表

方式一

create table if not exists student(
id int,
name string
)
row format delimited fields terminated by "\t"
stored as textfile;

方式二

create table if not exists student1 as select * from student;//表结构和数据

方式三

create table if not exists student2 like student;//只有表结构,没有数据

查询表的类型

desc formatted student;
  • 4、创建外部表
create external table if not exists teacher(
id int,
name string,
age int
)
row format delimited fields terminated by "\t"
location "/bigdata/hive;
  • 5、内部表和外部表相互转换
alter table student set tblpropertied('external'='true');//内部表转外部表
alter table student set tblpropertied('external'='false');//外部表转内部表
  • 6、内部表和外部表的区别
    建表语法的不同:external关键字
    删表操作:内部表删除表后,数据也会被删除;外部表删除表后,数据不会被删除,还可以通过建表指定存放位置和格式可以进行恢复。
六、shell-hive交互窗口
  • 1、操作本地命令:!ls /
  • 2、操作HDFS:dfs -ls /
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值