Hive 基本语法操练(二):视图和索引操作
1. 视图操作
------- 1) 创建一个测试表。 ```
hive> create table test(id int,name string);
OK
Time taken: 0.385 seconds
hive> desc test;
OK
id int
name string
Time taken: 0.261 seconds, Fetched: 2 row(s)
``` 2) 基于表 test 创建一个 test_view 视图。 ```
hive> create view test_view(id,name_length) as select id,length(name) from test;
``` 3) 查看 test_view 视图属性。 ```
hive> desc test_view;
OK
id int
name_length int
Time taken: 0.071 seconds, Fetched: 2 row(s) ``` 4) 查看视图结果。 ```
hive> select * from test_view;
``` 2. 索引操作
-------
1) Hive 创建索引。 ```
hive> create table user like group_test;
OK
Time taken: 0.232 seconds
hive> create index user_index on table user(uid) as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild IN TABLE user_index_table;
OK
Time taken: 0.183 seconds ``` 2) 更新数据。 ```
hive> alter index user_index on user rebuild;
Query ID = hadoop_20180518043232_ebdf97bd-5984-4310-a3c8-6befef328133
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapreduce.job.reduces=<number>
Starting Job = job_1526553207632_0018, Tracking URL = http://master:8088/proxy/application_1526553207632_0018/
Kill Command = /opt/modules/hadoop-2.6.0/bin/hadoop job -kill job_1526553207632_0018
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2018-05-18 04:32:55,632 Stage-1 map = 0%, reduce = 0%
2018-05-18 04:33:04,400 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.65 sec
2018-05-18 04:33:12,406 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 2.93 sec
MapReduce Total cumulative CPU time: 2 seconds 930 msec
Ended Job = job_1526553207632_0018
Loading data to table default.user_index_table
Table default.user_index_table stats: [numFiles=1, numRows=0, totalSize=0, rawDataSize=0]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 2.93 sec HDFS Read: 290 HDFS Write: 50 SUCCESS
Total MapReduce CPU Time Spent: 2 seconds 930 msec
OK
Time taken: 25.609 seconds ```
3) 查看索引
```
hive> show index on user;
OK
user_index user uid user_index_table compact
Time taken: 0.046 seconds, Fetched: 1 row(s) ``` 4) 删除索引 ```
hive> drop index user_index on user; OK
Time taken: 0.094 seconds
hive> show index on user;
OK
Time taken: 0.036 seconds ``` 5) 创建表和索引案例 ```
hive> create table index_test(id INT,name STRING) PARTITIONED BY (dt STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
``` 创建一个索引测试表 index_test,dt作为分区属性,“ROW FORMAT DELIMITED FIELDS TERMINATED BY ','” 表示用逗号分割字符串。 6) 创建一个临时索引表 index_tmp。 ```
hive> create table index_tmp(id INT,name STRING,dt STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
``` 7) 加载本地数据到 index_tmp 表中。 ```
[hadoop@master test]$ sudo vim test.txt
02,female,192.168.1.3
01,male,192.168.1.26
03,male,192.168.1.5
08,female,192.168.1.62
04,male,192.168.1.9
hive> load data local inpath '/home/hadoop/test/test.txt' into table index_tmp;
Loading data to table default.index_tmp
Table default.index_tmp stats: [numFiles=1, totalSize=106]
OK
Time taken: 0.224 seconds
hive> select * from index_tmp;
OK
2 female 192.168.1.3
1 male 192.168.1.26
3 male 192.168.1.5
8 female 192.168.1.62
4 male 192.168.1.9 ``` 设置 Hive 的索引属性来优化索引查询,命令如下。 ```
hive> set hive.exec.dynamic.partition.mode=nonstrict;----设置所有列为 dynamic partition
hive> set hive.exec.dynamic.partition=true;----使用动态分区
``` 8) 查询index_tmp 表中的数据,插入 table_test 表中。 ```
hive> insert overwrite table index_test partition(dt) select id,name,dt from index_tmp;
Query ID = hadoop_20180518044343_97e7fe67-a5a1-408b-be8e-e9dadb2f9e48
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_1526553207632_0019, Tracking URL = http://master:8088/proxy/application_1526553207632_0019/
Kill Command = /opt/modules/hadoop-2.6.0/bin/hadoop job -kill job_1526553207632_0019
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2018-05-18 04:43:42,621 Stage-1 map = 0%, reduce = 0%
2018-05-18 04:43:48,835 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 0.87 sec
MapReduce Total cumulative CPU time: 870 msec
Ended Job = job_1526553207632_0019
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to: hdfs://ns/tmp/hive/hadoop/9f7dd0d3-a14c-4535-9291-557b9cb6259b/hive_2018-05-18_04-43-36_337_559705388802402645-1/-ext-10000
Loading data to table default.index_test partition (dt=null)
Time taken for load dynamic partitions : 278
Loading partition {dt=192.168.1.62}
Loading partition {dt=192.168.1.3}
Loading partition {dt=192.168.1.5}
Loading partition {dt=192.168.1.26}
Loading partition {dt=192.168.1.9}
Time taken for adding to write entity : 0
Partition default.index_test{dt=192.168.1.26} stats: [numFiles=1, numRows=1, totalSize=7, rawDataSize=6]
Partition default.index_test{dt=192.168.1.3} stats: [numFiles=1, numRows=1, totalSize=9, rawDataSize=8]
Partition default.index_test{dt=192.168.1.5} stats: [numFiles=1, numRows=1, totalSize=7, rawDataSize=6]
Partition default.index_test{dt=192.168.1.62} stats: [numFiles=1, numRows=1, totalSize=9, rawDataSize=8]
Partition default.index_test{dt=192.168.1.9} stats: [numFiles=1, numRows=1, totalSize=7, rawDataSize=6]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Cumulative CPU: 0.87 sec HDFS Read: 308 HDFS Write: 338 SUCCESS
Total MapReduce CPU Time Spent: 870 msec
OK
Time taken: 15.225 seconds ``` 9) 使用 index_test 表,在属性 id 上创建一个索引 index1_index_test 。 ```
hive> create index index1_index_test on table index_test(id) as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' WITH DEFERRED REBUILD;
OK
Time taken: 0.109 seconds ``` 10) 填充索引数据。 ```
hive> alter index index1_index_test on index_test rebuild;
Query ID = hadoop_20180518044545_edc98c3b-03eb-42c0-83d1-ba266d8497e0
Total jobs = 5
(省略MapReduce过程)
Ended Job = job_1526553207632_0024
Loading data to table default.default__index_test_index1_index_test__ partition (dt=192.168.1.26)
Loading data to table default.default__index_test_index1_index_test__ partition (dt=192.168.1.3)
Loading data to table default.default__index_test_index1_index_test__ partition (dt=192.168.1.5)
Loading data to table default.default__index_test_index1_index_test__ partition (dt=192.168.1.62)
Loading data to table default.default__index_test_index1_index_test__ partition (dt=192.168.1.9)
Partition default.default__index_test_index1_index_test__{dt=192.168.1.26} stats: [numFiles=1, numRows=0, totalSize=70, rawDataSize=0]
Partition default.default__index_test_index1_index_test__{dt=192.168.1.3} stats: [numFiles=1, numRows=0, totalSize=69, rawDataSize=0]
Partition default.default__index_test_index1_index_test__{dt=192.168.1.5} stats: [numFiles=1, numRows=0, totalSize=69, rawDataSize=0]
Partition default.default__index_test_index1_index_test__{dt=192.168.1.62} stats: [numFiles=1, numRows=0, totalSize=70, rawDataSize=0]
Partition default.default__index_test_index1_index_test__{dt=192.168.1.9} stats: [numFiles=1, numRows=0, totalSize=69, rawDataSize=0]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 2.66 sec HDFS Read: 226 HDFS Write: 191 SUCCESS
Stage-Stage-5: Map: 1 Reduce: 1 Cumulative CPU: 1.94 sec HDFS Read: 227 HDFS Write: 187 SUCCESS
Stage-Stage-9: Map: 1 Reduce: 1 Cumulative CPU: 2.15 sec HDFS Read: 225 HDFS Write: 187 SUCCESS
Stage-Stage-13: Map: 1 Reduce: 1 Cumulative CPU: 1.9 sec HDFS Read: 228 HDFS Write: 191 SUCCESS
Stage-Stage-17: Map: 1 Reduce: 1 Cumulative CPU: 2.73 sec HDFS Read: 225 HDFS Write: 187 SUCCESS
Total MapReduce CPU Time Spent: 11 seconds 380 msec
OK
Time taken: 196.607 seconds
``` 11) 查看创建的索引。 ```
hive> show index on index_test
``` 12) 查看分区信息。 ```
hive> show partitions index_test;
``` 13) 查看索引数据。 ```
[hadoop@master hadoop-2.6.0]$ hadoop fs -ls /user/hive/warehouse/default__index_test_index1_index_test__
18/05/18 04:52:21 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 5 items
drwxr-xr-x - hadoop supergroup 0 2018-05-18 04:46 /user/hive/warehouse/default__index_test_index1_index_test__/dt=192.168.1.26
drwxr-xr-x - hadoop supergroup 0 2018-05-18 04:46 /user/hive/warehouse/default__index_test_index1_index_test__/dt=192.168.1.3
drwxr-xr-x - hadoop supergroup 0 2018-05-18 04:46 /user/hive/warehouse/default__index_test_index1_index_test__/dt=192.168.1.5
drwxr-xr-x - hadoop supergroup 0 2018-05-18 04:48 /user/hive/warehouse/default__index_test_index1_index_test__/dt=192.168.1.62
drwxr-xr-x - hadoop supergroup 0 2018-05-18 04:48 /user/hive/warehouse/default__index_test_index1_index_test__/dt=192.168.1.9
``` 14) 删除索引。 ```
hive> drop index index1_index_test on index_test;
OK
Time taken: 4.842 seconds
hive> show index on index_test;
OK
Time taken: 0.031 seconds ``` 15) 索引数据也被删除。 ```
[hadoop@master hadoop-2.6.0]$ hadoop fs -ls /user/hive/warehouse/default__index_test_index1_index_test__
18/05/18 04:53:52 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
ls: `/user/hive/warehouse/default__index_test_index1_index_test__': No such file or directory ``` &16) 修改配置文件信息。 ```
[hadoop@master hadoop-2.6.0]$ cd /opt/modules/hive1.0.0/conf/
[hadoop@master conf]$ sudo vim hive-site.xml(/+字符串快速查找)
<property>
<name>hive.optimize.index.filter</name>
<value>true</value>
</property>
<property>
<name>hive.optimize.index.groupby</name>
<value>true< /value>
</property>
<property>
<name>hive.optimize.index.filter.compact.minsize</name>
<value>5120</value>
</property> ```
hive.optimize.index.filter 和 hive.optimize.index.groupby 参数默认是 false。使用索引的时候必须把这两个参数开启,才能起到作用。 hive.optimize.index.filter.compact.minsize 参数为输入一个紧凑的索引将被自动采用最小尺寸、默认5368709120(以字节为单位)。 以上就是博主为大家介绍的这一板块的主要内容,这都是博主自己的学习过程,希望能给大家带来一定的指导作用,有用的还望大家点个支持,如果对你没用也望包涵,有错误烦请指出。如有期待可关注博主以第一时间获取更新哦,谢谢! 版权声明:本文为博主原创文章,未经博主允许不得转载。
Hive 基本语法操练(二):视图和索引操作的更多相关文章
- Hive基本语法操练
建表规则如下: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment ...
- Hive 基本语法操练(三):分区操作和桶操作
(一)分区操作 Hive 的分区通过在创建表时启动 PARTITION BY 实现,用来分区的维度并不是实际数据的某一列,具体分区的标志是由插入内容时给定的.当要查询某一分区的内容时可以采用 WHER ...
- Hive 基本语法操练(六):Hive 的权限控制
Hive 的权限控制 Hive从0.10可以通过元数据控制权限.但是Hive的权限控制并不是完全安全的.基本的授权方案的目的是防止用户不小心做了不合适的事情. 为了使用Hive的授权机制,有两个参数必 ...
- Hive 基本语法操练(五):Hive 的 JOIN 用法
Hive 的 JOIN 用法 hive只支持等连接,外连接,左半连接.hive不支持非相等的join条件(通过其他方式实现,如left outer join),因为它很难在map/reduce中实现这 ...
- Hive 基本语法操练(四):Hive 复合类型
hive语法中主要提供了以下复合数据类型: 1)Structs: structs内部的数据可以通过DOT(.)来存取.例如,表中一列c的类型为STRUCT{a INT; b INT},我们可以通过c. ...
- Hive 基本语法操练(一):表操作
Hive 和 Mysql 的表操作语句类似,如果熟悉 Mysql,学习Hive 的表操作就非常容易了,下面对 Hive 的表操作进行深入讲解. **(1)先来创建一个表名为student的内部表** ...
- Elasticsearch必知必会的干货知识二:ES索引操作技巧
该系列上一篇文章<Elasticsearch必知必会的干货知识一:ES索引文档的CRUD> 讲了如何进行index的增删改查,本篇则侧重讲解说明如何对index进行创建.更改.迁移.查询配 ...
- Hive SQL 语法学习与实践
Hive 介绍 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构化的数据文件映射为一张数据库表,并提供 ...
- 008-elasticsearch5.4.3【二】ES使用、ES客户端、索引操作【增加、删除】、文档操作【crud】
一.ES使用,以及客户端 1.pom引用 <dependency> <groupId>org.elasticsearch.client</groupId> < ...
随机推荐
- 分享一个js技巧!判断一个变量chat_websocket是否存在。
注意!!! 判断一个变量chat_websocket是否存在:if( "undefined" == typeof(chat_websocket) || null == chat_w ...
- css关于内外边距的详细解释
贴图吧,图一眼明了. 无效果时候有 只有一个<div>啦啦啦</div> 只有padding时候有: padding详细设计时: 解释:padding是对内的,如padding ...
- Jenkins搭建Nodejs自动化测试
一.安装Jenkins(Windows) 1. 在Jenkins官网(https://jenkins.io/)下载安装包,解压并安装 2. 安装完成后,会自动打开一个页面,根据提示在安装目录下找到随机 ...
- p4503&bzoj3555 企鹅QQ
传送门(洛谷) 传送门(bzoj) 题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相 ...
- window.parent ,window.top,window.self 详解及parent和opener的区别
window.parent ,window.top,window.self 详解 在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层fr ...
- Qt5编译项目出现GL/gl.h:No such file or directory错误
编译在Ubuntu12.04下安装了Qt5.1.1,在编译工程的时候出现了如下错误:“GL/gl.h:No such file or directory”,查了一下资料发现这个问题由于系统中没有安装O ...
- ProtoBuf练习
环境设置 项目地址 https://github.com/silvermagic/ProtoBufDev.git 操作系统 64位 Fedora 24 安装protobuf $ git clone h ...
- (PHP)redis String(字符串)操作
/** * * String操作 * 字符串操作 * */ //设置键值:成功返回true,否则返回false,键值不存在则新建,否则覆盖 $redis->set('string', 'hell ...
- Spring MVC 基于URL的映射规则(注解版)
好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了.这次就跟着之前的问题,继续总结下Spring MVC中的小知识. 关于SpringMVC的小demo可以参考这里! url-p ...
- [Xcode 实际操作]四、常用控件-(12)环形进度条控件的使用
目录:[Swift]Xcode实际操作 本文将演示环形进度条控件的使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class Vi ...