hive基礎1

Hive基礎

一、介紹

Hive是OLAP(online analyze process,在線分析處理)。一般稱爲數據倉庫,簡稱數倉。內置不少分析函數,可進行海量數據的在線分析處理。hive構建在hadoop之上,使用hdfs做爲進行存儲,計算過程採用的是Mapreduce完成,本質上hive是對hadoop的mr的封裝,經過原始的mr方式進行數據處理與分析,每每效率較低,並且具備至關的複雜度,學習曲線較長。hive經常使用傳統的sql方式做爲操做手段,極大的下降了學習曲線,畢竟大部分人對sql仍是比較熟悉的。但在運行時,仍然要將sql進行翻譯成mapreduce程序進行。java

二、hive安裝

下載hive的軟件包,解壓以後配置環境變量便可。node

三、hive配置

hive中元數據存在關係型數據庫中,默認是derby數據庫,這裏改爲mysql數據庫。hive的配置文件爲conf/hive-site.xml目錄下:mysql

<configuration>
  ...
  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://192.168.231.1:3306/big11_hive</value>
    <description>
      JDBC connect string for a JDBC metastore.
      To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.
      For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.
    </description>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>root</value>
    <description>Username to use against metastore database</description>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>root</value>
    <description>password to use against metastore database</description>
  </property>
  <property>
    <name>hive.server2.enable.doAs</name>
    <value>false</value>
    <description>
      Setting this property to true will have HiveServer2 execute
      Hive operations as the user making the calls to it.
    </description>
  </property>
  <property>
    <name>hive.metastore.schema.verification</name>
    <value>false</value>
    <description>
      Enforce metastore schema version consistency.
      True: Verify that version information stored in metastore matches with one from Hive jars.  Also disable automatic
            schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures
            proper metastore schema migration. (Default)
      False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
    </description>
  </property>


  ...
</configuration>

經過hive命令查看或者修改配置:算法

  1. header設置sql

    # 查看屬性
    $hive>set hive.cli.print.header ;
    # 修改屬性
    $hive>set hive.cli.print.header=true ;

四、初始化數據庫

$>schematool -dbtype mysql -dbType mysql -initSchema

五、登陸hive的命令行終端

$>hive
$hive>

六、hive經常使用命令

  1. 建立數據庫shell

    $hive>create database if not exists big12 ;

  2. 刪除庫數據庫

    $hive>drop database if exists big12 ;

  3. 建立表apache

    $hive>CREATE TABLE if not exists emp(
    id int ,
    name string ,
    age int,
    dep string,
    salary int
    )
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    lines terminated by  '\n'
    stored as textfile;
  4. 準備數據上傳到emp表centos

    [emp.txt]服務器

    1,tom,22,001,3000
    2,tomas,22,001,2500
    3,tomasLee,25,002,1200
    4,tomson,24,002,3400
    5,peter,24,001,3800
    6,john,25,001,4200
    7,looser,24,001,5500
    8,alex,25,003,6000
    9,jason,24,003,6000
    10,japser,22,003,3000
    # local是上傳文件到hdfs上(hive倉庫目錄下)
    $hive>load data local inpath '/home/centos/emp2.txt' into table emp ;
    
    # 移動hdfs的目錄到hive的表目錄下。
    $hive>load data inpath '/user/centos/emp.txt' into table emp ;
  5. 重命名錶

    $hive>alter table emp rename to e ;

七、hive的複雜類型

7.1類型

  1. map

    映射,key-value對

  2. struct

    結構體,至關於元組(等價於java的實體類),有多個屬性,每一個屬性有不一樣類型。在RDBMS中至關於一行記錄。

  3. array

    也能夠成爲list,有多行構成。

