Hive へのデータの一括インポート
ブログ[ビッグデータ] Hive テーブルに複数のデータを挿入するでは、Hive テーブルにデータを挿入するいくつかの方法を簡単に紹介しました。ただし、多くの場合、データを 1 つずつ挿入するのではなく、バッチでインポートします。この記事では、Hive にデータを一括インポートするいくつかの方法をより包括的に紹介します。
1. ローカルファイルシステムからデータをロード(ロード)します。
load data [local] inpath '路径' [overwrite] into table 表名 [partition (分区字段=值,…)];
overwrite
: テーブル内の既存のデータを上書きすることを意味し、それ以外の場合は追加を意味します。- このロード方法はデータのコピーです。
(1) テーブルを作成します。
hive (default)> create table student(id string, name string)
row format delimited fields terminated by '\t';
(2) ローカルファイルをHiveにロードします。
hive (default)> load data local inpath '/opt/module/datas/student.txt'
into table default.student;
2. HDFS ファイルシステムからデータをロード(ロード)します。
HDFS ファイル システムからテーブルへのデータのロードは、実際にはファイルの移動操作であり、データは事前に HDFS ファイル システムにアップロードされている必要があります。
(1) ファイルを HDFS にアップロードします (Linux ローカル/opt/module/datas/student.txt
ファイルを/user/victor/hive
ディレクトリにアップロードします)。
hive (default)> dfs -put /opt/module/datas/student.txt /user/victor/hive;
(2) HDFS ファイル システムからテーブルにデータをロードします。
hive (default)> load data inpath '/user/victor/hive/student.txt'
into table default.student;
3. as select を通じてテーブルにデータを挿入します。
hive (default)> create table if not exists student3 as select id, name from student;
4. insert into を使用してテーブルにデータを挿入します。
insert into table test [partition(partcol1=val1, partcol2=val2 ...)] select id,name from student;
insert into
: データを追加することによってテーブルまたはパーティションにデータを挿入します。元のデータは削除されません。
insert overwrite table test [partition(partcol1=val1, partcol2=val2 ...)] select id,name from student;
insert overwrite
: テーブル内の既存のデータを上書きします。
(1) パーティションテーブルを作成します。
hive (default)> create table student(id string, name string)
partitioned by (month string)
row format delimited fields terminated by '\t';
(2) 基本的なデータ挿入。
hive (default)> insert into table student partition(month='201801')
values('1004','wangwu');
(3) 基本モード挿入 (単一テーブルに基づくクエリ結果)。
hive (default)> insert overwrite table student partition(month='201802')
select id, name from student where month='201801';
(4) マルチ挿入モード (ソース テーブルを 1 回スキャンするだけで、複数の素な出力を生成できます)。
hive (default)> from student
insert overwrite table student partition(month='201803')
select id, name where month='201801'
insert overwrite table student partition(month='201804')
select id, name where month='201801';
5. 場所別
データ ファイルをlocation
指定された HDFS ディレクトリに直接アップロードします。
(1) テーブルを作成し、HDFS 上の場所を指定します。
hive (default)> create external table student(id int, name string)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student';
(2) データを HDFS にアップロードします。
hive (default)> dfs -mkdir -p /user/hive/warehouse/student;
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student;
(3) データをクエリします。
select * from student;