个人对hive的简单理解?

帮朋友打个广告 [二手空调维修拆装回收] 等一系列服务有需要的大哥们
请咨询
13636585819或者扫描下方的二维码

什么是hive?

1、hive是基于hadoop的一个数据仓库工具
2、可以将结构化的数据文件映射成一张数据库表,并提供类sql的查询功能
3、可以将sql语句转化成map任务(maptask)进行运行
4、可以用来数据提取、转化、加载(ETL)
5、hive是sql解析引擎,他将sql语句转化成M\R job在hadoop上运行
hive的表中的数据实际上就是hdfs中的目录文件
6、hive的优点和缺点:
可以提供类sql语句快速实现简单的mapreduce统计
不支持实时查询(效率低,只支持离线下对数据进行操作)
7、hive中的数据分为真实数据和元数据 (真实数据存储在hdfs中,元数据存储在与hive连接的数据库中

安装部署

基础依赖环境:

1,jdk 1.6+
2, hadoop 2.x
3,hive 0.13-0.19
4,mysql (mysql-connector-jar)

安装详细如下:

#java

export JAVA_HOME=/soft/jdk1.7.0_79/
export CLASSPATH=.:​JAVA_HOME/lib/tools.jar

#bin
export PATH=​JAVA_HOME/bin:​SCALA_HOME/bin:$SPARK_HOME/bin:/usr/local/hadoop/hive/bin

#hadoop
export HADOOP_HOME=/usr/local/hadoop/hadoop

#scala
export SCALA_HOME=/usr/local/hadoop/scala

#spark
export SPARK_HOME=/usr/local/hadoop/spark

#hive
export HIVE_HOME=/usr/local/hadoop/hive

一、开始安装:

1,下载:https://hive.apache.org/downloads.html

解压:

tar xvf apache-hive-2.1.0-bin.tar.gz -C /usr/local/hadoop/
cd /usr/local/hadoop/
mv apache-hive-2.1.0 hive

2,修改配置
修改启动环境
cd /usr/local/hadoop/hive
vim bin/hive-config.sh

#java


export JAVA_HOME=/soft/jdk1.7.0_79/

#hadoop


export HADOOP_HOME=/usr/local/hadoop/hadoop

#hive


export HIVE_HOME=/usr/local/hadoop/hive

修改默认配置文件

cd /usr/local/hadoop/hive
vim conf/hive-site.xml

3,修改tmp dir

修改将含有"system:java.io.tmpdir"的配置项的值修改为如上地址

/tmp/hive

4,安装mysql driver

去mysql官网下载驱动mysql-connector-java-5.1.40.zip

unzip mysql-connector-java-5.1.40.zip

cp mysql-connector-java-5.1.40-bin.jar /user/lcoal/hadoop/hive/lib/

二、安装好mysql,并且启动

1.创建数据库

create database hive

grant all on . to hive@’%’ identified by ‘hive’;

flush privileges;

三,初始化hive(初始化metadata)

cd /usr/local/hadoop/hive

bin/schematool -initSchema -dbType mysql

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Metastore connection URL: jdbc:mysql://hadoop3:3306/hive?createDatabaseInfoNotExist=true

Metastore Connection Driver : com.mysql.jdbc.Driver

Metastore connection User: hive

Starting metastore schema initialization to 2.1.0

Initialization script hive-schema-2.1.0.mysql.sql

Initialization script completed

schemaTool completed
四、启动

[hadoop@hadoop1 hadoop]$** hive/bin/hive**

which: no hbase in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin://soft/jdk1.7.0_79//bin:/bin:/bin:/bin:/usr/local/hadoop/hive/bin:/home/hadoop/bin)

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/usr/local/hadoop/hive/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/usr/local/hadoop/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in jar:file:/usr/local/hadoop/hive/lib/hive-common-2.1.0.jar!/hive-log4j2.properties Async: true

Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. tez, spark) or using Hive 1.X releases.

hive> show databases;

OK

default

Time taken: 1.184 seconds, Fetched: 1 row(s)

hive>
五,实践操作

使用hive创建表

以下两个操作只是针对当前session终端

1,hive> set hive.cli.print.current.db=true; 设置显示当前数据库名

hive (default)>

