使用該notebookutils.businessEvents模組從 Fabric 筆記本發佈商業活動。 你可以在事件結構集中發布單一事件,或是一組緊接商業事件結構的事件。
商業事件工具可在 Python 3.12 和 PySpark(Python)筆記本中使用。
下表列出可用的方法:
| Method | Signature | Description |
|---|---|---|
help |
help(methodName: str = '') -> None |
顯示模組或指定方法的說明資訊。 |
publish |
publish(eventSchemaSetWorkspace: str, eventSchemaSet: str, eventTypeName: str, eventData: Union[Dict[str, Any], List[Dict[str, Any]]], dataVersion: str = 'v1') -> bool |
將一個或多個業務事件發佈至指定的事件類型。 |
尋求幫助
執行以下指令以取得模組的概覽:
notebookutils.businessEvents.help()
若要取得 publish 方法的說明,請指定方法名稱:
notebookutils.businessEvents.help("publish")
發佈商業活動
用 publish() 來傳送符合商業事件結構的有效載荷:
published = notebookutils.businessEvents.publish(
eventSchemaSetWorkspace="ContosoWorkspace",
eventSchemaSet="OrderEvents",
eventTypeName="OrderDelayed",
eventData={
"orderId": "12345",
"status": "delayed",
"reason": "weather"
},
dataVersion="v1"
)
print(f"Published: {published}")
下表描述參數:
| Parameter | 類型 | Description |
|---|---|---|
eventSchemaSetWorkspace |
String | 包含事件結構集合的工作空間名稱或 ID。 |
eventSchemaSet |
String | 事件結構集合的名稱或 ID。 |
eventTypeName |
String | 要發表的商業活動類型名稱。 |
eventData |
字典或字典列表 | 活動載荷。 每個字典必須符合所選的商業事件架構。 |
dataVersion |
String | 商業事件架構的版本。 預設值為 v1。 |
當事件成功發佈時,該方法會回傳 True 。 如果事件無法發佈,方法會產生例外。
發布多個業務事件
若要在同一呼叫中發布多個相同類型的事件,請將字典清單傳遞給 eventData:
published = notebookutils.businessEvents.publish(
eventSchemaSetWorkspace="ContosoWorkspace",
eventSchemaSet="OrderEvents",
eventTypeName="OrderDelayed",
eventData=[
{
"orderId": "12345",
"status": "delayed",
"reason": "weather"
},
{
"orderId": "12346",
"status": "delayed",
"reason": "traffic"
}
],
dataVersion="v1"
)
print(f"Published: {published}")
列表中所有有效載荷必須符合由 dataVersion所指定的結構版本。
考慮事項
- 在從筆記本發佈事件之前,先建立商業事件類型及其架構。
- 請對
eventSchemaSetWorkspace和eventSchemaSet使用名稱或 ID。 - 將屬性名稱、資料型態和所需欄位
eventData與所選結構版本相匹配。 - 處理來自
publish()的例外,讓 Notebook 和管線執行回報發佈失敗。