【フィーチャーエンジニアリング】Hiveフィーチャーエンジニアリングデータ処理常用コマンドレコード

2184 ワード

(1)テーブルの作成、テーブルの表示、テーブルの削除、パーティションへのデータの挿入
create table if not exists table_name;

desc table_name;

drop table if exists table_name;

insert into behavior_log_basic_100291674 partition(dt='20190705',ts='5') values ();

(2)パーティションの表示
Show partitions table_name;

(3)1行分のデータを複数行に分割する
trans_array(4, ',', cookie, ideaid, tag, device, videopref) as (cookie, ideaid, tag, device, videopref_split);

注意:不変のkey値は4つあります.(前の4つ、cookie、ideaid、tag、device、','を区切り記号として残りのフィールドの内部を分割します.ここではvideopref)変換後:
cookie, ideaid, tag, device, videopref1
cookie, ideaid, tag, device, videopref2
cookie, ideaid, tag, device, videopref3
(4)文字列の先頭、後ろのスペースを削除
先頭スペース(LTrim)、後続スペース(RTrim)、または先頭スペースと後続スペース(Trim)を持たない文字列のコピーを返します.
LTrim(string)
RTrim(string)
Trim(string)
(5)NVL(式1,式2)
式1がNULLの場合、NVLは式2の値を返し、そうでない場合は式1の値を返します.この関数の目的は、空の値(null)を実際の値に変換することです.
(6)パケットソートtop n
select course, score from
(
select course, score, row_number() over(partition by cource order by score desc) as n
form lesson
) t1
where t1.n <= 2;

 
row_number()over(partition by cource order by score descとは、カリキュラムでグループ化され、成績順にソートされ、各グループのデータに行番号タグを付け、1から開始することを意味します.
(7)特殊文字を置換
regexp_replace(query,'','')
queryフィールドの改行文字をスペース、
(8)並べ替え
Order by ...デフォルトの昇順、descは降順
(9)ファジイクエリ
Where column like '%word%'
(10)表名の変更
alter table name rename to new_name;

(11)テーブルのサブパーティションの削除
alter table DG_query_log_on_odps_transfer drop partition (dt=20190725);

(12)多条件文
select prediction_score, 
(CASE WHEN prediction_score>=0.6 THEN 2

WHEN prediction_score<=0.6 and prediction_score>0.5 THEN 1

ELSE 0 END) as level
 from table;

(13)instr(title, "query")
指定した文字列を1つの文字列で検索します.指定した文字が見つかった場所を返し、見つからなかったら0を返します.
(14)split_part(フィールド、区切り記号、part)
(15)複数行を1行にまとめる
concat_ws(',str 1,str 2)は2文字接続されており、区切り文字を一度に指定できます
concat(str 1,str 2)nullのいずれかがある場合はnullを返します.