そごうソフトウェア研究所

SOA、開発プロセス、ITアーキテクチャなどについて書いています。Twitterやってます@rsogo

Hiveでファイルからデータをロードする

前記事はこれ。 begirama.hatenablog.com

begirama.hatenablog.com

読み込むファイルを作成します

今回はタブ区切りの次のようなファイルを作成します。

1    aaa
2   bbb
3   ccc

読み込む先のテーブルを作成します

項目の区切りをタブ、行末を改行として、数値型のidと、文字列型のnameを持つテーブルを作成します。項目の区切りとかを、ロード時じゃなくて、テーブル作成時に指定できるのが、RDBMSとは違う感じ。

hive> create table hoge(id int, name string) row format 
> delimited fields terminated by '\t' 
> lines terminated by '\n';
create table hoge(id int, name string) row format delimited fields terminated by '\t' lines terminated by '\n'
OK
Time taken: 0.138 seconds

load dataでさっき作ったファイルを読み込みます

hive> load data local inpath '/Users/rsogo/work/hive/sample.txt' into table hoge;
load data local inpath '/Users/rsogo/work/hive/sample.txt' into table hoge
Loading data to table testdb.hoge
Table testdb.hoge stats: [numFiles=1, totalSize=19]
OK
Time taken: 1.659 seconds

ロードしたデータをselectしてみます。項目名が出るように設定して・・・

hive> set hive.cli.print.header=true;
set hive.cli.print.header=true

selectします。

hive> select * from hoge;
select * from hoge
OK
hoge.id hoge.name
1   aaa
2   bbb
3   ccc
NULL    NULL
Time taken: 0.118 seconds, Fetched: 4 row(s)

あれ、最後の空行が1レコードとして登録されてる。