基于Hadoop2.7.3集群数据仓库Hive1.2.2的部署及使用

HBase是一种分布式、面向列的NoSQL数据库,基于HDFS存储,以表的形式存储数据,表由行和列组成,列划分到列族中。HBase不提供类SQL查询语言,要想像SQL这样查询数据,可以使用Phonix,让SQL查询转换成hbase的扫描和对应的操作,也可以使用现在说讲Hive仓库工具,让HBase作为Hive存储。

Hive是运行在Hadoop之上的数据仓库,将结构化的数据文件映射为一张数据库表,提供简单类SQL查询语言,称为HQL,并将SQL语句转换成MapReduce任务运算。有利于利用SQL语言查询、分析数据,适于处理不频繁变动的数据。Hive底层可以是HBase或者HDFS存储的文件。
两者都是基于Hadoop上不同的技术,相互结合使用,可处理企业中不同类型的业务,利用Hive处理非结构化离线分析统计,利用HBase处理在线查询。

1.安装hive通过二进制包安装
下载地址:http://mirrors.shuosc.org/apache/hive/stable/apache-hive-1.2.2-bin.tar.gz
tar -zxf apache-hive-1.2.2-bin.tar.gz

配置环境变量

# vi /etc/profile
HIVE_HOME=/data/yunva/apache-hive-1.2.-bin
PATH=$PATH:$HIVE_HOME/bin
export HIVE_NAME PATH
# source /etc/profile

2.安装mysql,存储hive相关的信息(此处因为资源使用问题,mysql安装在了另外的服务器中)

# yum install -y mariadb mariadb-server
# systemctl start mariadb

在MySQL创建Hive元数据存放库和连接用户

mysql>create database hive;
mysql>grant all on *.* to'hive'@'%' identified by 'hive';
mysql>flush privileges;

3.配置hive

cd /data/yunva/apache-hive-1.2.-bin/conf
cp hive-default.xml.template hive-default.xml

配置hive连接mysql的信息
# vim hive-site.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://10.10.11.214:3306/hive?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property> <property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hive</value>
<description>username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>hive</value>
<description>password to use against metastore database</description>
</property>
</configuration>

4.安装java连接mysql的驱动
下载地址:https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.45.tar.gz
将解压的mysql-connector-java-5.1.45-bin.jar放到/data/yunva/apache-hive-1.2.2-bin/lib目录

5.启动Hive服务

# hive --service metastore &

[root@test3 apache-hive-1.2.-bin]# ps -ef|grep hive
root : pts/ :: /usr/java/jdk1..0_65/bin/java -Xmx256m -Djava.net.preferIPv4Stack=true -Dhadoop.log.dir=/data/yunva/hadoop-2.7./logs -Dhadoop.log.file=hadoop.log -Dhadoop.home.dir=/data/yunva/hadoop-2.7. -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,console -Dhadoop.policy.file=hadoop-policy.xml -Djava.net.preferIPv4Stack=true -Xmx512m -Dhadoop.security.logger=INFO,NullAppender org.apache.hadoop.util.RunJar /data/yunva/apache-hive-1.2.-bin/lib/hive-service-1.2..jar org.apache.hadoop.hive.metastore.HiveMetaStore
root : pts/ :: grep hive
[root@test3 apache-hive-1.2.-bin]# jps
HRegionServer
Jps
RunJa # hive会启动叫做RunJa的程序

客户端配置,需要集成Hadoop环境
scp -P 48490 -r apache-hive-1.2.2-bin 10.10.114.112:/data/yunva

配置环境变量:
vim /etc/profile

# hive client
HIVE_HOME=/data/yunva/apache-hive-1.2.2-bin
PATH=$PATH:$HIVE_HOME/bin
export HIVE_NAME PATH

# vi hive-site.xml(或者直接使用原有配置不变,此时hive就有两个服务端了)

<configuration>
<!--通过thrift方式连接hive-->
<property>
<name>hive.metastore.uris</name>
<value>thrift://hive_server_ip:9083</value>
</property>
</configuration>

