一、基本概念

The Apache Hive™ data warehouse software facilitates reading, writing, and managing large datasets residing in distributed storage and queried using SQL syntax.

Hive数据仓库软件,致力于解决读写、管理分布式存储中的大规模数据集,以及使用SQL语法进行查询的问题。

Hive用于解决海量结构化日志的数据统计问题。

Hive是基于Hadoop的一个数据仓库工具。本质是将HQL(Hive的查询语言)转化成MapReduce程序。

HIve处理的数据存储在HDFS

HIve分析数据底层的默认实现是MapReduce

执行程序运行在Yarn上

Hive的优缺点

优点:

可以快速进行数据分析,不需要写MapReduce程序。

MapReduce适合处理大数据,不适合处理小数据

缺点:

HQL表达能力有限,迭代式算法不能表达,粒度较粗,调优比较困难。

自定义函数类别:

  • UDF
  • UDAF
  • UDTF

架构原理

执行顺序:解析器-编译器-优化器-执行器

Hive与数据库对比

HIve相比数据库,读多写少,没有索引,需要暴力扫描所有数据,即使引入了MapReduce机制,也不适合实时查询,扩展性和Hadoop的是一致的,扩展性强。

二、安装与启动

下载hive-1.2.2

需要启动Hadoop的HDFS和Yarn

配置conf/hive-env.sh

export HADOOP_HOME=/usr/local/hadoop(改成hadoop-home路径)
export HIVE_CONF_DIR=/ur/local/hive/conf

启动

bin/hive

三、Hive语句

显示数据库

show databases;

使用本地模式执行

hive> SET mapreduce.framework.name=local;

创建表、插入记录、查询记录

use default;

#### 创建表
create table student(id int,name string); #### 插入记录
insert into table student values(1,'fonxian'); #### 查询记录
select * from student;

在Hadoop上查看记录

从文件系统加载数据

创建数据文本student.txt

3,kafka
4,flume
5,hbase
6,zookeeper

创建表,定义分隔符

create table stu1(id int,name string) row format delimited fields terminated by ',';

加载数据

load data local inpath '/usr/local/hive/data/student.txt' into table stu1;

查看数据后的执行效果

四、Hive Hook使用

添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>hive-hook-example</groupId>
<artifactId>Hive-hook-example</artifactId>
<version>1.0</version> <dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies> </project>

创建HiveExampleHook

public class HiveExampleHook implements ExecuteWithHookContext {
public void run(HookContext hookContext) throws Exception {
System.out.println("operation name :" + hookContext.getQueryPlan().getOperationName());
System.out.println(hookContext.getQueryPlan().getQueryPlan());
System.out.println("Hello from the hook !!");
}
}

编译好,获得Hive-hook-example-1.0.jar

hive> add jar Hive-hook-example-1.0.jar

hive> set hive.exec.pre.hooks=HiveExampleHook;

hive> select * from student;

operation name :QUERY
Query(queryId:fangzhijie_20191221231550_0e949bbf-f8f7-45a8-8726-c1cdd679cef9, queryType:null, queryAttributes:{queryString=select * from student}, queryCounters:null, stageGraph:Graph(nodeType:STAGE, roots:null, adjacencyList:null), stageList:null, done:false, started:true)
Hello from the hook !!
OK
Time taken: 1.718 seconds
Time taken: 1.68 seconds

五、使用MySQL存储元数据

在本地安装mysql,创建hive-site.xml

<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://127.0.0.1:3306/metastore?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>root</value>
<description>username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
<description>password to use against metastore database</description>
</property>
</configuration>

执行bin/hive,查看数据库,发现有创建表。

在hive中执行reate table aaa(id int);,HDFS中有创建该文件,且metastore的TBLS表中有记录。

六、Beeline

HiveServer2 (introduced in Hive 0.11) has its own CLI called Beeline. HiveCLI is now deprecated in favor of Beeline, as it lacks the multi-user, security, and other capabilities of HiveServer2. To run HiveServer2 and Beeline from shell:

HiveServer2有自己的客户端,叫Beeline。HiveCLI目前已经废弃了,建议使用Beeline。

使用Beeline连接HiveServer2

beeline -u "jdbc:hive2://host:port/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -n username -p password

七、报错信息解决&问题定位

修改配置不生效

可能是配置路径的问题,查看hive-env.sh,最后发现hive配置路径写错。

错误的路径配置,导致根本找不到配置路径

export HIVE_CONF_DIR=/ur/local/hive/conf

正确的配置

export HIVE_CONF_DIR=/usr/local/hive/conf

插入数据失败

hive> insert into table student values(1,'fonxian');
Query ID = fangzhijie_20191205061055_6c8c233e-2d46-470a-972d-38f36bb8068c
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1575495654045_0004, Tracking URL = http://localhost:8088/proxy/application_1575495654045_0004/
Kill Command = /usr/local/hadoop/bin/hadoop job -kill job_1575495654045_0004
Hadoop job information for Stage-1: number of mappers: 0; number of reducers: 0
2019-12-05 06:10:58,803 Stage-1 map = 0%, reduce = 0%
Ended Job = job_1575495654045_0004 with errors
Error during job, obtaining debugging information...
FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
MapReduce Jobs Launched:
Stage-Stage-1: HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec

解决方法:执行下面的命令

hive> SET mapreduce.framework.name=local;

分析:

参考官方文档

Hive compiler generates map-reduce jobs for most queries. These jobs are then submitted to the Map-Reduce cluster indicated by the variable:  mapred.job.tracker

Hive编译器 为大多数查询操作生成MR任务,这些任务之后会被提交到MR集群。

Hive fully supports local mode execution. To enable this, the user can enable the following option:

Hive支持本地模式执行,用户可以使用下列操作:

hive> SET mapreduce.framework.name=local;

参考文档

Hive Getting Started

尚硅谷大数据课程之Hive

hive-hook-example

Beeline 官方文档

Hive(一)—— 启动与基本使用的更多相关文章

  1. hive cli 启动缓慢问题

    hive-0.13.1启动缓慢的原因 发现时间主要消耗在以下3个地方: 1. hadoopjar的时候要把相关的jar包上传到hdfs中(这里大概消耗5s,hive0.11一样,这个地方不太好优化) ...

  2. 单节点伪分布集群(weekend110)的Hive子项目启动顺序

    因为,我的mysql是用root用户,在/home/hadoop/app/目录下,创建的. 第一步:开启mysql服务 第二步:启动hive [hadoop@weekend110 app]$ su r ...

  3. ambari下 hive metastore 启动失败

    由字符集引起的hive 元数据进程启动失败 解决方法新增 这2句话 reload(sys)sys.setdefaultencoding('utf8')

  4. CDH hive metastore启动报错:Unknown column 'A0.SCHEMA_VERSION_V2' in 'field list'

    新集群CDH版本,刚刚搭建起来,5个节点起了1个hive服务,另外5个节点又单独起了1个hive服务,一共2个人hive服务.老哥对其中的一个hive进行了数据迁移,对hive数据库进行了替换,就这样 ...

  5. Hive元数据启动失败,端口被占用

    org.apache.thrift.transport.TTransportException: Could not create ServerSocket on address 0.0.0.0/0. ...

  6. Hive元数据启动失败

    Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocke ...

  7. hive安装启动错误总结

    错误一: Exception in thread "main" java.lang.NoClassDefFoundError: jline/console/completer/Ar ...

  8. Hive启动时的棘手问题的处理

    Hive是存在于Hadoop集群之上的数据仓库,作为大数据处理时的主要工具,对于大数据开发人员的重要性不言而喻.当然要使用Hive仓库的前提就是对于hive的安装,hive的安装是很简单的过程,主要关 ...

  9. Hive 学习笔记(启动方式,内置服务)

    一.Hive介绍 Hive是基于Hadoop的一个数据仓库,Hive能够将SQL语句转化为MapReduce任务进行运行. Hive架构图分为以下四部分. 1.用户接口 Hive有三个用户接口: 命令 ...

随机推荐

  1. 【转载】C#中List集合使用AddRange方法将一个集合加入到指定集合末尾

    C#编程开发过程中,List集合是时常使用到的集合对象,如果在List集合的操作中需要将1个List集合加入到另一个List集合的末尾,则可以使用List集合的AddRange方法来实现,AddRan ...

  2. Django:RestFramework之-------渲染器

    12.渲染器 from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer,AdminRenderer class Te ...

  3. Django:基于调试组插件go-debug-toolbar

    1.django-debug-toolbar 介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息.返回 ...

  4. 从linux进程角度看JVM内存模型

    普通进程栈区,在JVM一般仅仅用做线程栈,如下图所示 首先是永久代.永久代本质上是Java程序的代码区和数据区.Java程序中类(class),会被加载到整个区域的不同数据结构中去,包括常量池.域.方 ...

  5. vmware下ubuntu虚拟机如何安装vmware tools

      依次点击-->虚拟机-->安装VMware Tools   窗口下方会弹出安装提示   系统会加载安装驱动至光盘 双击--VMwareTools-****.tar.gz--的压缩包   ...

  6. MySQL/MariaDB数据库的各种日志管理

    MySQL/MariaDB数据库的各种日志管理 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.事务日志 (transaction log) 1>.Innodb事务日志相 ...

  7. Python入门篇-文件操作

    Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...

  8. mysql 增加列

    alter table t_zhaosheng_chengji_is_finish add season_id int(4) default '1' not null COMMENT '招生季ID';

  9. YES, There is No such thing as a free lunch

    软件行业本身就建立在copy的基础上的,据说视窗both Windows and Mac OS都借鉴了施乐的. 国内的很多的软件质量真的好差呀. https://queue.acm.org/detai ...

  10. 补充拓展:CSS权重值叠加

    都知道CSS选择器有权重优先级,权重大的优先展示. 但部分人可能不清楚,权重值也是可以叠加计算的 <!DOCTYPE html> <html> <head> < ...