HIVE数仓的安装与使用

版权声明:Copyright ◎ https://blog.csdn.net/huoliangwu/article/details/84639010

hive的安装与简单入门

1 HIVE简单介绍

1.1什么是 Hive
Hive:由 Facebook 开源用于解决海量结构化日志的数据统计。
Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类 SQL 查询功能。
本质是:将HQL 转化成 MapReduce 程序
1.2Hive 架构原理
hive架构
1.3Hive 在加载数据的过程中不会对数据进行任何处理,甚至不会对数据进行扫描,因此也没有对数据中的某些 Key 建立索引。

2 HIVE安装

2.1 下载地址
http://archive.apache.org/dist/hive/
github: https://github.com/apache/hive
2.2 hive 安装部署

[hadoop@mini01 ~]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C ~/install

[hadoop@mini01 ~]$ cd install

[hadoop@mini01 install]$ mv apache-hive-1.2.1-bin.tar.gz hive

[hadoop@mini01 install]$ cd hive/conf

[hadoop@mini01 conf]$  mv hive-env.sh.template hive-env.sh

配置hive-env.sh 文件

  • (a)配置 HADOOP_HOME 路径
    export HADOOP_HOME=/home/hadoop/install/hadoop-2.7.2
  • (b)配置HIVE_CONF_DIR 路径
    exportHIVE_CONF_DIR=/home/hadoop/install/hive/conf

3.启动HIVE

3.1启动hadoop

[hadoop@mini01 ~]$ start-dfs.sh

[hadoop@mini01 ~]$ start-yarn.sh

3.2在hdfs上新建/tmp目录和/user/hive/warehouse

[hadoop@mini01 ~]$ hdfs dfs -mkdir /tmp

[hadoop@mini01 ~]$ hdfs dfs -mkdir -p /user/hive/warehouse

3.3启动hive

[hadoop@mini01 ~]$ /home/hadoop/hive/bin/hive

4.HIVE基本操作

  • 查看数据库
hive > show database;
  • 使用默认数据库
hive > use default;
  • 创建表
hive > create table student(id int, name string) ;
  • 查看表的结构
hive > desc student;
  • 向表中插入数据
hive > insert into student values(1000,"ss");

HQL语句是类SQL语句

5.HIVE的基本数据类型

Hive数据类型 Java数据类型 长度 例子
TINYINT byte 1byte 有符号整数 20
SMALINT short 2byte 有符号整数 20
INT int 4byte 有符号整数 20
BIGINT long 8byte 有符号整数 20
BOOLEAN boolean 布尔类型,true 或者false TRUE FALSE
FLOAT float 单精度浮点数 3.14159
DOUBLE double 双精度浮点数 3.14159
STRING string 字符系列。可以指定字符集。可以使用单引号或者双引号 ‘now is the time’ “for all good men”
TIMESTAMP 时间类型
BINARY 字节数组

对于Hive 的 String 类型相当于数据库的 varchar 类型,该类型是一个可变的字符串, 不过它不能声明其中最多能存储多少个字符,理论上它可以存储 2GB 的字符数。

5.2集合类型

数据类型 描述 语法示例
STRUCT 和 c 语言中的 struct 类似,都可以通过“点” 符号访问元素内容。例如,如果某个列的数据类型是 STRUCT{first STRING, lastSTRING}, 那么第 1 个元素可以通过字段.first 来引用。 struct()
MAP MAP 是一组键-值对元组集合,使用数组表示法可以访问数据。例如,如果某个列的数据类型是 MAP ,其中键-> 值对是’first’->’John’和’last’->’Doe’,那么可以通过字段名[‘last’]获取最后一个元素 map()
ARRAY 数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。例如,数组值为[‘John’, ‘Doe’],那么第 2 个元素可以通过数组名[1]进行引用 Array()

基于上述数据结构,我们在 Hive 里创建对应的表,并导入数据
创建本地文件test.txt

songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing

yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing

Hive 上创建测试表 test

hive> create table test( name string,
    > friends array<string>, children map<string, int>,
    > address struct<street:string, city:string>)
    > row format delimited fields terminated by ',' collection items terminated by '_'
    > map keys terminated by ':'
    > lines terminated by '\n';
OK
Time taken: 35.772 seconds
hive> 

读取数据到表中

hive> load data local inpath '/home/hadoop/test/test.txt' into table test;

访问三种集合列里的数据,以下分别是 ARRAY,MAP,STRUCT 的访问方式

hive> select friends[1],children['xiao song'],address.city 
    > from test where name="songsong";
OK
lili	18	beijing
Time taken: 4.662 seconds, Fetched: 1 row(s)

猜你喜欢

转载自blog.csdn.net/huoliangwu/article/details/84639010