7.2 使用複雜類型

  1. 建立表

    $hive>CREATE TABLE emp(
    name string,
    arr ARRAY<string>,
    stru STRUCT<sex:string,age:int>,
    map1 MAP<string,int>,
    map2 MAP<string,ARRAY<string>>
    )
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY '|'
    COLLECTION ITEMS TERMINATED BY ','
    MAP KEYS TERMINATED BY ':'
    lines terminated by '\n';
  2. 準備數據

    Michael|Montreal,Toronto|Male,30|DB:80|Product:Developer^DLead
    Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
    Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
    Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead
  3. 查詢複雜數據類型

    $hive>select arr[0] , stru.sex , map1["DB"] ,map2["Product"][0] from emp ;
  4. CTAS方式建立表

    # create table as .. select ..
    $hive>create table emp2 ROW FORMAT DELIMITED
    FIELDS TERMINATED BY '|'
    COLLECTION ITEMS TERMINATED BY ','
    MAP KEYS TERMINATED BY ':'
    lines terminated by '\n' as select name ,arr from emp ;
  5. Like方式建立表

    like方式建立表和原表結構相同,不攜帶數據。

    $hive>create external table emp3 like emp ;
  6. 表數據複製

    $hive>insert into emp3 select * from emp ;

八、複雜類型對應的函數

  1. 查看全部函數

    $hive>show functions ;

  2. 查看函數幫助

    # 查看函數
    $hive>desc function array ;
    
    # 查看函數擴展信息
    $hive>desc function extended array ;
  3. array()

    $hive>select array(1,2,3) ;
  4. struct()

    不帶名成的結構體。

    # 構造結構體
    $hive>select struct(1, 'tomas' , 12 ,'hebei' , 80000) ;
    # 無名列,使用coln訪問
    $hive>select struct(1, 'tomas' , 12 ,'hebei' , 80000).col5 ;

  5. named_struct()

    帶有名稱的結構體。

    # 構造帶名結構體
    $hive>select named_struct('id',1,'name','tomas','sal' , 80000) ;
    # 查詢特定字段
    $hive>select named_struct('id',1,'name','tomas','sal' , 80000).sal ;
  6. map()

    map()函數試音key-value映射,使用方式同named_struct相相似。

    # 構造帶名結構體
    $hive>select map('id',1,'name','tomas','sal' , 80000) ;
    # 查詢特定字段
    $hive>select map('id',1,'name','tomas','sal' , 80000).sal ;

九、分區表

在表目錄再根據分區字段建立的子目錄,可以有效縮小搜索範圍。

  1. 建立分區表

    $hive>CREATE TABLE par1(
    id int,
    name string,
    age int
    )
    PARTITIONED BY (prov string, city string)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY '|'
    COLLECTION ITEMS TERMINATED BY ','
    MAP KEYS TERMINATED BY ':'
    lines terminated by '\n';

  2. 手動添加分區

    $hive>alter table par1 add partition(prov='henan' , city='kaifeng') partition(prov='henan' , city='zhengzhou');
  3. 顯式分區信息

    $hive>show partitions par1 ;
  4. 刪除分區

    $hive>alter table par1 DROP IF EXISTS PARTITION (prov='henan' , city='kaifeng');
  5. 加載數據到分區

    $hive>load data local inpath '/home/centos/1.txt' into table par1 partition(prov='henan' , city='kaifeng') ;
    # -f :force 覆蓋原有文件
    $>hdfs dfs -put -f par1.txt /user/hive/warehouse/big12.db/par1/prov=henan/city=kaifeng
  6. 複製數據到指定分區下

    # 分區表在嚴格模式下,必須至少指定一個靜態分區。
    $hive>insert into par1 partition(prov='hebei' , city) select 1 , 'tom' , 'cc';       
    # 指定了兩個都是動態分區(錯誤的 )
    $hive>insert into par1 partition(prov , city) select 10,'tom10',56,'Liaoning' , 'DL';
    
    # 設置動態分區非嚴格模式
    $hvie>set hive.exec.dynamic.partition.mode=nonstrict ;
  7. 動態分區模式

    動態分區有嚴格和非嚴格之分。嚴格模式下插入數據時至少有一個是靜態分區,非嚴格模式下均可以是動態分區。

    # 非嚴格
    $hive>set hive.exec.dynamic.partition.mode=nonstrict ; 
    # 嚴格
    $hive>set hive.exec.dynamic.partition.mode=strict ;
  8. 關閉動態分區

    # true | false
    $hive>set hive.exec.dynamic.partition=true ;

十、桶表