简单测试:
执行hive命令会进入命令界面:

[root@test3 apache-hive-1.2.-bin]# hive

Logging initialized using configuration in jar:file:/data/yunva/apache-hive-1.2.-bin/lib/hive-common-1.2..jar!/hive-log4j.properties
hive> show databases;
OK
default
Time taken: 1.158 seconds, Fetched: row(s) hive> create database yunvatest;
hive> use yunvatest;
OK
Time taken: 0.021 seconds
hive> show databases;
OK
default
yunvatest
Time taken: 0.225 seconds, Fetched: row(s)
hive> create table table_test(id string,name string);
OK
Time taken: 0.417 seconds
hive> show tables;
OK
table_test
Time taken: 0.033 seconds, Fetched: row(s)

6.Hive常用SQL命令
6.1先创建一个测试库

hive> create database test;
hive> use test;

创建tb1表,并指定字段分隔符为tab键(否则会插入NULL)

hive> create table tb1(id int,name string) row format delimited fields terminated by '\t';

如果想再创建一个表,而且表结构和tb1一样,可以这样:
hive> create table table2 like tb1;

查看下表结构:
hive> describe table2;
OK
id int
name string
Time taken: 0.126 seconds, Fetched: 2 row(s)

6.2从本地文件中导入数据到Hive表
先创建数据文件,键值要以tab键空格:

# cat seasons.txt
spring
summer
autumn
winter

再导入数据:
hive> load data local inpath'/root/seasons.txt' overwrite into table tb1;
查询是否导入成功

hive> select * from tb1;
OK
spring
summer
autumn
winter

6.3从HDFS中导入数据到Hive表:

列出hdfs文件系统根目录下的目录
hadoop fs -ls /

创建test根目录
hadoop fs -mkdir /test
put 命令向/test目录写入文件为siji.txt
hadoop fs -put /root/seasons.txt /test/siji.txt

查看siji.txt文件内容

# hadoop fs -cat /test/siji.txt
// :: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
spring
summer
autumn
winte

hive> load data inpath '/test/siji.txt' overwrite into table table2;
Loading data to table test.table2
Table test.table2 stats: [numFiles=1, numRows=0, totalSize=36, rawDataSize=0]
OK
Time taken: 0.336 seconds

查询是否导入成功

hive> select * from table2;
OK
spring
summer
autumn
winter
Time taken: 0.074 seconds, Fetched: row(s)

6.4上面是基本表的简单操作,为了提高处理性能,Hive引入了分区机制,那我们就了解分区表概念:

1>.分区表是在创建表时指定的分区空间
2>.一个表可以有一个或多个分区,意思把数据划分成块
3>.分区以字段的形式在表结构中,不存放实际数据内容
分区表优点:将表中数据根据条件分配到不同的分区中,缩小查询范围,提高检索速度和处理性能

6.5单分区表:
创建单分区表tb2(HDFS表目录下只有一级目录):
hive> create table tb2(id int,name string) partitioned by (dt string) row format delimited fields terminated by '\t';

注:dt可以理解为分区名称。

从文件中把数据导入到Hive分区表,并定义分区信息(需要已经存在的表)

hive> load data local inpath '/root/seasons.txt' into table tb2 partition (dt='2017-12-06');
hive> load data local inpath '/root/seasons.txt' into table tb2 partition (dt='2017-12-07');

查看表数据

hive> select * from tb2;
OK
spring --
summer --
autumn --
winter --
spring --
summer --
autumn --
winter --
Time taken: 0.086 seconds, Fetched: row(s)

查看HDFS仓库中表目录变化

[root@test4_haili_dev ~]# hadoop fs -ls -R /user/hive/warehouse/test.db/tb2
// :: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
drwxrwxrwx - root supergroup -- : /user/hive/warehouse/test.db/tb2/dt=--
-rwxrwxrwx root supergroup -- : /user/hive/warehouse/test.db/tb2/dt=--/seasons.txt
drwxrwxrwx - root supergroup -- : /user/hive/warehouse/test.db/tb2/dt=--
-rwxrwxrwx root supergroup -- : /user/hive/warehouse/test.db/tb2/dt=--/seasons.txt

