[ハイブ]テーブルデータの重複排除、ハイブクエリ結果のローカルまたはhdfsの保存

1つ、重複排除されたハイブテーブルデータ

1.テーブル構造をコピーします

CREATE TABLE <new_table> LIKE <old_table>;

2.重複排除後にデータを挿入します

insert overwrite table <new_table>(
select t.id, t.local_path
from (
select
id, local_path, row_number() over(distribute by id sort by local_path) as rn
from <old_table>
) t where t.rn=1
);

構造は次のとおりです

insert overwrite table <new_table> (
select <字段>
from (
select <字段>, row_number() over(distribute by <有重复的字段> sort by <重复字段的排列根据字段>) as rn
from <old_table>
) t where t.rn=1
);

新しいテーブルを作成せずに元のテーブルに直接挿入して直接更新することもできます

3.パーティションテーブルの重複排除 

#hive数据去重
      #获取表头
      fieldsTmp=$(hive -e "SET hive.cli.print.header=true;select * from ${dbname}.${table_name} limit 0;" | sed -e "s/\t/,/g;s/data\.//g" | grep -v "WARN")
      fields2=${fieldsTmp//,${table_name}.dt/}
      fields=${fields2//${table_name}./}
   hive -e "
   use $dbname;
   insert overwrite table $table_name partition(dt=$date_slice) select $fields from (select $fields,row_number() over(distribute by $tab_primarykey sort by $key_code desc)as rn from $table_name)t where t.rn=1"

サンプルコードで割り当てる必要のある変数は次のとおりです。

$ {dbname}:データベース名

$ {table_name}:テーブル名

$ {date_slice}:パーティション値

$ {tab_primarykey}:重複するフィールドがあります

$ {key_code}:繰り返されるフィールドの配置はフィールドに基づいています

fields2 = $ {fieldsTmp //、$ {table_name} .dt /}のdtはパーティションフィールドです

2.スクリプト内のハイブテーブルのフィールド名を取り出します

#存储到文本
hive -e "SET hive.cli.print.header=true;select * from dbname.tablename limit 0;" | sed -e "s/\t/,/g;s/data\.//g" | grep -v "WARN" > fileds.csv

#赋值给变量
fields=$(hive -e "SET hive.cli.print.header=true;select * from dbname.tablename limit 0;" | sed -e "s/\t/,/g;s/data\.//g" | grep -v "WARN")


ハイブテーブルのフィールド名をコンマで区切って取得します

3つ目は、ハイブクエリの結果をローカルまたはhdfsに保存することです。

ハイブクエリの結果をローカルに保存する

hive -e "use test;insert overwrite local directory '/tmp/score' ROW FORMAT DELIMITED FIELDS TERMINATED BY',' select * from student"

hdfsに保存するには、上記のコマンドからローカルを削除するだけです。
 

おすすめ

転載: blog.csdn.net/qq_44065303/article/details/112786504