桶表原理就是hash,針對文件進行劃分。分區表是針對目錄劃分。對記錄須要單條檢索的時候可使用桶表。分桶的大小推薦咱們每一個buck數據量是blocksize的2倍。桶表不能使用load指令進行數據的加載。經過表數據複製方式實現桶表數據存儲。

  1. 建立桶表

    $hive>CREATE TABLE buck1(
    id int ,
    name string
    )
    CLUSTERED BY (id) INTO 3 BUCKETS
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    lines terminated by  '\n';
  2. 複製數據到桶表

    $hive>insert into buck1 select id,name from par1 ;

十一、文件格式

在hive中列存儲格式的文件具備較好的性能,面向列的存儲將同一列的值連續存儲起來,當進行投影(查詢若干字段,而不是所有字段)查詢時,能夠發揮磁盤的線性讀寫,默認是文本文件。

  1. textfile

    文本文件,默認模式。

    # 建立表指定格式
    $hive>CREATE TABLE .. STORED AS textfile ;
    
    # 修改表,指定格式
    $hive>ALTER TABLE .. [PARTITION partition_spec] SET FILEFORMAT textfile ;

  2. sequencefile

    序列文件,key-value存儲方式。能夠指定壓縮形式,有block、record、none。

  3. rcfile

    Record Columnar File,kv存儲,相似於sequencefile,將數據文件水平切割多個組。若干group存放在一個hdfs中,先保存全部行的第一列,第二列,以此類推。該文件可切割.能夠跳過不相關部分,更快獲得數據,成本更低。

    經過CTAS方式建立RCFile文件:

    $hive>create table rcfile1 stored as rcfile as select * from buck1 ;
  4. orc

    Optimized Row Columnar,RCFile加強版。支持的大數據塊(256M)。和rcfile的不一樣是使用特殊的編碼器感知數據類型,並依據不一樣的類型進行壓縮優化。同時也存儲了基於column的一些基本的統計(MIN, MAX, SUM, and COUNT)信息,還有輕量級索引。支持範圍較窄,只有hive和pig。

    $hive>create table orc1 stored as orc as select * from buck1;
  5. parquet

    相似於orc,相對於orc文件格式,hadoop生態系統中大部分工程都支持parquet文件。

    $hive>create table parquet1 stored as parquet as select * from buck1;

十二、事務支持

hive對事務的支持是有限制的,須要桶表+事務屬性+orc文件+事務控制。

  1. 建立orc文件格式的桶表,並指定支持事務

    $hive>create table tx(id int ,name string , age int)
    clustered by (id) into 2 buckets
    stored as orc TBLPROPERTIES('transactional'='true');
  2. 設置hive相關的屬性支持

    $hive>SET hive.support.concurrency = true;
    SET hive.enforce.bucketing = true;
    SET hive.exec.dynamic.partition.mode = nonstrict;
    SET hive.txn.manager = org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
    SET hive.compactor.initiator.on = true;
    SET hive.compactor.worker.threads = 1;
  3. 複製數據都事務表

    $hive>insert into tx select id , name , 18 from par1 ;