2,hive (default)> set hive.cli.print.header=true; 当使用select 查询数据时候,显示的结果会带有表的字段名称

3,创建表,并导入数据

hive> create table teacherq(id bigint,name string) row format delimited fields terminated by ‘\t’;

OK

hive> create table people (id int ,name string);

OK

Time taken: 3.363 seconds

hive> SHOW TABLES;

OK

people

teacherq

student

Time taken: 0.283 seconds, Fetched: 1 row(s)

导入数据:

hive>load data local inpath ‘/root/stdent.txt’ into table teacherq;
注意:如果你是普通用户启动hive,则使用相对路径来导入本地数据

mv stdent.txt /usr/local/hadoop/hive/

cd /usr/local/hadoop/hive

load data local inpath ‘stdent.txt’ into table teacherq;

Loading data to table default.teacherq

OK

Time taken: 2.631 seconds

hive> select * from teacherq;

OK

1 zhangsan

2 lisi

3 wangwu

4 libai

Time taken: 1.219 seconds, Fetched: 4 row(s)

hive>

4.建表(默认是内部表)

适用于先创建表,后load加载数据、

create table trade_detail(id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by ‘\t’;

默认普通表load数据:

load data local inpath ‘/root/student.txt’ into table student;

建外部表 : 适用于,hdfs先有数据,后创建表,进行数据查询,分析管理

create external table td_ext(id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by ‘\t’** location **’/td_ext’;

外部表load数据:

load data local inpath ‘/root/student.txt’ into table student;

建分区表

方法一:先创建分区表,然后load数据

​ partition就是辅助查询,缩小查询范围,加快数据的检索速度和对数据按照一定的规格和条件进行管理。

​ create table td_part(id bigint, account string, income double, expenses double, time string) partitioned by (logdate string) row format delimited fields terminated by ‘\t’;

分区表中load数据

load data local inpath ‘/root/data.am’ into table beauty partition (nation=“USA”);

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

Time taken: 0.22 seconds, Fetched: 4 row(s)

方法二:先在hdfs 创建目录,倒入数据,最后,更改hive元数据的信息

1, 创建分区目录

hive (itcast)> dfs -mkdir /beat/nation=japan

dfs -ls /beat;

Found 2 items

drwxr-xr-x - hadoop supergroup 0 2016-12-05 16:07 /beat/nation=china

drwxr-xr-x - hadoop supergroup 0 2016-12-05 16:16 /beat/nation=japan

2, 为分区目录加载数据

hive (itcast)> dfs -put d.c /beat/nation=japan

​ 此时查询数据:数据还未加载进来。

hive (itcast)> dfs -ls /beat/nation=japan;

Found 1 items

-rw-r–r-- 3 hadoop supergroup 20 2016-12-05 16:16 /beat/nation=japan/d.c

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

Time taken: 0.198 seconds, Fetched: 4 row(s)

3,手动修改hive表结构,添加分区表信息

hive (itcast)> alter table beat add partition (nation=‘japan’) location “/beat/nation=japan”;

OK

Time taken: 0.089 seconds

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

7ab111.0japan

8rb23234.0japan

Time taken: 0.228 seconds, Fetched: 6 row(s)

此时数据加载完成。

删除分区

用户可以用 ALTER TABLE DROP PARTITION 来删除分区。分区的元数据和数据将被一并删除。

例:

ALTER TABLE beat DROP PARTITION (nation=‘japan’);

特殊情况案例:

1,表中的某个字段需要作为分区的分区名,默认不允许创建,解决方法:

hive (itcast)> create table sms(id bigint ,content string,area string) partitioned by (area string) row format delimited fields terminated by ‘\t’ ;

FAILED: SemanticException [Error 10035]: Column repeated in partitioning columns

解决方法:

建立冗余字段,即使用 area_pat来区分,

或者修改源码

hive (itcast)> create table sms(id bigint ,content string,area string) partitioned by (area_pat string) row format delimited fields terminated by ‘\t’ ;

基本的配置包括环境变量加入到etc/profile文件中

export HIVE_HOME=/home/wangpeng/opt/hive

在将HIVE_HOME加载到PATH变量中去。

2.hive-site.xml这个文件中基本的url,driver,username,password配置号

3.驱动加载好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值