通過(guò)Transforms處理和分析數(shù)據(jù)
Elasticsearch的Transforms功能是一種數(shù)據(jù)處理和分析工具,用于對(duì)數(shù)據(jù)進(jìn)行預(yù)處理、聚合和轉(zhuǎn)換等操作,Transforms功能可以在不影響原始數(shù)據(jù)的情況下,對(duì)數(shù)據(jù)進(jìn)行加工處理,更好地滿足數(shù)據(jù)分析和可視化的需求。本文通過(guò)Transforms功能對(duì)投籃數(shù)據(jù)進(jìn)行轉(zhuǎn)換,并查看轉(zhuǎn)換后的數(shù)據(jù)。
準(zhǔn)備工作
下載樣例數(shù)據(jù)。本文使用Kaggle上的數(shù)據(jù)集
NBA shot logs
,該數(shù)據(jù)集中包含投籃時(shí)間、投籃者、投籃點(diǎn)、最近的防守球員、最近的防守球員距離投籃者的距離等投籃數(shù)據(jù)。下載地址,請(qǐng)參見(jiàn)shot_logs.csv。創(chuàng)建阿里云Elasticsearch實(shí)例。具體操作,請(qǐng)參見(jiàn)創(chuàng)建阿里云Elasticsearch實(shí)例。本文使用7.10.0版本Elasticsearch實(shí)例。
說(shuō)明暫不支持8.5版本Elasticsearch實(shí)例。
登錄Kibana控制臺(tái)。具體操作,請(qǐng)參見(jiàn)登錄Kibana控制臺(tái)。
導(dǎo)入數(shù)據(jù)
nba_short_logs
,并創(chuàng)建索引。在Kibana頁(yè)面的左上角,選擇
。單擊Data Visualizer頁(yè)簽。
在Import data區(qū)域,單擊Upload file。
單擊圖標(biāo)。
選擇
shot_logs
文件。在頁(yè)面右下角,單擊Import。
在Simple頁(yè)簽下,輸入Index name(索引名稱)為
nba_short_logs
,選中Create index pattern(創(chuàng)建索引)。單擊Import。
數(shù)據(jù)導(dǎo)入成功后的界面如下圖所示。
操作步驟
本文通過(guò)三種方式實(shí)現(xiàn)數(shù)據(jù)的轉(zhuǎn)換和查看。
方法一:通過(guò)Kibana創(chuàng)建轉(zhuǎn)換并查看數(shù)據(jù)
在Kibana頁(yè)面的左上角,選擇
。在左側(cè)菜單欄,單擊
。單擊Create your first transform。
選擇
nba_short_logs
索引。在Configuration區(qū)域,Group by選擇histogram(GAME_ID),Aggregations選擇DRIBBLES.sum、DRIBBLES.avg和DRIBBLES.max。
說(shuō)明Group by GAME_ID:按比賽ID進(jìn)行分組。
DRIBBLES.sum:每場(chǎng)比賽中所有球員運(yùn)球的總次數(shù)。
DRIBBLES.avg:每場(chǎng)比賽中每個(gè)球員的平均運(yùn)球次數(shù)。
DRIBBLES.max:每場(chǎng)比賽中運(yùn)球次數(shù)最多的球員的運(yùn)球次數(shù)。
在Configuration區(qū)域右下角,單擊Next。
在Transform details區(qū)域,填寫(xiě)Transform ID和Destination index(目的地索引),然后在頁(yè)面右側(cè)單擊Next。
在Create區(qū)域,單擊Create and start。
說(shuō)明Progress的進(jìn)度條變?yōu)?00%,表示創(chuàng)建完成。
單擊Discover,查看Destination index數(shù)據(jù)。
方法二:通過(guò)API創(chuàng)建轉(zhuǎn)換并查看數(shù)據(jù)
在Kibana頁(yè)面的左上角,選擇
。在Console頁(yè)簽中,執(zhí)行以下命令,通過(guò)transform創(chuàng)建轉(zhuǎn)換。
PUT _transform/test2_nba_shot_logs { "source": { "index": "nba_short_logs" }, "dest" : { "index" : "test2_nba_short_logs" }, "pivot": { "group_by": { "game_id": { "terms": { "field": "GAME_ID" }} }, "aggregations": { "dribbles_sum": { "sum": { "field": "DRIBBLES" }}, "dribbles_avg": { "avg": { "field": "DRIBBLES" }}, "dribbles_max": { "cardinality": { "field": "DRIBBLES" }} } } }
執(zhí)行以下命令,通過(guò)preview查看轉(zhuǎn)換后的數(shù)據(jù)。
POST _transform/_preview { "source": { "index": "nba_shot_logs" }, "dest" : { "index" : "test2_nba_shot_logs" }, "pivot": { "group_by": { "game_id": { "terms": { "field": "GAME_ID" }} }, "aggregations": { "dribbles_sum": { "sum": { "field": "DRIBBLES" }}, "dribbles_avg": { "avg": { "field": "DRIBBLES" }}, "dribbles_max": { "cardinality": { "field": "DRIBBLES" }} } } }
方法三:通過(guò)API創(chuàng)建轉(zhuǎn)換并通過(guò)Discover查看數(shù)據(jù)
創(chuàng)建并啟動(dòng)Transform。
在Kibana頁(yè)面的左上角,選擇
。在Console頁(yè)簽中,執(zhí)行以下命令,創(chuàng)建Transform。
PUT _transform/test2_nba_shot_logs { "source": { "index": "nba_short_logs" }, "dest" : { "index" : "test2_nba_short_logs" }, "pivot": { "group_by": { "game_id": { "terms": { "field": "GAME_ID" }} }, "aggregations": { "dribbles_sum": { "sum": { "field": "DRIBBLES" }}, "dribbles_avg": { "avg": { "field": "DRIBBLES" }}, "dribbles_max": { "cardinality": { "field": "DRIBBLES" }} } } }
啟動(dòng)Transform。
POST _transform/test2_nba_shot_logs/_start
說(shuō)明Transform默認(rèn)是關(guān)閉的。
創(chuàng)建索引模式。
說(shuō)明在Discover中查看數(shù)據(jù)之前需要先創(chuàng)建索引模式。
在Kibana頁(yè)面的左上角,選擇
。在左側(cè)菜單欄,單擊
。在頁(yè)面右上角,單擊Create index pattern。
在Index pattern name中輸入轉(zhuǎn)換后的目標(biāo)索引,即test2_nba_short_logs,單擊Next step。
單擊Create index pattern。
通過(guò)Discover查看數(shù)據(jù)。
在Kibana頁(yè)面的左上角,選擇
。選擇轉(zhuǎn)換后的目標(biāo)索引,查看轉(zhuǎn)換后的數(shù)據(jù)。