可以看到tb2表导入的数据根据日期将数据划分到不同目录下

6.6多分区表:
创建多分区表tb3(HDFS表目录下有一级目录,一级目录下再有子级目录)

hive> create table table3(id int,name string) partitioned by (dt string,location string) row format delimited fields terminated by '\t';

从文件中把数据导入到Hive分区表,并定义分区信息

hive> load data local inpath '/root/seasons.txt' into table table3 partition (dt='2017-12-06',location='guangzhou');
hive> load data local inpath '/root/seasons.txt' into table table3 partition (dt='2017-12-07',location='shenzhen');

查看表数据

hive> select * from table3;
OK
spring -- guangzhou
summer -- guangzhou
autumn -- guangzhou
winter -- guangzhou
spring -- shenzhen
summer -- shenzhen
autumn -- shenzhen
winter -- shenzhen

查看HDFS仓库中表目录变化

[root@test3 yunva]# hadoop fs -ls -R /user/hive/warehouse/test.db/table3
// :: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
drwxrwxrwx - root supergroup -- : /user/hive/warehouse/test.db/table3/dt=--
drwxrwxrwx - root supergroup -- : /user/hive/warehouse/test.db/table3/dt=--/location=guangzhou
-rwxrwxrwx root supergroup -- : /user/hive/warehouse/test.db/table3/dt=--/location=guangzhou/seasons.txt
drwxrwxrwx - root supergroup -- : /user/hive/warehouse/test.db/table3/dt=--
drwxrwxrwx - root supergroup -- : /user/hive/warehouse/test.db/table3/dt=--/location=shenzhen
-rwxrwxrwx root supergroup -- : /user/hive/warehouse/test.db/table3/dt=--/location=shenzhen/seasons.txt

可以看到表中一级dt分区目录下又分成了location分区。

查看表分区信息
hive> show partitions table3;
OK
dt=2017-12-06/location=guangzhou
dt=2017-12-07/location=shenzhen
Time taken: 0.073 seconds, Fetched: 2 row(s)

根据分区查询数据

hive> select name from table3 where dt='2017-12-06';
OK
spring
summer
autumn
winter
Time taken: 0.312 seconds, Fetched: 4 row(s)

重命名分区
hive> alter table table3 partition (dt='2017-12-06',location='guangzhou') rename to partition(dt='20171206',location='shanghai');

删除分区
hive> alter table table3 drop partition(dt='2017-12-06',location='guangzhou');
OK
Time taken: 0.113 seconds
可以看到已经查不出来了
hive> select name from table3 where dt='2017-12-06';
OK
Time taken: 0.078 seconds

模糊搜索表
hive> show tables 'tb*';
OK
tb1
tb2

给表新添加一列

hive> alter table tb1 add columns (comment string);
OK
Time taken: 0.106 seconds
hive> describe tb1;
OK
id int
name string
comment string
Time taken: 0.079 seconds, Fetched: row(s)

重命名表
hive> alter table tb1 rename to new_tb1;
OK
Time taken: 0.095 seconds
hive> show tables;
OK
new_tb1
table2
table3
tb2

删除表
hive> drop table new_tb1;
OK
Time taken: 0.094 seconds
hive> show tables;
OK
table2
table3
tb2

