大数据(Hive-搭建和基本使用)

北城以北 提交于 2019-12-02 21:11:13

Hive背景及应用场景

Hive是什么?

由facebook开源,最初用于解决海量结构化的日志数据统计问题;
   ETL (Extraction-Transformation-Loading )工具

构建在Hadoop之上的数据仓库;
   数据计算使用MR ,数据存储使用HDFS

Hive 定义了一种类 SQL 查询语言——HQL;
   类似SQL , 但不完全相同

通常用于进行离线数据处理(采用MapReduce);

可认为是一个HQL MR的语言翻译器。

Hive典型应用场景

日志分析
   统计网站一个时间段内的pv、uv
   多维度数据分析
   大部分互联网公司使用Hive进行日志分析,包括百度、淘宝等

其他场景
   海量结构化数据离线分析
   低成本进行数据分析(不直接编写MR)

为什么使用Hive?

简单、容易上手
   提供了类SQL 查询语言HQL ;

为超大数据集设计的计算/扩展能力
   MR 作为计算引擎,HDFS

Hive基本架构

Hive各模块组成

用户接口
   包括 CLI ,JDBC/ODBC ,WebUI

元数据存储(metastore)
   默认存储在自带的数据库derby 中,线上使用时一般换为MySQL

驱动器(Driver)
   解释器、编译器、优化器、执行器

Hadoop
   用 MapReduce进行计算,用HDFS进行存储

Hive部署架构-实验环境

数据类型(不断增加中……)

数据定义语句(DDL)

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
(col_name data_type, ...)
[PARTITIONED BY (col_name data_type, ...)]
[CLUSTERED BY (col_name, col_name, ...) [SORTED BY
(col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[SKEWED BY (col_name, col_name, ...)]
[ [ROW FORMAT row_format] [STORED AS file_format] ]
[LOCATION hdfs_path]

 

搭建步骤

1:下载地址
http://archive.apache.org/dist/hive

2:解压
3:配置hive的环境变量
    在当前用户的.bashrc中配置如下内容
    export HIVE_HOME=/home/hadoop/bd/apache-hive-2.1.0-bin

4:配置hive安装目录下的conf目录下的hive-env.sh文件
    该文件可以通过复制hive-env.sh.template改名得来
    配置内容如下:
    # Set HADOOP_HOME to point to a specific hadoop install directory
     HADOOP_HOME=/home/hadoop/bd/hadoop-2.7.3

    # Hive Configuration Directory can be controlled by:
     export HIVE_CONF_DIR=/home/hadoop/bd/apache-hive-2.1.0-bin/conf

    # Folder containing extra ibraries required for hive compilation/execution can be controlled by:
     export HIVE_AUX_JARS_PATH=/home/hadoop/bd/apache-hive-2.1.0-bin/lib

5:修改hive的日志文件存放的地址
    cp hive-log4j2.properties.template hive-log4j2.properties
    通过vi修改日志的存放文件
    property.hive.log.dir = /home/hadoop/bd/apache-hive-2.1.0-bin/logs

6:启动hadoop集群

7:安装默认的derby数据库为hive的元数据库
    可以先通过./schematool --help 命令来查看schematool命令的一些选项
     ./schematool -dbType derby -initSchema,使用这个命令来安装derby数据库为元数据

8:执行bin目录下的hive命令,进入hive命令行
    ./hive

如果没有问题的话,hive就安装成功了


1:创建表
    create table 表名
    指定分隔符创建表:create table teacher (id int, name string) row format delimited fields terminated by '\t';

二:更改元数据库为mysql

1:复制文件hive-default.xml.template更名为hive-site.xml
    cp hive-default.xml.template hive-site.xml

2:清空hive-site.xml里面的配置信息
    添加我们自定义的信息

    <configuration>
     <property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://hm02:3306/hive?createDatabaseIfNotExist=true</value>
    </property>
    <property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>root</value>
    </property>
    <property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>123</value>
    </property>
  </configuration>

3:拷贝mysql驱动jar包到hive安装目录下的lib目录

4:mysql授权以及实例化metastore
    1)如果之前对该主机和用户进行了授权,那么可以不用再次授权,否则进行授权,参考sqoop那章
    (grant all privileges on *.* to root@'主机名' identified by '密码')
    前提是use mysql这个库。

    2)实例化metastore命令:
    ./schematool -dbType mysql -initSchema

5:关于mysql数据库作为元数据库的几点说明
    1)hive当中创建的表的信息,在元数据库的TBLS表里面
    2)这个表的字段信息,在元数据库的COLUMNS_V2表里面
    3)这个表在HDFS上面的位置信息,在元数据库的SDS表里面

hive-site.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
--><configuration>
 	<property>
		<name>javax.jdo.option.ConnectionURL</name>
		<value>jdbc:mysql://hm02:3306/hive?createDatabaseIfNotExist=true</value>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionDriverName</name>
		<value>com.mysql.jdbc.Driver</value>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionUserName</name>
		<value>root</value>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionPassword</name>
		<value>123</value>
	</property>
  </configuration>

hive-site-back.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
--><configuration>
	<property>
		<name>javax.jdo.option.ConnectionURL</name>
		<value>jdbc:mysql://hm:3306/hive?createDatabaseIfNotExist=true</value>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionDriverName</name>
		<value>com.mysql.jdbc.Driver</value>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionUserName</name>
		<value>root</value>
	</property>
	<property>
		<name>javax.jdo.option.ConnectionPassword</name>
		<value>123</value>
	</property>
  </configuration>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!