Regular Join流程解析

以下文章全部基于 Flink 1.14

StreamingJoinOperator.java

debug 代碼可以看到 Regular Join 邏輯實現在 StreamingJoinOperator.java (幾種 Regular Join 都是 )

regular-join.jpg

processElement 注釋偽代碼

if input record is accumulate
|  if input side is outer
|  |  if there is no matched rows on the other side, send +I[record+null], state.add(record, 0)
|  |  if there are matched rows on the other side
|  |  | if other side is outer
|  |  | |  if the matched num in the matched rows == 0, send -D[null+other]
|  |  | |  if the matched num in the matched rows > 0, skip
|  |  | |  otherState.update(other, old + 1)
|  |  | endif
|  |  | send +I[record+other]s, state.add(record, other.size)
|  |  endif
|  endif
|  if input side not outer
|  |  state.add(record)
|  |  if there is no matched rows on the other side, skip
|  |  if there are matched rows on the other side
|  |  |  if other side is outer
|  |  |  |  if the matched num in the matched rows == 0, send -D[null+other]
|  |  |  |  if the matched num in the matched rows > 0, skip
|  |  |  |  otherState.update(other, old + 1)
|  |  |  |  send +I[record+other]s
|  |  |  else
|  |  |  |  send +I/+U[record+other]s (using input RowKind)
|  |  |  endif
|  |  endif
|  endif
endif

if input record is retract
|  state.retract(record)
|  if there is no matched rows on the other side
|  | if input side is outer, send -D[record+null]
|  endif
|  if there are matched rows on the other side, send -D[record+other]s if outer, send -D/-U[record+other]s if inner.
|  |  if other side is outer
|  |  |  if the matched num in the matched rows == 0, this should never happen!
|  |  |  if the matched num in the matched rows == 1, send +I[null+other]
|  |  |  if the matched num in the matched rows > 1, skip
|  |  |  otherState.update(other, old - 1)
|  |  endif
|  endif
endif

數據

order_log

order_id movie_id order_timestamp
1 1 2021-12-25 00:00:00
2 2 2021-12-25 00:01:00
3 3 2021-12-25 00:02:00

price_log

order_id set_price price_timestamp
1 40 2021-12-25 00:00:01
3 80 2021-12-25 00:02:00

Left Join

Left Join Demo

INSERT INTO print
SELECT
    o.order_id
    ,movie_id
    ,seat_price
    ,o.timestamp
FROM order_log o
LEFT JOIN price_log p ON o.order_id = p.order_id

Left Join 過程

  1. 左表來一條數據蚀同,與右表逐個比較進行關聯
    • 如果能關聯上缘缚,發(fā)送 +I[left_record, matched_right_record]鹤耍,并將 (left_record, matched_right_record_number) 保存到左表狀態(tài)中 (以供后續(xù) join 使用)
    • 如果不能關聯上迄埃,發(fā)送 +I[left_record, null]湘捎,并將 (left_record, 0) 保存到左表狀態(tài)中
  2. 右表來一條數據,與左表逐個比較進行關聯,不論是否關聯到钱豁,都將右表數據保存到右表狀態(tài)中
    • 如果能關聯上
      • 如果關聯到左表數據的 numOfAssociations 等于0 ,則發(fā)送 -D[matched_left_record, null]疯汁,更新左表狀態(tài) (matched_left_record, numOfAssociations + 1)牲尺,發(fā)送 +I[matched_left_record, right_record] (把之前沒關聯到右表數據的 left_record 撤回,把關聯到的最新結果下發(fā))
      • 如果關聯到左表數據的 numOfAssociations 大于0幌蚊,更新左表狀態(tài) (matched_left_record, numOfAssociations + 1)谤碳,發(fā)送 +I[matched_left_record, right_record]
    • 否則什么也不做

Inner Join

Inner Join Demo

INSERT INTO print
SELECT
    o.order_id
    ,movie_id
    ,seat_price
    ,o.timestamp
FROM order_log o
INNER JOIN price_log p ON o.order_id = p.order_id

