zoukankan      html  css  js  c++  java
  • 深入理解hive之事务处理

    事务的四个特性


       1.automicity:原子性

       2.consistency:一致性

       3. isolation:独立性

       4.durability:持久性

      5.支持事务有几个条件需要满足:1.所有的事务都支持自动提交;2.只支持ORC格式的数据;3.桶表

      7.配置hive的参数使其支持事务:

       在hive-site.xml文件中进行如下的配置

    <property>
        <name>hive.support.concurrency</name>
        <value>true</value>
    </property>
    <property>
        <name>hive.exec.dynamic.partition.mode</name>
        <value>nonstrict</value>
    </property>
    <property>
        <name>hive.txn.manager</name>
        <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
    </property>
    <property>
        <name>hive.compactor.initiator.on</name>
        <value>true</value>
    </property>
    <property>
        <name>hive.compactor.worker.threads</name>
        <value>1</value>
    </property>
    <property>
        <name>hive.enforce.bucketing</name>
        <value>true</value>
    </property>

    对几个重要的属性做阐述:

      hive.exec.dynamic.partition.mode: 
      可选值有:strict, nonstric. 
      strict严格模式下,必须制定一个partition为静态分区,目的是为了防止误操作其他partition. 
      在一个事务中,可能不止会更新一个Partition, 而且更新时也无法控制到底哪些partition会被操作到,因此为了支持事务,必须使用 Nonstrict.

      hive.compactor.initiator.on 
      默认值是 false, 因为默认的情况连事务都不开启。 
      这个属性很重要的原因是回答之前我们的一个问题,如果 delta 文件过多,对namenode造成了影响,我们改如何改善系统性能?(在thrift metaserver 上)开启了这个属性之后,会使得在 metaStore 实例上运行   Initiator, cleaner 进程。initiator 进程负责查找哪些表或者分区的 delta 文件需要被压缩,cleaner 进程负责删除已经不再需要的 delta 文件。接下来看看几个hive的事务性操作

     $hive>create table tx(id int,name string,age int) clustered by (id) into 3 buckets row format delimited fields terminated by ',' stored as orc ;//创建桶表,存储格式为orc使其支持事务
     $hive>desc formatted tx ;  //查看tx表的结构
    $hive>insert into tx values(1,'tom',23);    //向桶表中来插入数据

      

     hive分区


      

      Hive分区的概念与传统关系型数据库分区不同。

      传统数据库的分区方式:就oracle而言,分区独立存在于段里,里面存储真实的数据,在数据进行插入的时候自动分配分区。

      Hive的分区方式:由于Hive实际是存储在HDFS上的抽象,Hive的一个分区名对应一个目录名,子分区名就是子目录名,并不是一个实际字段。所以我们在插入数据的时候指定分区,就是新建一个目录或者子目录,或者在原来目录的基础上来添加数据。对于hive分区而言,可以分为静态分区和动态分区这两个类

      1.静态分区 

       $hive>create table customers(id int,name string ,age int ) partitioned by(year int,month int) row format delimited  fields terminated by ','; //创建静态分区表

         $hive>alter table customers add partition(year=2014,month=11) partition(year=2014,month=12);//在静态分区表中来添加分区

       $hive>desc customers;//查看表结构

        $hive>show partitions customers ;   //查看customers表的分区结构

       $hive>load data local inpath '/data/customers.txt' into table customers  partition(year=2014,year=11); //从外部表加载数据到静态分区表的指定分区中来,这是文件的复制操作

        $hive>dfs -lsr /;  //查看文件系统的文件结构

        $hive>select  * from customers where year=2014 and  month=11;

        新建表的时候定义的分区顺序,决定了文件目录顺序(谁是父目录谁是子目录),正因为有了这个层级关系,当我们查询所有year=1024的时候,2014以下的所有日期下的数据都会被查出来。如果只查询月份分区,但父目录都有该日期的数据,那么Hive会对输入路径进行修剪,从而只扫描日期分区,性别分区不作过滤(即查询结果包含了所有性别)。

       2.动态分区

      在使用静态分区的时候,我们首先要知道有什么分区类型,然后每个分区来进行数据的加载,这个操作过程比较麻烦;而动态分区不会有这些不必要的操作,动态分区可以根据查询得到的数据动态地分配到分区中去,动态分区与静态分区最大的区别是不指定分区目录,由系统自己进行过选择。

      动态分区模式可以分为严格模式(strict)和非严格模式(non-strict),二者的区别是:严格模式在进行插入的时候至少指定一个静态分区,而非严格模式在进行插入的时候可以不指定静态分区

      首先启动动态分区的功能:

     $hive>set hive.exec.dynamic.partition=true;

      再设置分区模式为非严格模式

     

    $hive>set hive.exec.dynamic.partition.mode=nonstrict

        

      

  • 相关阅读:
    webdav srs相关
    How To Configure WebDAV Access with Apache on Ubuntu 14.04
    ubuntu 编译lighttpd
    srs编译及推流测试
    Compile pciutils (lspci, setpci) in Windows x86,在 Windows x86 平台下编译 pciutils (lspci, setpci)
    mingw MSYS2 区别
    Qt之美(三):隐式共享
    Qt之美(二):元对象
    Qt之美(一):d指针/p指针详解
    C++的栈空间和堆空间
  • 原文地址:https://www.cnblogs.com/bigdata-stone/p/9316107.html
Copyright © 2011-2022 走看看