1三、查詢

  1. 去重

    指定多個reduce有效,key進行hash處理,key只要相同,hash到同一reduce,所以可使用多個reduce實現去重。

    $hive>set mapreduce.job.reduces= 3 ;
    $hive>select distinct id ,name from par1 ;
  2. 排序

    hive實現全排序有兩種方式,order by方式和sort by方式。order by使用reduce實現,致使數據傾斜。

    • order by

      $hive>select * from par1 order by id desc ;
    • sort by

      部分排序,在reduce端按照指定的key進行部分排序。如下語句在每一個reduce內按照sal降序排列。

      $hive>create table tmp as select * from emp2 sort by sal desc ;
    • distribute by

      按照指定字段進行分發,就是分區過程,該語句須要在sort by以前。

      $hive>create table tmp3 as select * from emp2 distribute by dep sort by sal desc;
    • cluster by

      若是distribute by 和 sort by使用的是相同的字段,則能夠直接使用cluster by。

      $hive>create table tmp4 as select * from emp2 cluster by sal desc ;

    • 對氣溫數據進行全排序

      年份升序全排序,年分內氣溫值降序排列。

      1. 建立表

        $hive>CREATE TABLE temps(
        year int,
        temp int
        )
        ROW FORMAT DELIMITED
        FIELDS TERMINATED BY ' '
        lines terminated by '\n';

      2. 加載數據

        $hive>load data

      3. 執行查詢

        $hive>create table tmp5 as select * from temps distribute by case when year < 1930 then 0 when year > 1960 then 2 else 1 end sort by year asc , temp desc ;

  • 鏈接查詢

    鏈接查詢實現方式能夠是map端鏈接,也能夠reduce端鏈接。

    hive自動轉換鏈接到map端鏈接使用以下屬性控制:

    # 是否啓用自動轉換成map端控制
    $hive>set hive.auto.convert.join=true ;
    
    #無條件任務鏈接自動轉換
    $hive>set hive.auto.convert.join.noconditionaltask=true ;
    
    #
    $hive>set hive.auto.convert.join.noconditionaltask.size=100000000 ;
    • map端鏈接

      map端鏈接不須要reduce,天然也沒有shuffle,性能好。使用場景是小表+大表,小表能夠載入內存。

      $hive>select a.*,b.* FROM custs a ,orders b WHERE b.cid = a.id and b.id < 10;
    • reduce鏈接

      若是大表 + 大表方式,須要使用reduce端鏈接。

  • 傾斜控制

    hive對傾斜鏈接的控制。

    # 是否啓用傾斜鏈接控制,默認false。
    $hive>set hive.optimize.skewjoin=true ;
    
    # 鏈接key超過100000行。
    $hive>set hive.skewjoin.key=100000;
    
    $hive>set hive.optimize.skewjoin=true ;
    
    # 控制傾斜鏈接的map端任務數
    $hive>set hive.skewjoin.mapjoin.map.tasks=10000 ;
    
    # 控制切片大小
    $hive>set hive.skewjoin.mapjoin.min.split=33554432 ;

1四、hiveserver2服務器

hiveserver2是相似於mysql的服務器,接受jdbc的鏈接請求,轉換成mr程序,在hadoop上運行。對於其餘的開發人員就能夠經過jdbc方式進行訪問了。

14.1 啓動hiveserver2服務器

# 後臺啓動hiveserver2服務器
$>/soft/hive/bin/hiveserver2 &

14.2 驗證是否成功

$>netstat -anop |grep 10000

14.3 啓動beeline命令行

# 啓動
$>/soft/hive/bin/beeline

# 查看幫助
$beeline>!help

# 鏈接到庫
$beeline>!connect jdbc:hive2://localhost:10000/big12 ;

# 執行查詢
$beeline>select * from par1 ;

14.3 使用jdbc客戶端訪問hiveserver

@Test
public void testSelect() throws Exception {
  String driver = "org.apache.hive.jdbc.HiveDriver" ;
  Class.forName(driver) ;
  Connection conn = DriverManager.getConnection("jdbc:hive2://s101:10000/big12");
  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery("select * from par1 sort by age desc") ;
  while(rs.next()){
    int id = rs.getInt("id") ;
    String name = rs.getString("name") ;
    String prov= rs.getString("prov") ;
    String city = rs.getString("city") ;
    System.out.printf("%d,%s,%s,%s\r\n" , id ,name , prov , city);
  }
  rs.close();
  conn.close();
}

1五、導出導入表

15.1 導出

導出表包含表結構和數據。可用於數據遷移。

# 導出指定表的內容到hdfs目錄下
$hive>export big12.par1 to '/user/centos/par1' ;

運行結果以下:

_metadata是元數據,包含的是表結構信息。

15.2 導入

導入時將導出的數據目錄和表機構導入到當前的數據庫中。

# 指定目錄記錄
$hive>import from '/user/centos/par1' ;

15.3 導出hive數據到本地目錄

$hive>INSERT OVERWRITE LOCAL DIRECTORY '/home/centos/par1'
SELECT * FROM par1 ;

1六、壓縮控制

16.1 中間結果的壓縮處理

中間文件是指在mr之間產生的輸出文件是否進行壓縮。

# 指定是否壓縮
$hive>set hive.exec.compress.intermediate=true ;
# 使用的壓縮算法
$hive>SET hive.intermediate.compression.codec=org.apache.hadoop.io.compress.SnappyCodec ;