Inner Join 過程

  1. 左表來一條數據,與右邊逐個比較進行關聯溢豆,將左表數據保存到狀態(tài)中
    • 如果不能關聯上蜒简,什么都不做
    • 如果能關聯上,發(fā)送 +I[left_record, matched_right_record]
  2. 右表來一條數據漩仙,與左邊逐個比較進行關聯搓茬,將右表數據保存到狀態(tài)中,
    • 如果不能關聯上讯赏,什么都不做
    • 如果能關聯上垮兑,發(fā)送 +I[matched_left_record, reight_record]

Full Outer Join

Full Outer Join Demo

INSERT INTO print
SELECT
    o.order_id
    ,movie_id
    ,seat_price
    ,o.timestamp
FROM order_log o
FULL OUTER JOIN price_log p ON o.order_id = p.order_id

Full Outer Join 過程

  1. 左表來一條數據,與右表逐個比較關聯

    • 如果不能關聯上漱挎,發(fā)送 +I[left_record, null]系枪,并將 (left_record, 0) 保存到左表狀態(tài)中
    • 如果能關聯上
      • 如果關聯到的右表數據的 numOfAssociations 等于0,則發(fā)送 -D[null, matched_right_record]磕谅,更新右表狀態(tài) (matched_right_record, numOfAssociations + 1)私爷,發(fā)送 +I[left_record, matched_right_record],更新左表狀態(tài) (left_record, matched_right_record_number)
      • 如果關聯到右表數據的 numOfAssociations 不等于0膊夹,更新右表狀態(tài) (matched_right_record, numOfAssociations + 1)衬浑,發(fā)送 +I[left_record, matched_right_record],更新左表狀態(tài) (left_record, matched_right_record_number)
  2. 右表來一條數據放刨,與左表逐個比較關聯

    • 如果不能關聯上工秩,發(fā)送 +I[null, right_record],并將 (right_record, 0) 保存到右表狀態(tài)中
    • 如果能關聯上
      • 如果關聯到的左表數據的 numOfAssociations 等于0进统,則發(fā)送 -D[matched_left_record, null]助币,更新左表狀態(tài) (matched_left_record, numOfAssociations + 1),發(fā)送 +I[matched_left_record, right_record]螟碎,更新右表狀態(tài) (right_record, matched_left_record_number)
      • 如果關聯到的左表數據的 numOfAssociations 不等于0眉菱,更新左表狀態(tài) (matched_left_record, numOfAssociations + 1),發(fā)送 +I[matched_left_record, right_record]掉分,更新右表狀態(tài) (right_record, matched_left_record_number)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末俭缓,一起剝皮案震驚了整個濱河市克伊,隨后出現的幾起案子,更是在濱河造成了極大的恐慌华坦,老刑警劉巖愿吹,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異季春,居然都是意外死亡洗搂,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進店門载弄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耘拇,“玉大人,你說我怎么就攤上這事宇攻”古眩” “怎么了?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵逞刷,是天一觀的道長嘉涌。 經常有香客問我,道長夸浅,這世上最難降的妖魔是什么仑最? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮帆喇,結果婚禮上警医,老公的妹妹穿的比我還像新娘。我一直安慰自己坯钦,他們只是感情好预皇,可當我...
    茶點故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著婉刀,像睡著了一般吟温。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上突颊,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天鲁豪,我揣著相機與錄音,去河邊找鬼律秃。 笑死呈昔,一個胖子當著我的面吹牛,可吹牛的內容都是我干的友绝。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼肝劲,長吁一口氣:“原來是場噩夢啊……” “哼迁客!你這毒婦竟也來了郭宝?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤掷漱,失蹤者是張志新(化名)和其女友劉穎粘室,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體卜范,經...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡衔统,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了海雪。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锦爵。...
    茶點故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖奥裸,靈堂內的尸體忽然破棺而出险掀,到底是詐尸還是另有隱情,我是刑警寧澤湾宙,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布樟氢,位于F島的核電站,受9級特大地震影響侠鳄,放射性物質發(fā)生泄漏埠啃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一伟恶、第九天 我趴在偏房一處隱蔽的房頂上張望碴开。 院中可真熱鬧,春花似錦知押、人聲如沸叹螟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽罢绽。三九已至,卻和暖如春静盅,著一層夾襖步出監(jiān)牢的瞬間良价,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工蒿叠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留明垢,地道東北人。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓市咽,卻偏偏與公主長得像痊银,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子施绎,可洞房花燭夜當晚...
    茶點故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內容