Hiveでパーティション・テーブルをコピーする方法(データを含む)

12331 ワード

Hiveではレプリケーション・テーブルの要件が発生する場合があります.レプリケーション・テーブルとは、レプリケーション・テーブルの構造とデータを指します.
非パーティション・テーブルの場合は簡単です.
CREATE TABLE new_table AS SELECT * FROM old_table;

では、パーティションテーブルなら?
まず考えられる方法は、
まず1枚とold_を作成しますtable構造が同じnew_table、パーティションを含む;CREATE TABLE new_をご利用いただけますtable LIKE old_table;
次にダイナミックパーティションを使用してold_tableのデータINSERTからnew_テーブルにあります.
この方法はもちろんいいですが、一番速くないかもしれません.
実際には、
  • CREATE TABLE new_table LIKE old_table;
  • hadoop fs-cpコマンドを使用してold_table対応のHDFSディレクトリのフォルダを全てnew_にコピーtable対応HDFSディレクトリの下;
  • MSCK REPAIR TABLE new_を使用table;新しいテーブルのパーティションメタデータを修復します.

  • 例えば、1つのパーティションテーブルt 1があり、2つのパーティションしかなく、各パーティションには以下のようなデータがある.
    hive> show partitions t1;
    OK
    pt=2015-09-11
    pt=2015-09-12
    Time taken: 0.11 seconds, Fetched: 2 row(s)
    hive> desc t1;
    OK
    id                      string                                      
    pt                      string                                      
                     
    # Partition Information          
    # col_name              data_type               comment             
                     
    pt                      string                                      
    Time taken: 0.123 seconds, Fetched: 7 row(s)
    hive> select * from t1;
    OK
    X       2015-09-11
    Y       2015-09-12
    Time taken: 0.095 seconds, Fetched: 2 row(s)
    hive> 
    

    同じテーブル構造の新しいテーブルt 2を作成する
    hive> create table t2 like t1;
    OK
    Time taken: 0.162 seconds
    hive> desc t2;
    OK
    id                      string                                      
    pt                      string                                      
                     
    # Partition Information          
    # col_name              data_type               comment             
                     
    pt                      string                                      
    Time taken: 0.139 seconds, Fetched: 7 row(s)
    hive> show partitions t2;
    OK
    Time taken: 0.082 seconds
    

    hadoop fs-cpコマンドを使用して、t 1対応HDFSディレクトリのすべてのフォルダをt 2対応HDFSディレクトリの下にコピーします.
    [liuxiaowen@dev ~]$ hadoop fs -cp /hivedata/warehouse/liuxiaowen.db/t1/* /hivedata/warehouse/liuxiaowen.db/t2/
    [liuxiaowen@dev ~]$ hadoop fs -ls /hivedata/warehouse/liuxiaowen.db/t2/
    Found 2 items
    drwxr-xr-x   - liuxiaowen liuxiaowen          0 2015-09-11 17:17 /hivedata/warehouse/liuxiaowen.db/t2/pt=2015-09-11
    drwxr-xr-x   - liuxiaowen liuxiaowen          0 2015-09-11 17:17 /hivedata/warehouse/liuxiaowen.db/t2/pt=2015-09-12
    

    Hive用にMSCK REPAIR TABLE t 2を使用する.新しいテーブルt 2のパーティションメタデータを修復する.
    hive> show partitions t2;
    OK
    Time taken: 0.082 seconds
    hive> MSCK REPAIR TABLE t2;
    OK
    Partitions not in metastore:    t2:pt=2015-09-11        t2:pt=2015-09-12
    Repair: Added partition to metastore t2:pt=2015-09-11
    Repair: Added partition to metastore t2:pt=2015-09-12
    Time taken: 0.249 seconds, Fetched: 3 row(s)
    hive> show partitions t2;
    OK
    pt=2015-09-11
    pt=2015-09-12
    Time taken: 0.068 seconds, Fetched: 2 row(s)
    hive> select * from t2;
    OK
    X       2015-09-11
    Y       2015-09-12
    Time taken: 0.123 seconds, Fetched: 2 row(s)
    hive> 
    

    新しいテーブルt 2はコピーされ、t 1と同じテーブル構造、パーティション構造、パーティションおよびデータを有する.
    リファレンス
    http://lxw1234.com/archives/2015/09/484.htm