16.2 控制job的輸出壓縮

# 指定是否壓縮
$hive>SET hive.exec.compress.output=true ;
# 使用的壓縮算法
$hive>SET mapred.output.compression.codec=org.apache.hadoop.io.compress.Lz4Codec ;

1七、視圖

視圖是虛表,能夠理解爲一些複雜查詢語句的別名,使用表的方式來對待。

17.1 建立視圖

$hive>create view v1 as

17.2 使用視圖

$hive>select * from v1 ;

17.3 刪除視圖

$hive>drop view if exists v1 ;

1八、虛列

hive自動爲表分配的列,建表語句中不須要指定這些列的。

$hive>select id ,INPUT__FILE__NAME ,BLOCK__OFFSET__INSIDE__FILE ,ROW__OFFSET__INSIDE__BLOCK from par1 ;

1九、高級聚合函數

聚合函數和group by配合使用。select子句中的字段要麼是聚合函數,或者是分組字段。

19.1 常規聚合

# ok
$hive>select dep , avg(sal) from emp2 group by dep ;
# error ,name不是分組字段,也不是聚合函數
$hive>select name , dep , avg(sal) from emp2 group by dep ;

19.2 高級函數

19.2.1 高級聚合函數

在group by以後經過grouping sets指定各類聚合條件。group by指定哪些字段參與聚合,grouping sets指定具體按照哪些組合進行聚合。

  1. grouping sets

    $hive>select dep,age,avg(sal) from emp2 group by dep,age grouping sets(dep,age, (dep,age)) ;
  2. cube

    $hive>select dep,age,avg(sal) from emp2 group by age , dep with cube ;
    $hive>select dep,age,avg(sal) from emp2 group by age , dep grouping sets(dep,age,(dep,age),()) ;
  3. rollup

    $hive>select dep,age,substr(name,1,1) ,avg(sal) from emp2 group by dep,age,substr(name,1,1) with rollup ;
    
    $hive>select dep,age,substr(name,1,1) ,avg(sal) from emp2 group by dep,age,substr(name,1,1) grouping sets((dep,age,substr(name,1,1)),(dep,age),(dep),()) ;
19.2.2 分析函數

聚合函數不能將常規字段進行統計輸出,輸出的只能是分組字段和聚合函數表達式。分析函數能夠將聚合字段和每條記錄組合輸出。有開窗函數,開窗函數能夠按照行開窗和範圍開窗。

  • rows between .. and ..

    行開窗,查行數。

  • range between ..add ..

    範圍開窗,對字段值進行加減後的範圍肯定數據的範圍。

語法結構:

function (arg1,..., argn) OVER ([PARTITION BY <..>] [ORDER BY <..>] [<window_clause>])
  1. 分區

    # 使用分區
    $hive>select id,name,age,salary , avg(salary) over (partition by dep) from emp2;
    
    $hive>select id,name,age,salary , avg(salary) over (partition by dep , age) from emp2 ;

  2. 排序

    # first_value
    $hive>select id,name,age,sal,first_value(sal) over (order by id desc) from emp2  ;
    
    # last_value, 開窗範圍是前導全部行到當前行結束
    # rows between 指定開窗範圍
    # UNBOUNDED PRECEDING 前導全部行
    # current row 當前行
    # UNBOUNDED following 後續全部行
    $hive>select id,name,age,sal,last_value(sal) over (order by id desc rows BETWEEN UNBOUNDED PRECEDING AND current row) from emp2 ;
    
    # 行開窗範圍 :前導全部行和後續全部行==所有數據
    $hive>select id,name,age,sal,last_value(sal) over (order by id desc rows BETWEEN UNBOUNDED PRECEDING AND unbounded following) from emp2 ;
    
    # 行開窗範圍 : 上一行,當前行和下一行三者之間的最後一個值。
    $hive>select id,name,age,sal,last_value(sal) over (order by age desc rows BETWEEN 1 PRECEDING AND 1 following) from emp2 ;
    
    # 範圍開窗範圍 : 上一行,當前行和下一行三者之間的最後一個值。
    $hive>select id,name,age,sal,last_value(sal) over (order by age desc range BETWEEN 1 PRECEDING AND 1 following) from emp2 ;
    
    # max()
    $hive>select id,name,dep,sal,MAX(sal) OVER (PARTITION BY dep ORDER BY sal rows BETWEEN 3 PRECEDING AND 3 following) as t2 from emp2;
    
    # rank() ,有縫排名
    $hive>select id,name,dep,sal,rank() OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;
    
    # dense_rank() ,有縫排名
    $hive>select id,name,dep,sal,dense_rank() OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;
    
    # percent_rank() ,百分比排名
    $hive>select id,name,dep,sal,percent_rank() OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;
    
    # Ntile()分桶函數 ,10條記錄,3個桶 ,分配結果: 4,3,3
    $hive>select id,name,dep,sal,ntile(4) OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;
    
    # lag()前導查詢 lag(字段,前導的個數,默認值),前導個數不能是負數。
    $hive>select id,name,dep,sal,lag(sal,2,'-1000') OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;
    
    # lead()後向查詢 lag(字段,前導的個數,默認值)
    $hive>select id,name,dep,sal,lead(sal,2,'-1000') OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;
    
    # row_number() 取行號。
    $hive>select id,name,dep,sal,row_number() OVER (PARTITION BY dep ORDER BY sal desc) as t2 from emp2;

