大数据学习13:Hive的DML整理,查询走MapReduce还是走Fetch
2017-09-15 16:34
495 查看
Hive的DML整理:
小问题:如何不通过访问meta data数据来获取我们指定表的字段
hive> desc formatted emp;
DML
加载数据:
load
关键字local 有:本地文件系统 ;没有:HDFS
关键字filepath 文件路径
关键字overwrite 有:覆盖;没有:追加
关键字tablename:表名
多次load overwrite # 不会产生新文件
多次load #会产生copy文件
再次load overwrite # copy文件会消失
复制表结构后再插入数据
create table emp3 like emp; #产生一个emp3 目录,目录下为空
insert overwrite table emp3 select *from emp; # 在emp3 目录下产生文件000000_0
insert into table emp3 select * fromemp; 在emp3 目录下产生文件000000_0_copy_1
insert overwrite table emp3 select *from emp; # 在emp3 目录下重新产生文件000000_0,copy消失
坑:
(1)在有数据的情况下,插入数据时,字段数量要一样,字段的类型要一致,否则命令执行成功,但是那一字段数据丢失。
(2)如果sql的字段写反了或者错位了,会导致表完全错位。
(3)insert 的时候必须写全字段,空的地方用null或者0 补齐
导出表数据2种方式:
(1)insert overwrite [local] directory ... [row format].......select ..from ...;
e.g.
hive> insert overwrite localdirectory "/root/dl_data" rowformat delimited fields terminated by '\t' select * from emp3;
(2)直接hadoop -fs get
e.g.
[root@hadoop002 dl_data]# hadoop fs –get /user/hive/warehouse/emp3/ 000000_0
注意:get 要把所有文件都get下来,否则数据不全,而用第一种方式,则会全部导出。
版本特性
输出到HDFS,有些cdh版本不支持0.11.0 后支持
0.14 版本后可以支持单条插入,update,delete,但是生产几乎不用
不使用控制台取得结果。
(1)用hive –e
hive -e "select * from emp limit5" 可以在os层面得到结果
hive -e "select * from emp limit5" | grep smith
hive -e "select * from emp limit5" > file
(2)将sql写入text.sql文件中,然后用hive 执行,在控制台会打印输出。
text.sql 然后 hive -f text.sql
聚合函数:max min count sum avg
select count(1),max(sal),min(sal),avg(sal) from emp;
分组
select deptno,avg(sal) from emp group by deptno,job ;
条件语句
select deptno,avg(sal) from emp group by deptno,job
havingavg(sal) > 2500;
select ename,sal,
case
when sal > 1 and sal <=1000 then'lower'
when sal > 1000 and sal <=2000
then'middle'
when sal > 2000 and sal <=4000
then'high'
else 'highest' end
from emp; #不进行MapReduce
数据导入导出:
EXPORT TABLE tablename [PARTITION (part_column="value"[,...])]
TO 'export_target_path' [ FORreplication('eventid') ] #只能是导出到hdfs
IMPORT [[EXTERNAL] TABLEnew_or_original_tablename [PARTITION (part_column="value"[, ...])]]
FROM 'source_path'
[LOCATION 'import_target_path'] #可以从本地导入
e.g.
export table aa to '/hive_export';
[root@hadoop002 dl_data]# hadoop fs -ls/hive_export/_metadata
-rwxr-xr-x 1 root supergroup 1220 2017-09-14 21:34/hive_export/_metadata
[root@hadoop002 dl_data]# hadoop fs -ls /hive_export/emp
Found 2 items
-rwxr-xr-x 1 root supergroup 15712017-09-14 21:46 /hive_export/emp/_metadata #乱码。
drwxr-xr-x - root supergroup 0 2017-09-14 21:46 /hive_export/emp/data #数据
hive> import table emp_imported from'/hive_export/emp'; #导入元数据和数据
export 会导出元数据和数据,导出的数据能在其他的hadoop或者hive实例上用import导入。
有个扩展了解
在查询的时候有些查询会触发MapReduce,有些不会,是什么原因呢?
---------------------------
官方文档:
hive.fetch.task.conversion
Default Value: minimal in Hive0.10.0 through 0.13.1, more in Hive 0.14.0 and later
Added In: Hive 0.10.0 with HIVE-2925; default changedin Hive 0.14.0 with HIVE-7397
Some select queries can be converted toa single FETCH task, minimizing latency. Currently the query should besingle sourced not having any subquery and should not have any aggregationsor distincts (which incur RS – ReduceSinkOperator, requiring a MapReduce
task),lateral views and joins.
Supported values are none, minimal and more.
0. none: Disable hive.fetch.task.conversion (value added in Hive 0.14.0with HIVE-8389)
1. minimal: SELECT *, FILTER onpartition columns (WHERE and HAVING clauses), LIMIT only
2. more: SELECT, FILTER, LIMIT only(including TABLESAMPLE, virtual columns)
"more"can take any kind of expressions in the SELECT clause, including UDFs.
(UDTFs and lateral views are not yet supported – see HIVE-5718.)
##
#<property>
# <name>hive.fetch.task.conversion</name>
# <value>more</value>
# <description>
# Some select queries can be converted to single FETCH task
# minimizing latency.Currently the query should be single
# sourced not having any subquery and should not have
# any aggregations or distincts (which incurrs RS),
# lateral views and joins.
# 1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
# 2. more : SELECT, FILTER, LIMITonly (+TABLESAMPLE, virtual columns)
# </description>
#</property>
###
在hive-site.xml中这个参数可以控制,那些不使用MapReduce的查询是通过fetch来查询的。
在配置的时候将值配置为more即可,也可以用以下两种方法临时修改。
(1)hive> set hive.fetch.task.conversion=more;
(2)bin/hive --hiveconf hive.fetch.task.conversion=more
根据测试:
一些简单的查询是不用通过MapReduce:
(1)select * ,字段等抽字段查询
(2)hive> select * from emp limit 10; #抽样查询
(3)case when 查询
注:Create TableAs Select (CTAS) 会触发MapReduce
小问题:如何不通过访问meta data数据来获取我们指定表的字段
hive> desc formatted emp;
DML
加载数据:
load
关键字local 有:本地文件系统 ;没有:HDFS
关键字filepath 文件路径
关键字overwrite 有:覆盖;没有:追加
关键字tablename:表名
多次load overwrite # 不会产生新文件
多次load #会产生copy文件
再次load overwrite # copy文件会消失
复制表结构后再插入数据
create table emp3 like emp; #产生一个emp3 目录,目录下为空
insert overwrite table emp3 select *from emp; # 在emp3 目录下产生文件000000_0
insert into table emp3 select * fromemp; 在emp3 目录下产生文件000000_0_copy_1
insert overwrite table emp3 select *from emp; # 在emp3 目录下重新产生文件000000_0,copy消失
坑:
(1)在有数据的情况下,插入数据时,字段数量要一样,字段的类型要一致,否则命令执行成功,但是那一字段数据丢失。
(2)如果sql的字段写反了或者错位了,会导致表完全错位。
(3)insert 的时候必须写全字段,空的地方用null或者0 补齐
导出表数据2种方式:
(1)insert overwrite [local] directory ... [row format].......select ..from ...;
e.g.
hive> insert overwrite localdirectory "/root/dl_data" rowformat delimited fields terminated by '\t' select * from emp3;
(2)直接hadoop -fs get
e.g.
[root@hadoop002 dl_data]# hadoop fs –get /user/hive/warehouse/emp3/ 000000_0
注意:get 要把所有文件都get下来,否则数据不全,而用第一种方式,则会全部导出。
版本特性
输出到HDFS,有些cdh版本不支持0.11.0 后支持
0.14 版本后可以支持单条插入,update,delete,但是生产几乎不用
不使用控制台取得结果。
(1)用hive –e
hive -e "select * from emp limit5" 可以在os层面得到结果
hive -e "select * from emp limit5" | grep smith
hive -e "select * from emp limit5" > file
(2)将sql写入text.sql文件中,然后用hive 执行,在控制台会打印输出。
text.sql 然后 hive -f text.sql
聚合函数:max min count sum avg
select count(1),max(sal),min(sal),avg(sal) from emp;
分组
select deptno,avg(sal) from emp group by deptno,job ;
条件语句
select deptno,avg(sal) from emp group by deptno,job
havingavg(sal) > 2500;
select ename,sal,
case
when sal > 1 and sal <=1000 then'lower'
when sal > 1000 and sal <=2000
then'middle'
when sal > 2000 and sal <=4000
then'high'
else 'highest' end
from emp; #不进行MapReduce
数据导入导出:
EXPORT TABLE tablename [PARTITION (part_column="value"[,...])]
TO 'export_target_path' [ FORreplication('eventid') ] #只能是导出到hdfs
IMPORT [[EXTERNAL] TABLEnew_or_original_tablename [PARTITION (part_column="value"[, ...])]]
FROM 'source_path'
[LOCATION 'import_target_path'] #可以从本地导入
e.g.
export table aa to '/hive_export';
[root@hadoop002 dl_data]# hadoop fs -ls/hive_export/_metadata
-rwxr-xr-x 1 root supergroup 1220 2017-09-14 21:34/hive_export/_metadata
[root@hadoop002 dl_data]# hadoop fs -ls /hive_export/emp
Found 2 items
-rwxr-xr-x 1 root supergroup 15712017-09-14 21:46 /hive_export/emp/_metadata #乱码。
drwxr-xr-x - root supergroup 0 2017-09-14 21:46 /hive_export/emp/data #数据
hive> import table emp_imported from'/hive_export/emp'; #导入元数据和数据
export 会导出元数据和数据,导出的数据能在其他的hadoop或者hive实例上用import导入。
有个扩展了解
在查询的时候有些查询会触发MapReduce,有些不会,是什么原因呢?
---------------------------
官方文档:
hive.fetch.task.conversion
Default Value: minimal in Hive0.10.0 through 0.13.1, more in Hive 0.14.0 and later
Added In: Hive 0.10.0 with HIVE-2925; default changedin Hive 0.14.0 with HIVE-7397
Some select queries can be converted toa single FETCH task, minimizing latency. Currently the query should besingle sourced not having any subquery and should not have any aggregationsor distincts (which incur RS – ReduceSinkOperator, requiring a MapReduce
task),lateral views and joins.
Supported values are none, minimal and more.
0. none: Disable hive.fetch.task.conversion (value added in Hive 0.14.0with HIVE-8389)
1. minimal: SELECT *, FILTER onpartition columns (WHERE and HAVING clauses), LIMIT only
2. more: SELECT, FILTER, LIMIT only(including TABLESAMPLE, virtual columns)
"more"can take any kind of expressions in the SELECT clause, including UDFs.
(UDTFs and lateral views are not yet supported – see HIVE-5718.)
##
#<property>
# <name>hive.fetch.task.conversion</name>
# <value>more</value>
# <description>
# Some select queries can be converted to single FETCH task
# minimizing latency.Currently the query should be single
# sourced not having any subquery and should not have
# any aggregations or distincts (which incurrs RS),
# lateral views and joins.
# 1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
# 2. more : SELECT, FILTER, LIMITonly (+TABLESAMPLE, virtual columns)
# </description>
#</property>
###
在hive-site.xml中这个参数可以控制,那些不使用MapReduce的查询是通过fetch来查询的。
在配置的时候将值配置为more即可,也可以用以下两种方法临时修改。
(1)hive> set hive.fetch.task.conversion=more;
(2)bin/hive --hiveconf hive.fetch.task.conversion=more
根据测试:
一些简单的查询是不用通过MapReduce:
(1)select * ,字段等抽字段查询
(2)hive> select * from emp limit 10; #抽样查询
(3)case when 查询
注:Create TableAs Select (CTAS) 会触发MapReduce
相关文章推荐
- Hive学习笔记 --- Hive中创建分区表,并且查询数据
- Hive学习笔记 5 Hive的数据查询、函数
- Hive中SQL查询转MapReduce作业过程------学习笔记
- 大数据学习12:Hive的整体框架和DDL整理
- 数据库(学习整理)----4--Oracle数据查询(基础点1)
- 多维数据查询OLAP及MDX语言学习整理
- MySQL 学习教程(七)【整理】-数据的基本查询操作
- hadoop学习笔记之HiveSQL 数据查询
- 大数据技术学习笔记之hive框架基础2-hive中常用DML和UDF和连接接口使用
- php查询数据的4个fetch用法和区别
- mysql学习笔记四(查询数据)
- 快速查询hive数据仓库表中的总条数
- Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)
- 一脸懵逼学习Hive(数据仓库基础构架)
- android学习日记13--数据存储之File存储
- NoSQL之【MongoDB】学习(二):DML和查询操作说明
- pg学习_数据查询
- hive学习-测试数据
- hive加载数据后查询为空
- hadoop学习第八节:Hive介绍和安装部署(根据实验楼整理)