CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Day36-45/45.大数据平台和HiveSQL.md
Views: 729
Hive简介
Hive 是 Facebook 开源的一款基于 Hadoop 的数据仓库工具,目前由 Apache 软件基金会维护,它是应用最广泛的大数据处理解决方案,它能将 SQL 查询转变为 MapReduce(Google提出的一个软件架构,用于大规模数据集的并行运算)任务,对 SQL 提供了完美的支持,能够非常方便的实现大数据统计。
说明:可以通过https://www.edureka.co/blog/hadoop-ecosystem来了解 Hadoop 生态圈。
如果要简单的介绍 Hive,那么以下两点是其核心:
把 HDFS 中结构化的数据映射成表。
通过把 HQL 进行解析和转换,最终生成一系列基于 Hadoop 的 MapReduce 任务或 Spark 任务,通过执行这些任务完成对数据的处理。也就是说,即便不学习 Java、Scala 这样的编程语言,一样可以实现对数据的处理。
Hive的应用场景。
Hive和传统关系型数据库的对比如下图和下表所示。
Hive | RDBMS | |
---|---|---|
查询语言 | HQL | SQL |
存储数据 | HDFS | 本地文件系统 |
执行方式 | MapReduce / Spark | Executor |
执行延迟 | 高 | 低 |
数据规模 | 大 | 小 |
准备工作
搭建如下图所示的大数据平台。
通过Client节点(跳板机)访问大数据平台。
创建文件Hadoop的文件系统。
将准备好的数据文件拷贝到Hadoop文件系统中。
进入 hive 命令行。
建库建表
创建。
删除。
切换。
数据类型
Hive的数据类型如下所示。
基本数据类型:
数据类型 | 占用空间 | 支持版本 |
---|---|---|
tinyint | 1-Byte | |
smallint | 2-Byte | |
int | 4-Byte | |
bigint | 8-Byte | |
boolean | ||
float | 4-Byte | |
double | 8-Byte | |
string | ||
binary | 0.8版本 | |
timestamp | 0.8版本 | |
decimal | 0.11版本 | |
char | 0.13版本 | |
varchar | 0.12版本 | |
date | 0.12版本 |
复合数据类型:
数据类型 | 描述 | 例子 |
---|---|---|
struct | 和C语言中的结构体类似 | struct<first_name:string, last_name:string> |
map | 由键值对构成的元素的集合 | map<string,int> |
array | 具有相同类型的变量的容器 | array<string> |
创建内部表。
加载数据。
或
创建分区表。
提供分区数据。
设置动态分区。
修复分区。
查询
基本语法
group by
order by
常用函数
from_unixtime
:将时间戳转换成日期unix_timestamp
:将日期转换成时间戳datediff
:计算两个日期的时间差if
:根据条件返回不同的值substr
:字符串取子串get_json_object
:从JSON字符串中取出指定的key
对应的value
,如:get_json_object(info, '$.first_name')
。说明:MySQL对应的函数名字叫
json_extract
。