Contenu connexe Similaire à Hiveハンズオン (20) Plus de Satoshi Noto (11) Hiveハンズオン19. その他のコマンド
• hadoop fs -lsr <path>
• hadoop fs -cat <path>
• hadoop fs -mkdir <path>
• hadoop fs -rm <path>
• hadoop fs -rmr <path>
• hadoop fs -help
http://hadoop.apache.org/common/docs/current/file_system_shell.html 参照
25. select
kカラムで並べ替える
select * from hoge order by k limit
10;
group byやcountも普通に使える
select k, count(k) from hoge
group by k order by k limit 10;
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select 参照
27. データ型
• primitive
• TINYINT - 1 byte integer
• SMALLINT - 2 byte integer
• INT - 4 byte integer
• BIGINT - 8 byte integer
• FLOAT - single precision
• DOUBLE - Double precision
• BOOLEAN - TRUE/FALSE
• STRING - 文字列
• complex
• Maps (key-value tuples)
• Arrays (indexable lists)
• Structs
https://cwiki.apache.org/confluence/display/Hive/Tutorial#Tutorial-TypeSystem 参照
30. drop table
create tableするとディレクトリが出来る
create table fuga (k int, v string);
dfs -lsr /user/hive;
load data localするとローカルのファイルがHDFSにコピーされる
load data local inpath './examples/files/kv1.txt'
overwrite into table fuga;
dfs -lsr /user/hive;
drop tableするとディレクトリが削除される
drop table fuga;
dfs -lsr /user/hive;
31. insert overwrite
もう一度fugaテーブルを作りhogeテーブルの中身をfugaテーブルにinsertする
create table fuga (k int, v string);
insert overwrite table fuga select * from hoge limit 10;
select * from fuga;
fugaテーブルの実体もファイル
dfs -lsr /user/hive;
dfs -cat /user/hive/*/fuga/*;
hive0.7まではoverwriteは必須オプション。そのため、insertするとテーブル
の中身が総入れ替えとなる
0.8でinsert intoが実装され、追記ができるようになるらしい
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML 参照
34. パーティションの利用1
create tableする際にpartitioned byを足すことでパーティションを利用
するテーブルを作成できる
create table piyo (k int, v string) partitioned by (p string);
dfs -lsr /user/hive;
load data する際にパーティションを指定する
load data local inpath './examples/files/kv1.txt' overwrite
into table piyo partition (p='p1');
dfs -lsr /user/hive;
load data local inpath './examples/files/kv2.txt' overwrite
into table piyo partition (p='p2');
dfs -lsr /user/hive;
37. 物理データモデル
• テーブル
• HDFS上のディレクトリ
• スキーマ情報はMetastoreに保持
• パーティションの利用は任意
• パーティション
• テーブルのディレクトリのサブディレクトリ
• キーは複数指定できる
• partitioned by (id string, date string)
• ファイル
• HDFS上のファイル
• バケットを利用してファイル毎の内容を特定のカラムをキーにしてまとめることができる
40. 正規表現も利用できる
CREATE TABLE accesslog(
host STRING, identity STRING, user STRING, time STRING, request STRING,
status STRING, size STRING, referer STRING, agent STRING)
ROW FORMAT SERDE
'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
"input.regex" = "([^ ]*) ([^ ]*) ([^ ]*) (-¦[[^]]*]) ([^ "]*¦"[^"]*") (-¦[0-9]*) (-¦
[0-9]*)(?: ([^ "]*¦"[^"]*") ([^ "]*¦"[^"]*"))?",
"output.format.string" = "%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s"
)
STORED AS TEXTFILE;
load data local inpath './examples/files/apache.access.log' into table accesslog;
load data local inpath './examples/files/apache.access.2.log' into table
accesslog;
select * from accesslog;
43. 外部テーブル
create tableする際にexternal句を指定すると外部テーブルを作成できる
create external table ext (k int, v string) location '/user/cloudera/
ext';
dfs -lsr /user/cloudera;
extディレクトリにファイルを追加すると参照できるようになる
load data local inpath './examples/files/kv1.txt' overwrite into
table ext;
select count(*) from ext;
dfs -lsr /user/cloudera;
外部テーブルはdrop tableしてもファイルは削除されない
drop table ext;
dfs -lsr /user/cloudera;
44. join
普通に使える
select t1.k, t1.v, t2.v from hoge t1
join piyo t2 on (t1.k = t2.k) limit
20;
where句の後ろに複数のテーブル名を書く方法
はサポートしていない
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins 参照