zoukankan      html  css  js  c++  java
  • Hive(八)Hive的Shell操作与压缩存储

    一、Hive的命令行

    1、Hive支持的一些命令

    Command Description

    quit Use quit or exit to leave the interactive shell.

    set key=value Use this to set value of particular configuration variable. One thing to note here is that if you misspell the variable name, cli will not show an error.

    set This will print a list of configuration variables that are overridden by user or hive.

    set -v This will print all hadoop and hive configuration variables.

    add FILE [file] [file]* Adds a file to the list of resources

    add jar jarname

    list FILE list all the files added to the distributed cache

    list FILE [file]* Check if given resources are already added to distributed cache

    ! [cmd] Executes a shell command from the hive shell

    dfs [dfs cmd] Executes a dfs command from the hive shell

    [query] Executes a hive query and prints results to standard out

    source FILE Used to execute a script file inside the CLI.

    2、语法结构

    hive [-hiveconf x=y]* [<-i filename>]* [<-f filename>|<-e query-string>] [-S]

    说明:

    1、-i 从文件初始化 HQL

    2、-e 从命令行执行指定的 HQL

    3、-f 执行 HQL 脚本

    4、-v 输出执行的 HQL 语句到控制台

    5、-p connect to Hive Server on port number

    6、-hiveconf x=y(Use this to set hive/hadoop configuration variables)

    7、-S:表示以不打印日志的形式执行命名操作

    3、示例

    (1)“-e”不进入hive交互窗口执行sql语句---运行一个查询

    [hadoop@hadoop3 ~]$ hive -e "select * from cookie.cookie1;"

    (2)“-f”执行脚本中sql语句---运行一个文件

     编写hive.sql文件

    运行编写的文件

    (3)运行参数文件

    从配置文件启动 hive,并加载配置文件当中的配置参数

    (4)执行文件中的sql语句并将结果写入文件中

     bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt

    4、其他命令

    1)退出hive窗口:

    在新版的oracle中没区别了,在以前的版本是有的:

    exit:先隐性提交数据,再退出; quit:不提交数据,退出;

    hive(default)>exit;

    hive(default)>quit;

    2)在hive cli命令窗口中如何查看hdfs文件系统

     hive(default)>dfs -ls /;

    3)在hive cli命令窗口中如何查看hdfs本地系统

     hive(default)>! ls /opt/module/datas;

    4)查看在hive中输入的所有历史命令

    (1)进入到当前用户的根目录/root或/home/admin

    (2)查看. hivehistory文件

    cat .hivehistory

    二、Hive的参数配置方式

    1、Hive的参数配置大全

    https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties

    2、Hive的参数设置方式

    开发 Hive 应用时,不可避免地需要设定 Hive 的参数。设定 Hive 的参数可以调优 HQL 代码 的执行效率,或帮助定位问题。然而实践中经常遇到的一个问题是,为什么设定的参数没有 起作用?这通常是错误的设定方式导致的

    对于一般参数,有以下三种设定方式:

    1、配置文件 (全局有效)

    2、命令行参数(对 hive 启动实例有效)

    3、参数声明 (对 hive 的连接 session 有效)

    (1)配置文件

    Hive 的配置文件包括:

      A. 用户自定义配置文件:$HIVE_CONF_DIR/hive-site.xml

      B. 默认配置文件:$HIVE_CONF_DIR/hive-default.xml

    用户自定义配置会覆盖默认配置。

    另外,Hive 也会读入 Hadoop 的配置,因为 Hive 是作为 Hadoop 的客户端启动的,Hive 的配 置会覆盖 Hadoop 的配置。

    配置文件的设定对本机启动的所有 Hive 进程都有效。

    (2)命令行参数

    启动 Hive(客户端或 Server 方式)时,可以在命令行添加-hiveconf param=value 来设定参数,例如:

    这一设定对本次启动的 session(对于 server 方式启动,则是所有请求的 session)有效。

    (3)参数声明 

    可以在 HQL 中使用 SET 关键字设定参数,例如:

    这一设定的作用域也是 session 级的。

    set hive.exec.reducers.bytes.per.reducer= 每个 reduce task 的平均负载数据量 Hive 会估算总数据量,然后用该值除以上述参数值,就能得出需要运行的 reduceTask 数

    set hive.exec.reducers.max= 设置 reduce task 数量的上限

    set mapreduce.job.reduces= 指定固定的 reduce task 数量

    但是,这个参数在必要时<业务逻辑决定只能用一个 reduce task> hive 会忽略,比如在设置 了 set mapreduce.job.reduces = 3,但是 HQL 语句当中使用了 order by 的话,那么就会忽略该参数的设置。

    上述三种设定方式的优先级依次递增。即参数声明覆盖命令行参数,命令行参数覆盖配置 文件设定。注意某些系统级的参数,例如 log4j 相关的设定,必须用前两种方式设定,因为 那些参数的读取在 session 建立以前已经完成了。

    三、查询

     备注:官网地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select

    语法备注

    1、基本查询(Select…From)

    1.1 全表和特定字段查询

     1)全表查询

    hive (default)> select * from emp;

     2)选择特定列查询

    hive (default)> select empno, ename from emp;

     注意:

    (1)SQL 语言大小写不敏感。

    (2)SQL 可以写在一行或者多行

    (3)关键字不能被缩写也不能分行

    (4)各子句一般要分行写。

    (5)使用缩进提高语句的可读性。

    1.2 列别名

     1)重命名一个列。

     2)便于计算。

     3)紧跟列名,也可以在列名和别名之间加入关键字‘AS’

     4)案例实操

    (1)查询名称和部门

    hive (default)> select ename AS name, deptno dn from emp;

    1.3 算术运算符

    案例实操

     hive (default)> select sal +1 from emp;

    1.4 常用函数

    1)求总行数(count)

    hive (default)> select count(*) cnt from emp;

     2)求工资的最大值(max)

    hive (default)> select max(sal) max_sal from emp;

     3)求工资的最小值(min)

    hive (default)> select min(sal) min_sal from emp;

    4)求工资的总和(sum)

    hive (default)> select sum(sal) sum_sal from emp;

    5)求工资的平均值(avg)

    hive (default)> select avg(sal) avg_sal from emp;

    1.5 Limit语句

    典型的查询会返回多行数据。LIMIT子句用于限制返回的行数。

     hive (default)> select * from emp limit 5;

    2、Where语句

    介绍

    1)使用WHERE 子句,将不满足条件的行过滤掉。

    2)WHERE 子句紧随 FROM 子句。

    3)案例实操

    查询出薪水大于1000的所有员工

    hive (default)> select * from emp where sal >1000;

    2.1 比较运算符(Between/In/ Is Null)

    1)下面表中描述了谓词操作符,这些操作符同样可以用于JOIN…ON和HAVING语句中。

    2)案例实操

    (1)查询出薪水等于5000的所有员工

    hive (default)> select * from emp where sal =5000;

    (2)查询工资在500到1000的员工信息

    hive (default)> select * from emp where sal between 500 and 1000;

    (3)查询comm为空的所有员工信息

    hive (default)> select * from emp where comm is null;

    (4)查询工资是1500和5000的员工信息

    hive (default)> select * from emp where sal IN (1500, 5000);

    2.2 Like和RLike

    1)使用LIKE运算选择类似的值

    2)选择条件可以包含字符或数字:

     % 代表零个或多个字符(任意个字符)。

     _ 代表一个字符。

      3)RLIKE子句是Hive中这个功能的一个扩展,其可以通过Java的正则表达式这个更强大的语言来指定匹配条件。

     4)案例实操

    (1)查找以2开头薪水的员工信息

    hive (default)> select * from emp where sal LIKE '2%';

    (2)查找第二个数值为2的薪水的员工信息

    hive (default)> select * from emp where sal LIKE '_2%';

    3)查找薪水中含有2的员工信息

    hive (default)> select * from emp where sal RLIKE '[2]';

    2.3 逻辑运算符(And/Or/Not)

    案例实操

    (1)查询薪水大于1000,部门是30

    hive (default)> select * from emp where sal>1000 and deptno=30;

    (2)查询薪水大于1000,或者部门是30

    hive (default)> select * from emp where sal>1000 or deptno=30;

    (3)查询除了20部门和30部门以外的员工信息

    hive (default)> select * from emp where deptno not IN(30, 20);

    3、分组

    3.1 Group By语句

    GROUP BY语句通常会和聚合函数一起使用,按照一个或者多个列队结果进行分组,然后对每个组执行聚合操作。

    案例实操:

    (1)计算emp表每个部门的平均工资 

    hive (default)> select t.deptno, avg(t.sal) avg_sal from emp t group by t.deptno;

    (2)计算emp每个部门中每个岗位的最高薪水

    hive (default)> select t.deptno, t.job, max(t.sal) max_sal from emp t group by t.deptno, t.job;

    3.2 Having语句

     1)having与where不同点

    (1)where针对表中的列发挥作用,查询数据;having针对查询结果中的列发挥作用,筛选数据。

    (2)where后面不能写分组函数,而having后面可以使用分组函数。

    (3)having只用于group by分组统计语句。

    2)案例实操:

    (1)求每个部门的平均薪水大于2000的部门

    求每个部门的平均工资

    hive (default)> select deptno, avg(sal) from emp group by deptno;

    求每个部门的平均薪水大于2000的部门

    hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno having avg_sal > 2000;

    4、Join语句

    4.1 等值Join

      Hive支持通常的SQL JOIN语句,但是只支持等值连接,不支持非等值连接。

    案例操作

    (1)根据员工表和部门表中的部门编号相等,查询员工编号、员工名称和部门编号;

    hive (default)> select e.empno, e.ename, d.deptno, d.dname from emp e join dept d on e.deptno = d.deptno;

    4.2 表的别名

     1)好处

    (1)使用别名可以简化查询。

    (2)使用表名前缀可以提高执行效率。

      2)案例实操

      合并员工表和部门表

    hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;

    4.3 内连接

    内连接:只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来。

    hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;

    4.4 左外连接

    左外连接:JOIN操作符左边表中符合WHERE子句的所有记录将会被返回。

    hive (default)> select e.empno, e.ename, d.deptno from emp e left join dept d on e.deptno = d.deptno;

    4.5 右外连接

    右外连接:JOIN操作符右边表中符合WHERE子句的所有记录将会被返回。

    hive (default)> select e.empno, e.ename, d.deptno from emp e right join dept d on e.deptno = d.deptno;

    4.6 满外链接

    满外连接:将会返回所有表中符合WHERE语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。

    hive (default)> select e.empno, e.ename, d.deptno from emp e full join dept d on e.deptno = d.deptno;

    4.7 多表连接

    注意:连接 n个表,至少需要n-1个连接条件。例如:连接三个表,至少需要两个连接条件。

     0)数据准备

    location.txt

    1)创建位置表

    create table if not exists default.location(

    loc int,

    loc_name string

    )

    row format delimited fields terminated by ' ';

      2)导入数据

    hive (default)> load data local inpath '/opt/module/datas/location.txt' into table default.location;

     3)多表连接查询

    hive (default)>SELECT e.ename, d.deptno, l. loc_name

    FROM emp e

    JOIN dept d

    ON d.deptno = e.deptno

    JOIN location l

    ON d.loc = l.loc;

    大多数情况下,Hive会对每对JOIN连接对象启动一个MapReduce任务。本例中会首先启动一个MapReduce job对表e和表d进行连接操作,然后会再启动一个MapReduce job将第一个MapReduce job的输出和表l;进行连接操作。

    注意:为什么不是表d和表l先进行连接操作呢?这是因为Hive总是按照从左到右的顺序执行的。

    4.8 笛卡尔积 JOIN

     1)笛卡尔集会在下面条件下产生:

    (1)省略连接条件

    (2)连接条件无效

    (3)所有表中的所有行互相连接

     2)案例实操

    hive (default)> select empno, deptno from emp, dept;

    FAILED: SemanticException Column deptno Found in more than One Tables/Subqueries

    5、排序

    5.1 全局排序(Order By)

     Order By:全局排序,一个MapReduce

    1)使用 ORDER BY 子句排序

     ASC(ascend): 升序(默认)

     DESC(descend): 降序

    2)ORDER BY 子句在SELECT语句的结尾。

     3)案例实操

    (1)查询员工信息按工资升序排列

    hive (default)> select * from emp order by sal;

    (2)查询员工信息按工资降序排列

    hive (default)> select * from emp order by sal desc;

    5.2 按照别名排序

    按照员工薪水的2倍排序

    hive (default)> select ename, sal*2 twosal from emp order by twosal;

    5.3 多个列排序

    按照部门和工资升序排序

    hive (default)> select ename, deptno, sal from emp order by deptno, sal ;

    5.4 每个MapReduce内部排序(Sort By)

     Sort By:每个MapReduce内部进行排序,对全局结果集来说不是排序

    1)设置reduce个数

    hive (default)> set mapreduce.job.reduces=3;

     2)查看设置reduce个数

    hive (default)> set mapreduce.job.reduces;

    3)根据部门降序查看员工信息

    hive (default)> select * from emp sort by empno desc;

    4)将查询结果导入到文件中(按照部门编号降序排序)

    hive (default)> insert overwrite local directory '/opt/module/datas/sortby-result' select * from emp sort by deptno desc;

    5.5 分区排序(Distribute By)

     Distribute By:类似MR中partition,进行分区,结合sort by使用。

    注意,Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前。

    案例实操:

    (1)先按照部门编号分区,再按照员工编号降序排序。

    hive (default)> insert overwrite local directory '/opt/module/datas/distby-desc' select * from emp distribute by deptno sort by empno desc;

    5.6 Cluster By

    当distribute by和sorts by字段相同时,可以使用cluster by方式。

    cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒序排序,不能指定排序规则为ASC或者DESC。

      1)以下两种写法等价

     select * from emp cluster by deptno;

     select * from emp distribute by deptno sort by deptno;

     注意:按照部门编号分区,不一定就是固定死的数值,可以是20号和30号部门分到一个分区里面去。

    6、分桶及抽样查询

    6.1 分桶表数据存储

    分区针对的是数据的存储路径;分桶针对的是数据文件。

      分区提供一个隔离数据和优化查询的便利方式。不过,并非所有的数据集都可形成合理的分区,特别是之前所提到过的要确定合适的划分大小这个疑虑。

     分桶是将数据集分解成更容易管理的若干部分的另一个技术。

     1)先创建分桶表,通过直接导入数据文件的方式

     (0)数据准备

    student.txt

    (1)创建分桶表

    create table stu_buck(id int, name string)

    clustered by(id)

    into 4 buckets

    row format delimited fields terminated by ' '

    (2)查看表结构

    hive (default)> desc formatted stu_buck;

    Num Buckets: 4

    (3)导入数据到分桶表中

    hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu_buck;

    (4)查看创建的分桶表中是否分成4个桶

      发现并没有分成4个桶。是什么原因呢?

     2)创建分桶表时,数据通过子查询的方式导入

    (1)先建一个普通的stu表

    create table stu(id int, name string)

    row format delimited fields terminated by ' ';

    (2)向普通的stu表中导入数据

    load data local inpath '/opt/module/datas/student.txt' into table stu;

    (3)清空stu_buck表中数据

    truncate table stu_buck;

    select * from stu_buck;

    (4)导入数据到分桶表,通过子查询的方式

    insert into table stu_buck

    select id, name from stu cluster by(id);

    (5)发现还是只有一个分桶

    (6)需要设置一个属性

    hive (default)>set hive.enforce.bucketing=true;

    hive (default)> set mapreduce.job.reduces=-1;

    hive (default)>insert into table stu_buck

    select id, name from stu cluster by(id);

    (7)查询分桶的数据

    hive (default)> select * from stu_buck;

    OK

    查询结果

    6.2 分桶抽样查询

    对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。

    查询6.6.1中表stu_buck的数据。

    hive (default)> select * from stu_buck TABLESAMPLE(bucket 1 out of 4 on id);

    注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。

    y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。

    x表示从哪个bucket开始抽取。例如,table总bucket数为4,tablesample(bucket 4 out of 4),表示总共抽取(4/4=)1个bucket的数据,抽取第4个bucket的数据。

    注意:x的值必须小于等于y的值,否则

    FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck

    6.3 数据块抽样

    Hive提供了另外一种按照百分比进行抽样的方式,这种事基于行数的,按照输入路径下的数据块百分比进行的抽样。

    hive (default)> select * from stu tablesample(0.1 percent);

    提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是一个HDFS数据块。因此,如果表的数据大小小于普通的块大小128M的话,那么将会返回所有行。

    四、Hive的压缩与存储

    1hadoop源码编译支持snappy压缩

    1.1 前期准备工作

    1)CentOS联网

    [root@node100 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

    DEVICE=eth0

    HWADDR=00:0c:29:ca:6e:ec

    TYPE=Ethernet

    UUID=9e008bf7-44f6-4e72-8ead-71b8ea7a9b5b

    ONBOOT=yes

    NM_CONTROLLED=yes

    BOOTPROTO=dhcp

    [root@node100 ~]# service network restart

    注意:采用root角色编译,减少文件夹权限出现问题

    2)jar包准备(hadoop源码、JDK7 、 maven、 ant 、protobuf)

    (1)hadoop-2.7.6-src.tar.gz

    (2)jdk-7u79-linux-x64.gz

    (3)snappy-1.1.3.tar.gz

    (4)apache-maven-3.0.5-bin.tar.gz

    (5)protobuf-2.5.0.tar.gz

    1.2 jar包安装

    0)注意:所有操作必须在root用户下完成

    1)JDK解压、配置环境变量 JAVA_HOME和PATH,验证java-version(如下都需要验证是否配置成功)

    [root@node100 software] # tar -zxf jdk-7u79-linux-x64.gz -C /opt/module/

    [root@node100 software]# vi /etc/profile

    #JAVA_HOME

    export JAVA_HOME=/opt/module/jdk1.7.0_79

    export PATH=$PATH:$JAVA_HOME/bin

    [root@node100 software]#source /etc/profile

    验证命令:java -version

    2)Maven解压、配置  MAVEN_HOME和PATH。

    [root@node100 software]# tar -zxvf apache-maven-3.0.5-bin.tar.gz -C /opt/module/

    [root@node100 apache-maven-3.0.5]# vi /etc/profile

    #MAVEN_HOME

    export MAVEN_HOME=/opt/module/apache-maven-3.0.5

    export PATH=$PATH:$MAVEN_HOME/bin

    [root@node100 software]#source /etc/profile

    验证命令:mvn -version

    1.3 编译源码

    1)准备编译环境

    [root@node100 software]# yum install svn

    [root@node100 software]# yum install autoconf automake libtool cmake

    [root@node100 software]# yum install ncurses-devel

    [root@node100 software]# yum install openssl-devel

    [root@node100 software]# yum install gcc*

    2)编译安装snappy

    [root@node100 software]# tar -zxvf snappy-1.1.3.tar.gz -C /opt/module/

    [root@node100 module]# cd snappy-1.1.3/

    [root@node100 snappy-1.1.3]# ./configure

    [root@node100 snappy-1.1.3]# make

    [root@node100 snappy-1.1.3]# make install

    # 查看snappy库文件

    [root@node100 snappy-1.1.3]# ls -lh /usr/local/lib |grep snappy

    3)编译安装protobuf

    [root@node100 software]# tar -zxvf protobuf-2.5.0.tar.gz -C /opt/module/

    [root@node100 module]# cd protobuf-2.5.0/

    [root@node100 protobuf-2.5.0]# ./configure 

    [root@node100 protobuf-2.5.0]#  make 

    [root@node100 protobuf-2.5.0]#  make install

    # 查看protobuf版本以测试是否安装成功
    [root@node100 protobuf-2.5.0]# protoc --version

    4)编译hadoop native

    [root@node100 software]# tar -zxvf hadoop-2.7.6-src.tar.gz

    [root@node100 software]# cd hadoop-2.7.6-src/

    [root@node100 software]# mvn clean package -DskipTests -Pdist,native -Dtar -Dsnappy.lib=/usr/local/lib -Dbundle.snappy

    执行成功后,/opt/software/hadoop-2.7.6-src/hadoop-dist/target/hadoop-2.7.6.tar.gz即为新生成的支持snappy压缩的二进制安装包。

    2Hadoop压缩配置

    2.1 MR支持的压缩编码

    压缩格式

    工具

    算法

    文件扩展名

    是否可切分

    DEFAULT

    DEFAULT

    .deflate

    Gzip

    gzip

    DEFAULT

    .gz

    bzip2

    bzip2

    bzip2

    .bz2

    LZO

    lzop

    LZO

    .lzo

    LZ4

    LZ4

    .lz4

    Snappy

    Snappy

    .snappy

    为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器,如下表所示

    压缩格式

    对应的编码/解码器

    DEFLATE

    org.apache.hadoop.io.compress.DefaultCodec

    gzip

    org.apache.hadoop.io.compress.GzipCodec

    bzip2

    org.apache.hadoop.io.compress.BZip2Codec

    LZO

    com.hadoop.compression.lzo.LzopCodec

    LZ4

    org.apache.hadoop.io.compress.Lz4Codec

    Snappy

    org.apache.hadoop.io.compress.SnappyCodec

    压缩性能的比较

    压缩算法

    原始文件大小

    压缩文件大小

    压缩速度

    解压速度

    gzip

    8.3GB

    1.8GB

    17.5MB/s

    58MB/s

    bzip2

    8.3GB

    1.1GB

    2.4MB/s

    9.5MB/s

    LZO

    8.3GB

    2.9GB

    49.3MB/s

    74.6MB/s

    http://google.github.io/snappy/

    On a single core of a Core i7 processor in 64-bit mode, Snappy compresses at about 250 MB/sec or more and decompresses at about 500 MB/sec or more.

    2.2 压缩配置参数

    要在Hadoop中启用压缩,可以配置如下参数(mapred-site.xml文件中):

    参数

    默认值

    阶段

    建议

    io.compression.codecs   

    (在core-site.xml中配置)

    org.apache.hadoop.io.compress.DefaultCodec, org.apache.hadoop.io.compress.GzipCodec, org.apache.hadoop.io.compress.BZip2Codec,

    org.apache.hadoop.io.compress.Lz4Codec

    输入压缩

    Hadoop使用文件扩展名判断是否支持某种编解码器

    mapreduce.map.output.compress

    false

    mapper输出

    这个参数设为true启用压缩

    mapreduce.map.output.compress.codec

    org.apache.hadoop.io.compress.DefaultCodec

    mapper输出

    使用LZO、LZ4或snappy编解码器在此阶段压缩数据

    mapreduce.output.fileoutputformat.compress

    false

    reducer输出

    这个参数设为true启用压缩

    mapreduce.output.fileoutputformat.compress.codec

    org.apache.hadoop.io.compress. DefaultCodec

    reducer输出

    使用标准工具或者编解码器,如gzip和bzip2

    mapreduce.output.fileoutputformat.compress.type

    RECORD

    reducer输出

    SequenceFile输出使用的压缩类型:NONE和BLOCK

    3开启Map输出阶段压缩

    开启map输出阶段压缩可以减少job中map和Reduce task间数据传输量。具体配置如下:

    案例实操:

    1)开启hive中间传输数据压缩功能

    hive (default)>set hive.exec.compress.intermediate=true;

    2)开启mapreduce中map输出压缩功能

    hive (default)>set mapreduce.map.output.compress=true;

    3)设置mapreduce中map输出数据的压缩方式

    hive (default)>set mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec;

    4)执行查询语句

    hive (default)> select count(ename) name from emp;

    4开启Reduce输出阶段压缩

    当Hive将输出写入到表中时,输出内容同样可以进行压缩。属性hive.exec.compress.output控制着这个功能。用户可能需要保持默认设置文件中的默认值false,这样默认的输出就是非压缩的纯文本文件了。用户可以通过在查询语句或执行脚本中设置这个值为true,来开启输出结果压缩功能。

    案例实操

    1)开启hive最终输出数据压缩功能

    hive (default)>set hive.exec.compress.output=true;

    2)开启mapreduce最终输出数据压缩

    hive (default)>set mapreduce.output.fileoutputformat.compress=true;

    3)设置mapreduce最终数据输出压缩方式

    hive (default)> set mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;

    4)设置mapreduce最终数据输出压缩为块压缩

    hive (default)> set mapreduce.output.fileoutputformat.compress.type=BLOCK;

    5)测试一下输出结果是否是压缩文件

    hive (default)> insert overwrite local directory '/opt/module/datas/distribute-result' select * from emp distribute by deptno sort by empno desc;

    5文件存储格式

    Hive支持的存储数的格式主要有:TEXTFILE 、SEQUENCEFILE、ORC、PARQUET。

    5.1 列式存储和行式存储

     

    上图左边为逻辑表,右边第一个为行式存储,第二个为列式存储。

    行存储的特点: 查询满足条件的一整行数据的时候,列存储则需要去每个聚集的字段找到对应的每个列的值,行存储只需要找到其中一个值,其余的值都在相邻地方,所以此时行存储查询的速度更快。

    列存储的特点: 因为每个字段的数据聚集存储,在查询只需要少数几个字段的时候,能大大减少读取的数据量;每个字段的数据类型一定是相同的,列式存储可以针对性的设计更好的设计压缩算法。

    TEXTFILE和SEQUENCEFILE的存储格式都是基于行存储的;

    ORC和PARQUET是基于列式存储的。

    5.2 TEXTFILE格式

    默认格式,数据不做压缩,磁盘开销大,数据解析开销大。可结合Gzip、Bzip2使用(系统自动检查,执行查询时自动解压),但使用这种方式,hive不会对数据进行切分,从而无法对数据进行并行操作。

    5.3 ORC格式

    Orc (Optimized Row Columnar)是hive 0.11版里引入的新的存储格式。

    可以看到每个Orc文件由1个或多个stripe组成,每个stripe250MB大小,这个Stripe实际相当于RowGroup概念,不过大小由4MB->250MB,这样应该能提升顺序读的吞吐率。每个Stripe里有三部分组成,分别是Index Data,Row Data,Stripe Footer:

     

    1)Index Data:一个轻量级的index,默认是每隔1W行做一个索引。这里做的索引应该只是记录某行的各字段在Row Data中的offset。

    2)Row Data:存的是具体的数据,先取部分行,然后对这些行按列进行存储。对每个列进行了编码,分成多个Stream来存储。

    3)Stripe Footer:存的是各个Stream的类型,长度等信息。

    每个文件有一个File Footer,这里面存的是每个Stripe的行数,每个Column的数据类型信息等;每个文件的尾部是一个PostScript,这里面记录了整个文件的压缩类型以及FileFooter的长度信息等。在读取文件时,会seek到文件尾部读PostScript,从里面解析到File Footer长度,再读FileFooter,从里面解析到各个Stripe信息,再读各个Stripe,即从后往前读。

    5.4 PARQUET格式

    Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目。

    Parquet文件是以二进制方式存储的,所以是不可以直接读取的,文件中包括该文件的数据和元数据,因此Parquet格式文件是自解析的。

    通常情况下,在存储Parquet数据的时候会按照Block大小设置行组的大小,由于一般情况下每一个Mapper任务处理数据的最小单位是一个Block,这样可以把每一个行组由一个Mapper任务处理,增大任务执行并行度。Parquet文件的格式如下图所示。

     

    上图展示了一个Parquet文件的内容,一个文件中可以存储多个行组,文件的首位都是该文件的Magic Code,用于校验它是否是一个Parquet文件,Footer length记录了文件元数据的大小,通过该值和文件长度可以计算出元数据的偏移量,文件的元数据中包括每一个行组的元数据信息和该文件存储数据的Schema信息。除了文件中每一个行组的元数据,每一页的开始都会存储该页的元数据,在Parquet中,有三种类型的页:数据页、字典页和索引页。数据页用于存储当前行组中该列的值,字典页存储该列值的编码字典,每一个列块中最多包含一个字典页,索引页用来存储当前行组下该列的索引,目前Parquet中还不支持索引页。

    5.5 主流文件存储格式对比实验

    从存储文件的压缩比和查询速度两个角度对比。

    存储文件的压缩比测试

    0)测试数据

    1)TextFile

    (1)创建表,存储数据格式为TEXTFILE

    create table log_text (

    track_time string,

    url string,

    session_id string,

    referer string,

    ip string,

    end_user_id string,

    city_id string

    )

    row format delimited fields terminated by ' '

    stored as textfile ;

    (2)向表中加载数据

    hive (default)> load data local inpath '/opt/module/datas/log.data' into table log_text ;

    (3)查看表中数据大小

    dfs -du -h /user/hive/warehouse/log_text;

    18.1 M  /user/hive/warehouse/log_text/log.data

    2)ORC

    (1)创建表,存储数据格式为ORC

    create table log_orc(

    track_time string,

    url string,

    session_id string,

    referer string,

    ip string,

    end_user_id string,

    city_id string

    )

    row format delimited fields terminated by ' '

    stored as orc ;

    (2)向表中加载数据

    insert into table log_orc select * from log_text ;

    (3)查看表中数据大小

    dfs -du -h /user/hive/warehouse/log_orc/ ;

    2.8 M  /user/hive/warehouse/log_orc/000000_0

    3)Parquet

    (1)创建表,存储数据格式为parquet

    create table log_parquet(

    track_time string,

    url string,

    session_id string,

    referer string,

    ip string,

    end_user_id string,

    city_id string

    )

    row format delimited fields terminated by ' '

    stored as parquet ;

    (2)向表中加载数据

    insert into table log_parquet select * from log_text ;

    (3)查看表中数据大小

    dfs -du -h /user/hive/warehouse/log_parquet/ ;

    13.1 M  /user/hive/warehouse/log_parquet/000000_0

    存储文件的压缩比总结:

    ORC >  Parquet >  textFile

    存储文件的查询速度测试

    1)TextFile

    hive (default)> select count(*) from log_text;

    _c0

    100000

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

    2)ORC

    hive (default)> select count(*) from log_orc;

    _c0

    100000

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

    3)Parquet

    hive (default)> select count(*) from log_parquet;

    _c0

    100000

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

    存储文件的查询速度总结:

    ORC > TextFile > Parquet

    6、存储和压缩结合

    官网:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ORC

    ORC存储方式的压缩:

    Key

    Default

    Notes

    orc.compress

    ZLIB

    high level compression (one of NONE, ZLIB, SNAPPY)

    orc.compress.size

    262,144

    number of bytes in each compression chunk

    orc.stripe.size

    67,108,864

    number of bytes in each stripe

    orc.row.index.stride

    10,000

    number of rows between index entries (must be >= 1000)

    orc.create.index

    true

    whether to create row indexes

    orc.bloom.filter.columns

    ""

    comma separated list of column names for which bloom filter should be created

    orc.bloom.filter.fpp

    0.05

    false positive probability for bloom filter (must >0.0 and <1.0)

    1)创建一个非压缩的的ORC存储方式

    (1)建表语句

    create table log_orc_none(

    track_time string,

    url string,

    session_id string,

    referer string,

    ip string,

    end_user_id string,

    city_id string

    )

    row format delimited fields terminated by ' '

    stored as orc tblproperties ("orc.compress"="NONE");

    (2)插入数据

    insert into table log_orc_none select * from log_text ;

    (3)查看插入后数据

    dfs -du -h /user/hive/warehouse/log_orc_none/ ;

    7.7 M  /user/hive/warehouse/log_orc_none/000000_0

    2)创建一个SNAPPY压缩的ORC存储方式

    (1)建表语句

    create table log_orc_snappy(

    track_time string,

    url string,

    session_id string,

    referer string,

    ip string,

    end_user_id string,

    city_id string

    )

    row format delimited fields terminated by ' '

    stored as orc tblproperties ("orc.compress"="SNAPPY");

    (2)插入数据

    insert into table log_orc_snappy select * from log_text ;

    (3)查看插入后数据

    dfs -du -h /user/hive/warehouse/log_orc_snappy/ ;

    3.8 M  /user/hive/warehouse/log_orc_snappy/000000_0

    3)上一节中默认创建的ORC存储方式,导入数据后的大小为

    2.8 M  /user/hive/warehouse/log_orc/000000_0

    比Snappy压缩的还小。原因是orc存储文件默认采用ZLIB压缩。比snappy压缩的小。

    4)存储方式和压缩总结:

    在实际的项目开发当中,hive表的数据存储格式一般选择:orc或parquet。压缩方式一般选择snappy。

  • 相关阅读:
    SQL Server 数据库基础编程
    Spring整合DWR comet 实现无刷新 多人聊天室
    SQL Server 数据库安全
    【MongoDB for Java】Java操作MongoDB
    SQL Server 触发器
    使用Dom4j操作XML
    BlazeDS 整合 Flex HelloWorld 示例
    SQL Server 索引和视图
    JDOM 操作XML
    SQL Server 数据库设计
  • 原文地址:https://www.cnblogs.com/ZackSun/p/9713442.html
Copyright © 2011-2022 走看看