Hive 視圖 索引 動態分區裝載數據

視圖

創建視圖
create view v_emp AS select t.name, t.age, t.addr from t_emp;

刪除視圖
drop view if exists v_emp;


索引

創建索引
create index t_emp_index
    on table t_emp (name)
    as ‘org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler’
    with deferred rebuild in table t_index;

顯示索引
查看索引

重建索引
alter index t_emp_index on t_emp rebuild;

刪除索引
drop index if exists t_emp_index on t_emp ;


普通裝載數據(分區需指定)

  • 從文件中裝載數據

    hive>LOAD DATA [LOCAL] INPATH ‘…’ [OVERWRITE] INTO TABLE t_employee [PARTITION (…)];

  • 通過查詢表裝載數據

    hive>INSERT OVERWRITE TABLE t_emp PARTITION (…) SELECT * FROM xxx WHERE xxx

  • 批量插入

    hive>FROM t_emp
                INSERT OVERWRITE TABLE t_test PARTITION (…) SELECT … WHERE …
                INSERT OVERWRITE TABLE t_test PARTITION (…) SELECT … WHERE …
                INSERT OVERWRITE TABLE t_test PARTITION (…) SELECT … WHERE …

    batch_insert
    shows


動態分區裝載數據(分區不需指定)

  • 若沒有開啓動態分區只支持以下寫法

    hive>INSERT OVERWRITE TABLE t_test PARTITION (country=’china’, city=’chengDu’)
                  SELECT name, age, addr
                   FROM t_emp
                 WHERE t_emp.country = ‘china’
                     AND t_emp.city = ‘chengDu’;

  • 開啓動態分區支持

    hive>set hive.exec.dynamic.partition=true; // 開啓動態分區
    hive>set hive.exec.dynamic.partition.mode=nostrict; // 設置爲非嚴格模式
    hive>set hive.exec.max.dynamic.partitions.pernode=1000; // 最大動態可分區數
    hive> insert overwrite table t_dynamic partition(country, city) select name, age, addr, country, city from t_emp;

    dynamic
    dynamic2


數據導出

  • 通過hdfs方式導出

    到本地
    hive> dfs -copyToLocal /user/hive_remote/warehouse/t_dynamic /home/tt;

    hive> dfs -get /user/hive_remote/warehouse/t_dynamic /home/t_dynamic;
    這裏寫圖片描述

    到hdfs
    hive> dfs -cp /user/hive_remote/warehouse/t_dynamic /tmp/t_dynamic;
    export hdfs

  • 使用DIRECTORY

    hive> insert overwrite [local] directory ‘/home/tt2’ select * from t_dynamic;
      加local到本地/默認到hdfs

    這裏寫圖片描述

發佈了59 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章