最常用的 5 種流式 ETL 模式!
使用 SQL 語句進行轉(zhuǎn)換?
是的!SQL 將聲明性語言的強大和簡潔性與任何使用代碼或數(shù)據(jù)的人的普遍技能相結(jié)合。與您可能用作替代的幾乎任何編程語言不同,SQL 的普及要歸功于將近 50 年的壽命——計算行業(yè)中的幾乎每個人都曾在某個時候使用過它。SQL 的強大功能和普遍性意味著它無處不在,甚至在構(gòu)建最新開發(fā)人員技術(shù)和服務(wù)的公司中也是如此。當通過函數(shù)增強時,SQL 變得更加強大——我們將在以后的博客文章中介紹。
管道模式
大多數(shù) ETL 管道都適合一種或多種模式。Decodable 的連接 - 流 - 管道抽象意味著您可以選擇將所有內(nèi)容構(gòu)建到單個管道中,或者根據(jù)需要將復(fù)雜的轉(zhuǎn)換分解為由流、跨團隊、區(qū)域和用例連接的可重用管道網(wǎng)絡(luò)。
1:過濾器

過濾器從流中刪除不需要的記錄,刪除與 SQL where子句中的“規(guī)則”不匹配的記錄。過濾器通常用于抑制敏感記錄以確保合規(guī)性,或減少目標系統(tǒng)上的處理負載或存儲需求。
-- Filter only records pertaining to the application
insert into application_events
select * from http_eventswhere hostname = 'app.decodable.co'
-- Filter only records that modify the inventory
insert into inventory_updates
select * from http_eventswhere hostname = 'api.mycompany.com' and
path like '/v1/inventory%' and
method in ( 'POST', 'PUT', 'DELETE', 'PATCH' )
?
2:路線

Route 模式從一個或多個輸入流創(chuàng)建多個輸出流,根據(jù)一組規(guī)則將記錄定向到正確的目的地。此模式實際上由多個過濾器組成,它們都可以查看每個輸入記錄,但每個過濾器僅傳輸與該特定目的地的規(guī)則匹配的那些記錄。
-- Route security-related HTTP events
insert into security_events
select * from http_eventswhere path like '/login%' or
path like '/billing/cc%'
-- Route app-related HTTP events
insert into application_events
select * from http_eventswhere hostname = 'app.decodable.co'
-- Route requests to Customer Success if it looks like the user needs help
insert into cs_alerts
select * from http_events
where response_code between 500 and 599 or -- any server failure
( path = '/signup' and response_code != 200 ) or -- failed to sign up for any reason
?
3:變換

轉(zhuǎn)換管道通過修改輸入記錄來創(chuàng)建輸出記錄。通常這將導(dǎo)致 1:1 傳輸,但在某些情況下,輸出來自多個輸入記錄,因此可能存在 1:many 關(guān)系。在這里,我們將調(diào)用三個專門的轉(zhuǎn)換:
變換:提取

解析輸入記錄,從輸入記錄中提取數(shù)據(jù)并將其用作豐富派生輸出記錄的基礎(chǔ)。
-- Parse timestamp and action
insert into user_events
select
to_date(fields['ts'], 'YYYY-MM-DD''T''HH:MI:SS') as ts,
fields['user_id'] as user_id,
fields['path'] as path, case fields['method'] when 'GET' then 'read'
when 'POST', 'PUT' then 'modify'
when 'DELETE' then 'delete'
end as actionfrom ( select
grok(
body, '\[${ISO8661_DATETIME:ts} ${DATA:method} "${PATH:path}" uid:${DATA:user_id}'
) as fields from http_event
)
?
變換:歸一化

傳入的數(shù)據(jù)記錄通常需要針對模式進行規(guī)范化,以便目標系統(tǒng)處理它們。缺少的字段可能需要填充默認值,可能需要刪除可選字段,并強制執(zhí)行數(shù)據(jù)類型。
-- Cleanse incoming data for downstream processes
insert into sensor_readings
select
cast(ifnull(sensor_id, '0') as bigint) as sensor_id, lower(trim(name)) as name, cast(`value` as bigint) as reading
from raw_sensor_readings
轉(zhuǎn)換:匿名化

在目標系統(tǒng)不需要信息來完成處理的情況下,匿名管道只是出于合規(guī)、監(jiān)管或隱私原因而消除了敏感字段。
-- Anonymize SSNs and zip codes
insert into user_events_masked
select
user_id,
username, overlay(ssn placing '*' from 1 for 12) as ssn, substring(zip_code from 1 for 2) as zip_code_1,action
from user_events
?
4:聚合

聚合管道通常使用 SQL 窗口函數(shù)將傳入記錄分組到存儲桶中(通?;跁r間),在這些存儲桶上執(zhí)行聚合操作。Count、Min、Max、Avg、Sum 是典型的運算符,但還有很多。
-- Count the number of events by path and status every 10 seconds.
insert into site_activity
select
window_start,
window_end,
path,status, count(1) as `count`
from table(
tumble( table http_events, descriptor(_time),
interval '10' seconds
)
)group by window_start, window_end, path, status
5:觸發(fā)

我們的最終模式是觸發(fā)器。與幾乎所有其他模式不同,觸發(fā)器輸出記錄可能與輸入記錄的模式幾乎沒有重疊,因為它表明已在一個或多個輸入記錄上檢測到一組條件,并作為結(jié)果輸出警報。輸出模式可以表示檢測到的條件、要采取的行動或兩者兼而有之。
-- Build hourly usage data for a Stripe integration on the output stream
insert into stripe_product_usage
select
window_start as _time,
customer_id, 'abcd1234' as price_id sum(bytes_sent) / 1024 / 1024 as mb_sentfrom table(
tumble( table document_downloads, descriptor(_time),
interval '1' hour
)
)group by window_start, customer_idhaving mb_sent > 1024
?
