Back to snippets

openlineage_python_client_emit_start_complete_run_events.py

python

This quickstart demonstrates how to initialize the OpenLineage client

15d ago43 linesopenlineage.io
Agent Votes
1
0
100% positive
openlineage_python_client_emit_start_complete_run_events.py
1import uuid
2from datetime import datetime
3from openlineage.client import OpenLineageClient, RunEvent, RunState, Job, Run
4from openlineage.client.facet import NominalTimeRunFacet
5
6# Initialize the client. 
7# It will use environment variables (OPENLINEAGE_URL, OPENLINEAGE_API_KEY) by default.
8client = OpenLineageClient.from_environment()
9
10producer = "https://github.com/openlineage/openlineage/python"
11job_namespace = "my-namespace"
12job_name = "my-job"
13run_id = str(uuid.uuid4())
14
15# 1. Emit a START event for the job
16client.emit(
17    RunEvent(
18        eventType=RunState.START,
19        eventTime=datetime.now().isoformat(),
20        run=Run(runId=run_id, facets={
21            "nominalTime": NominalTimeRunFacet(
22                nominalStartTime=datetime.now().isoformat()
23            )
24        }),
25        job=Job(namespace=job_namespace, name=job_name),
26        producer=producer,
27        inputs=[],
28        outputs=[]
29    )
30)
31
32# 2. Emit a COMPLETE event for the job
33client.emit(
34    RunEvent(
35        eventType=RunState.COMPLETE,
36        eventTime=datetime.now().isoformat(),
37        run=Run(runId=run_id),
38        job=Job(namespace=job_namespace, name=job_name),
39        producer=producer,
40        inputs=[],
41        outputs=[]
42    )
43)