基于Hadoop2.7.3集群数据仓库Hive1.2.2的部署及使用的更多相关文章

  1. Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作

    前言 安装Apache Hive前提是要先安装hadoop集群,并且hive只需要在hadoop的namenode节点集群里安装即可,安装前需保证Hadoop已启(动文中用到了hadoop的hdfs命 ...

  2. hive1.2.1安装步骤(在hadoop2.6.4集群上)

    hive1.2.1在hadoop2.6.4集群上的安装 hive只需在一个节点上安装即可,这里再hadoop1上安装 1.上传hive安装包到/usr/local/目录下 2.解压 tar -zxvf ...

  3. 分布式Hbase-0.98.4在Hadoop-2.2.0集群上的部署

    fesh个人实践,欢迎经验交流!本文Blog地址:http://www.cnblogs.com/fesh/p/3898991.html Hbase 是Apache Hadoop的数据库,能够对大数据提 ...

  4. 基于centos6.5 hbase 集群搭建

    注意本章内容是在上一篇文章“基于centos6.5 hadoop 集群搭建”基础上创建的 1.上传hbase安装包 hbase-0.96.2-hadoop2 我的目录存放在/usr/hadoop/hb ...

  5. Hadoop2.X分布式集群部署

    本博文集群搭建没有实现Hadoop HA,详细文档在后续给出,本次只是先给出大概逻辑思路. (一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 基于A ...

  6. Ubuntu下用hadoop2.4搭建集群(伪分布式)

    要真正的学习hadoop,就必需要使用集群,可是对于普通开发人员来说,没有大规模的集群用来測试,所以仅仅能使用伪分布式了.以下介绍怎样搭建一个伪分布式集群. 为了节省时间和篇幅,前面一些步骤不再叙述. ...

  7. 新闻实时分析系统-Hadoop2.X分布式集群部署

    (一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 1.基于Apache厂商的最原始的hadoop版本, 所有发行版均基于这个版本进行改进. 2.基于 ...

  8. 新闻网大数据实时分析可视化系统项目——3、Hadoop2.X分布式集群部署

    (一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 1.基于Apache厂商的最原始的hadoop版本, 所有发行版均基于这个版本进行改进. 2.基于 ...

  9. Tomcat:基于Apache+Tomcat的集群搭建

    根据Tomcat的官方文档说明可以知道,使用Tomcat配置集群需要与其它Web Server配合使用才可以完成,典型的有Apache和IIS. 这里就使用Apache+Tomcat方式来完成基于To ...

随机推荐

  1. Linux命令(十四)gdb调试

    1. gdb调试 fun.c #include <stdio.h> #include "head.h" int sum(int a, int b) { printf(& ...

  2. Openresty 学习笔记(一)opm 工具的使用

    1.自1.11.2.2开始,OpenResty版本已经包含并默认安装opm.所以通常你不需要自己安装opm. 2.我们在这里只需要做一个软连接就可以了 cd /usr/local/openresty/ ...

  3. MVC实用架构设计(三)——EF-Code First(1):Repository,UnitOfWork,DbContext

    前言 终于到EF了,实在不好意思,最近有点忙,本篇离上一篇发布已经一个多星期了,工作中的小迭代告一段落,终于有点时间来继续我们的架构设计了,在这里先对大家表示歉意. 其实这段时间我并不是把这个系列给忘 ...

  4. 使用wget命令下载JDK失败(文件特别小)

    问题RT: 我们在网页上下载的时候要点一下 “Accept License Agreement ” ,使用wget下载的时候也需要提交这个 accept,方法如下: wget --no-check-c ...

  5. Python获取下载速度并显示进度条

    #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys import time from urllib import request ''' urll ...

  6. 认证加密算法php hash_hmac和java hmacSha1的问题

    public class Test{ public static void main(String[] args) throws Exception { String postString = &qu ...

  7. loj 10181 绿色通道 二分答案+单调队列DP

    空题段长度即为单调队列长度区间 每次二分答案进行check即可 #include<bits/stdc++.h> using namespace std; ; const int inf=0 ...

  8. [C++]PAT乙级1009. 说反话 (17/20)

    /* 1009. 说反话 (20) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例, 在一行内给出总长度不超过80的字符串. 字符串由若干单词和若干 ...

  9. DAO层设计

    一.类图分析 二.参考文档 ( JavaBean中DAO设计模式介绍)(附:设计源码) 三.类图设计文件 百度云盘:https://pan.baidu.com/s/1i5xaS8P[Power Des ...

  10. 微信小程序 后端用Flask实现

    手上有个微信小程序项目,因为对Python相对熟悉一些,打算后端用python写,具体采用python 轻量级的flask框架. 在做的过程中,有些问题需要考虑,记录在下边. 1. 开发的小程序后端怎 ...