20、修改hive本地模式

hive的本地模式是指使用hadoop的本地模式來運行。

# 開啓自動切換hive的本地模式
$hive>set hive.exec.mode.local.auto=true;
$hive>set hive.exec.mode.local.auto.inputbytes.max=134217728 ;
$hive>set hive.exec.mode.local.auto.input.files.max=4 ;

$hive>set mapreduce.input.fileinputformat.split.maxsize=50;
$hive>set mapreduce.input.fileinputformat.split.minsize=50
$hive>set dfs.blocksize ;

2一、索引

hive中索引本質上一種表,元數據庫中查詢的tbls表結果是index_table。

21.1 建立索引

建立索引後,索引表時空的,沒有所數據。

$hive>CREATE INDEX idx_emp2_name ON TABLE emp2(name) AS 'COMPACT' WITH DEFERRED REBUILD;

21.2 查看元數據中的索引表

$mysql>select * from big12_hive.tbls ;

查詢結果如圖:

21.3 重建索引值

# 重建索引
$hive>ALTER INDEX idx_emp2_name ON emp2 REBUILD ;

# 查看索引表
$hive>select * from idx_emp2_name ;

2二、合併小文件

對太小文件不要單獨啓動一個mapper,默認狀況下,每一個文件至少對應一個map。

# 每一個Map切片的最大值
$hive>set mapred.max.split.size=256000000;

# 每一個節點的切片的最小值
$hive>set mapred.min.split.size.per.node=1;

# 每一個機架的切片的最小值
$hive>set mapred.min.split.size.per.rack=1;

# hive的輸入格式
$hive>set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;

hadoop能夠經過CombineTextinputFormat解決大量小文件問題:

// 避免產生大量的mapper
job.setInputFormatClass(CombineTextInputFormat.class);
// 設置路徑過濾,哪些輸入路徑能夠訪問
CombineTextInputFormat.setInputPathFilter(job , MyPathFilter.class);
// 設置組合文件輸入格式的切片的最大值,默認Long.MAX_VALUE。
CombineTextInputFormat.setMaxInputSplitSize(job , 700);

2三、hive的解釋執行計劃

explain指令能夠翻譯sql語句對應的mr執行過程。

$hive>explain select distinct dep,age from emp2

2四、hive表採樣

# 
$hive>SELECT * FROM emp TABLESAMPLE(BUCKET 3 OUT OF 32 ON rand()) s;

# 將id分紅16個桶,取第3個桶的數據。
$hive>SELECT * FROM emp TABLESAMPLE(BUCKET 3 OUT OF 16 ON id) ;
# 從開始按比例採樣
$hive>SELECT * FROM emp TABLESAMPLE(50 PERCENT) s ;
# 從開始位置採樣100M
$hive>SELECT * FROM emp TABLESAMPLE(100M) s; 
# 從開始行採樣 10行
$hive>SELECT * FROM emp TABLESAMPLE(10 ROWS);
相關文章
相關標籤/搜索