1.hive-testbench 簡介
hive-testbench 是一個(gè)用于測(cè)試和基準(zhǔn)測(cè)試 Apache Hive 的工具集扣猫。它提供了一系列的測(cè)試數(shù)據(jù)集和查詢樣例替裆,用于評(píng)估和比較 Hive 在不同配置和環(huán)境下的性能窖式。hive-testbench 的主要目標(biāo)是模擬真實(shí)的大規(guī)模數(shù)據(jù)集和復(fù)雜查詢場(chǎng)景,以幫助用戶評(píng)估和優(yōu)化 Hive 的性能食店。
hive-testbench 包含了 TPC-DS 和 TPCH 的各種不同規(guī)模的數(shù)據(jù)集,從幾百兆到幾十億行的數(shù)據(jù)規(guī)模,以及包含多個(gè)表和復(fù)雜查詢的查詢樣例扣囊。
說明:
- TPC-DS:是一個(gè)面向決策支持系統(tǒng)的包含多維度常規(guī)應(yīng)用模型的決策支持基準(zhǔn),定義了 25 張表绒疗、99 個(gè)查詢
- TPC-H:用來評(píng)估在線分析處理的基準(zhǔn)程序侵歇,主要模擬了供應(yīng)商和采購商之間的交易行為,包含了 8 張表吓蘑、22 個(gè)查詢惕虑。
2.入門教程
前提條件
1.安裝和配置 Hive。
2.下載 hive-testbench
問題1:TPC-DS 性能測(cè)試場(chǎng)景下磨镶,如何生成 10GB 的 ORC 文件格式的 Hive 數(shù)據(jù)集
# 進(jìn)入 hive-testbench 的根目錄
# 步驟1:構(gòu)建數(shù)據(jù)生成器
$ ./tpcds-build.sh
# 步驟2:運(yùn)行腳本生成數(shù)據(jù)集
$./tpcds-setup.sh 10
問題2:TPC-DS 性能測(cè)試場(chǎng)景下溃蔫,如何生成 1TB 的 RCFile 文件格式的 Hive 數(shù)據(jù)集
# 進(jìn)入 hive-testbench 的根目錄
# 步驟1:構(gòu)建數(shù)據(jù)生成器
$ ./tpcds-build.sh
# 步驟2:設(shè)置 環(huán)境變量并運(yùn)行腳本生成數(shù)據(jù)集
$export FORMAT=rcfile
$./tpcds-setup.sh 1000
問題2:TPC-DS 性能測(cè)試場(chǎng)景下,如何生成 500GB 的 TXT文件格式的 Hive 數(shù)據(jù)集
# 進(jìn)入 hive-testbench 的根目錄
# 步驟1:構(gòu)建數(shù)據(jù)生成器
$ ./tpcds-build.sh
# 步驟2:設(shè)置 環(huán)境變量并運(yùn)行腳本生成數(shù)據(jù)集
$export FORMAT=txt
$./tpcds-setup.sh 500
問題4:TPC-H 性能測(cè)試場(chǎng)景下琳猫,如何生成 100GB 的 ORC 文件格式的 Hive 數(shù)據(jù)集
# 進(jìn)入 hive-testbench 的根目錄
# 步驟1:構(gòu)建數(shù)據(jù)生成器
$ ./tpch-build.sh
# 步驟2:運(yùn)行腳本生成數(shù)據(jù)集
$./tpch-setup.sh 100
3.分析 hive-testbench 的工作流程
說明:執(zhí)行腳本
tpcds-build.sh
可以構(gòu)建出./tpcds-gen/target/tpcds-gen-1.0-SNAPSHOT.jar
伟叛,而tpch-build.sh
可以構(gòu)建出./tpch-gen/target/tpch-gen-1.0-SNAPSHOT.jar
下面以腳本 tpcds-setup.sh
生成 ORC 格式、10GB 的 HIve 數(shù)據(jù)集為例脐嫂,剖析 hive-testbench 的工作流程痪伦。
① 驗(yàn)證參數(shù):腳本首先會(huì)驗(yàn)證輸入的參數(shù)是否正確,包括存儲(chǔ)格式 FORMAT
雹锣、數(shù)據(jù)規(guī)模 SCALE
等參數(shù)网沾。
② 設(shè)置環(huán)境變量:腳本會(huì)設(shè)置一些環(huán)境變量,如 Hadoop蕊爵、Hive辉哥、TPC-DS 數(shù)據(jù)生成器等的路徑。
③ 生成 TPC-DS 數(shù)據(jù)集:腳本會(huì)調(diào)用 TPC-DS 數(shù)據(jù)生成器生成 TPC-DS 數(shù)據(jù)集。這個(gè)過程包括以下幾個(gè)步驟:
- 在 hdfs 中生成 txt 數(shù)據(jù)文件:腳本會(huì)調(diào)用 TPC-DS 數(shù)據(jù)生成器生成相應(yīng)規(guī)模因子的數(shù)據(jù)集醋旦。生成的數(shù)據(jù)包括多個(gè)表恒水,每個(gè)表包含多個(gè)分區(qū)的數(shù)據(jù)。
# 在腳本tpcds-setup.sh 中
# 在 Hadoop 分布式環(huán)境中運(yùn)行打包成 JAR 文件的 Hadoop 應(yīng)用程序
if [ $? -ne 0 ]; then
echo "Generating data at scale factor $SCALE."
(cd tpcds-gen; hadoop jar target/*.jar -d ${DIR}/${SCALE}/ -s ${SCALE})
fi
- 創(chuàng)建數(shù)據(jù)庫和加載 hdfs 的數(shù)據(jù)文件:腳本會(huì)在 Hive 中創(chuàng)建一個(gè)數(shù)據(jù)庫和對(duì)應(yīng)的表饲齐,并加載 hdfs 的數(shù)據(jù)文件钉凌。
# 在腳本tpcds-setup.sh 中
# 腳本ddl-tpcds/text/alltables.sql 創(chuàng)建了 hive表
runcommand "$HIVE -i settings/load-flat.sql -f ddl-tpcds/text/alltables.sql --hivevar DB=tpcds_text_${SCALE} --hivevar LOCATION=${DIR}/${SCALE}"
#在ddl-tpcds/text/alltables.sql中
drop table if exists store_sales;
create external table if not exists store_sales(
ss_sold_date_sk bigint
, ss_sold_time_sk bigint
, ss_item_sk bigint
, ss_customer_sk bigint
, ss_cdemo_sk bigint
, ss_hdemo_sk bigint
, ss_addr_sk bigint
, ss_store_sk bigint
, ss_promo_sk bigint
, ss_ticket_number bigint
, ss_quantity int
, ss_wholesale_cost decimal(7,2)
, ss_list_price decimal(7,2)
, ss_sales_price decimal(7,2)
, ss_ext_discount_amt decimal(7,2)
, ss_ext_sales_price decimal(7,2)
, ss_ext_wholesale_cost decimal(7,2)
, ss_ext_list_price decimal(7,2)
, ss_ext_tax decimal(7,2)
, ss_coupon_amt decimal(7,2)
, ss_net_paid decimal(7,2)
, ss_net_paid_inc_tax decimal(7,2)
, ss_net_profit decimal(7,2)
)
row format delimited fields terminated by '|'
location '${LOCATION}/store_sales'
;
# `location '${LOCATION}/store_sales'`指定了外部表的存儲(chǔ)位置。`${LOCATION}`為${DIR}/${SCALE}即/tmp/tpcds-generate/10
# row format delimited fields terminated by '|'表示文件中每行的以'|'字符作為分隔符捂人,即每行的數(shù)據(jù)對(duì)應(yīng)著表中的字段
④ 轉(zhuǎn)換表為 ORC 格式:腳本會(huì)使用 Hive 的 INSERT OVERWRITE TABLE
命令將原始數(shù)據(jù)表轉(zhuǎn)換為 ORC 格式的數(shù)據(jù)表御雕。這個(gè)過程會(huì)創(chuàng)建新的表,并將數(shù)據(jù)以 ORC 格式寫入其中滥搭。下面以 ORC 格式和 10GB 數(shù)據(jù)量為例酸纲。
# 在腳本tpcds-setup.sh 中
# 創(chuàng)建新的數(shù)據(jù)庫tpcds_bin_partitioned_${FORMAT}_${SCALE},即 tpcds_bin_partitioned_orc_10瑟匆,還有目錄 ddl-tpcds 下的全部 25 張表
# 省略 ...
DIMS="date_dim time_dim item customer customer_demographics household_demographics customer_address store promotion warehouse ship_mode reason income_band call_center web_page catalog_page web_site"
FACTS="store_sales store_returns web_sales web_returns catalog_sales catalog_returns inventory"
# 省略 ...
DATABASE=tpcds_bin_partitioned_${FORMAT}_${SCALE}
# 省略 ...
for t in ${DIMS}
do
COMMAND="$HIVE -i settings/load-partitioned.sql -f ddl-tpcds/bin_partitioned/${t}.sql \
--hivevar DB=${DATABASE} --hivevar SOURCE=tpcds_text_${SCALE} \
--hivevar SCALE=${SCALE} \
--hivevar REDUCERS=${REDUCERS} \
--hivevar FILE=${FORMAT}"
echo -e "${t}:\n\t@$COMMAND $SILENCE && echo 'Optimizing table $t ($i/$total).'" >> $LOAD_FILE
i=`expr $i + 1`
done
for t in ${FACTS}
do
COMMAND="$HIVE -i settings/load-partitioned.sql -f ddl-tpcds/bin_partitioned/${t}.sql \
--hivevar DB=${DATABASE} \
--hivevar SCALE=${SCALE} \
--hivevar SOURCE=tpcds_text_${SCALE} --hivevar BUCKETS=${BUCKETS} \
--hivevar RETURN_BUCKETS=${RETURN_BUCKETS} --hivevar REDUCERS=${REDUCERS} --hivevar FILE=${FORMAT}"
echo -e "${t}:\n\t@$COMMAND $SILENCE && echo 'Optimizing table $t ($i/$total).'" >> $LOAD_FILE
i=`expr $i + 1`
done
# 以目錄 ddl-tpcds/bin_partitioned/store_sales.sql 為例闽坡,分析一下 hive sql
# 創(chuàng)建數(shù)據(jù)庫,其中指定格式為 FILE
create database if not exists ${DB};
use ${DB};
drop table if exists store_sales;
# 創(chuàng)建表 store_sales
create table store_sales
(
ss_sold_time_sk bigint
, ss_item_sk bigint
, ss_customer_sk bigint
, ss_cdemo_sk bigint
, ss_hdemo_sk bigint
, ss_addr_sk bigint
, ss_store_sk bigint
, ss_promo_sk bigint
, ss_ticket_number bigint
, ss_quantity int
, ss_wholesale_cost decimal(7,2)
, ss_list_price decimal(7,2)
, ss_sales_price decimal(7,2)
, ss_ext_discount_amt decimal(7,2)
, ss_ext_sales_price decimal(7,2)
, ss_ext_wholesale_cost decimal(7,2)
, ss_ext_list_price decimal(7,2)
, ss_ext_tax decimal(7,2)
, ss_coupon_amt decimal(7,2)
, ss_net_paid decimal(7,2)
, ss_net_paid_inc_tax decimal(7,2)
, ss_net_profit decimal(7,2)
)
partitioned by (ss_sold_date_sk bigint)
stored as ${FILE};
# 從表為 ${SOURCE}.store_sales 即數(shù)據(jù)格式為 txt 的 tpcds_text_${SCALE} .store_sales愁溜,即 tpcds_text_10.store_sales疾嗅,導(dǎo)入新數(shù)據(jù)庫的 store_sales
from ${SOURCE}.store_sales ss
insert overwrite table store_sales partition (ss_sold_date_sk)
select
ss.ss_sold_time_sk,
ss.ss_item_sk,
ss.ss_customer_sk,
ss.ss_cdemo_sk,
ss.ss_hdemo_sk,
ss.ss_addr_sk,
ss.ss_store_sk,
ss.ss_promo_sk,
ss.ss_ticket_number,
ss.ss_quantity,
ss.ss_wholesale_cost,
ss.ss_list_price,
ss.ss_sales_price,
ss.ss_ext_discount_amt,
ss.ss_ext_sales_price,
ss.ss_ext_wholesale_cost,
ss.ss_ext_list_price,
ss.ss_ext_tax,
ss.ss_coupon_amt,
ss.ss_net_paid,
ss.ss_net_paid_inc_tax,
ss.ss_net_profit,
ss.ss_sold_date_sk
where ss.ss_sold_date_sk is not null
insert overwrite table store_sales partition (ss_sold_date_sk)
select
ss.ss_sold_time_sk,
ss.ss_item_sk,
ss.ss_customer_sk,
ss.ss_cdemo_sk,
ss.ss_hdemo_sk,
ss.ss_addr_sk,
ss.ss_store_sk,
ss.ss_promo_sk,
ss.ss_ticket_number,
ss.ss_quantity,
ss.ss_wholesale_cost,
ss.ss_list_price,
ss.ss_sales_price,
ss.ss_ext_discount_amt,
ss.ss_ext_sales_price,
ss.ss_ext_wholesale_cost,
ss.ss_ext_list_price,
ss.ss_ext_tax,
ss.ss_coupon_amt,
ss.ss_net_paid,
ss.ss_net_paid_inc_tax,
ss.ss_net_profit,
ss.ss_sold_date_sk
where ss.ss_sold_date_sk is null
sort by ss.ss_sold_date_sk
;
⑤ 收集統(tǒng)計(jì)信息:腳本會(huì)使用 Hive 的 ANALYZE TABLE
命令收集表的統(tǒng)計(jì)信息,以幫助優(yōu)化查詢計(jì)劃冕象。
# 利用 hive cli執(zhí)行 /ddl-tpcds/text/analyze_everything.sql
hive>USE tpcds_bin_partitioned_orc_10宪迟;
hive>SOURCE /ddl-tpcds/text/analyze_everything.sql;