|
3 | 3 | This example is the streaming counterpart to a normal batch entrypoint. Instead |
4 | 4 | of a batch ``Client`` with ``read_dlo`` / ``write_to_dlo`` (which read and write |
5 | 5 | a bounded snapshot), it uses a :class:`StreamingClient` and its streaming delta |
6 | | -methods: |
| 6 | +methods. |
7 | 7 |
|
8 | | -* ``client.read_dlo_deltas()`` returns a *streaming* DataFrame over the |
9 | | - Change Data Feed of the source DLO. Each row carries the source columns plus |
10 | | - change-feed metadata columns (``_record_type``, ``_commit_*``). |
11 | | -* ``client.write_dlo_deltas(name, df)`` starts a streaming query that writes |
12 | | - each micro-batch to the target DLO and returns the ``StreamingQuery`` handle. |
13 | | - The runtime owns the trigger, and checkpoint location — the caller only |
14 | | - chooses the table. |
| 8 | +The first run of a streaming job will use the run mode INITIAL_SYNC which behaves |
| 9 | +like a batch run on the streaming source. A streaming transform can also use run |
| 10 | +mode REBUILD to do the same thing on demand. Note that these will process all |
| 11 | +source rows and overwrite the target. |
15 | 12 |
|
16 | 13 | The transform in between is ordinary PySpark. Because the source is a change |
17 | 14 | feed, keep the metadata columns on the DataFrame you hand to |
18 | 15 | ``write_dlo_deltas`` — the sink relies on them to merge changes correctly. |
19 | 16 |
|
20 | | -This entrypoint only runs inside the Data Cloud streaming (``DELTA_SYNC``) |
21 | | -runtime; the local ``datacustomcode run`` readers/writers raise |
| 17 | +This entrypoint only runs inside the Data Cloud runtime; |
| 18 | + the local ``datacustomcode run`` readers/writers raise |
22 | 19 | ``NotImplementedError`` for the delta methods. |
23 | 20 | """ |
24 | 21 |
|
| 22 | +from pyspark.sql import DataFrame |
25 | 23 | from pyspark.sql.functions import col, upper |
26 | 24 |
|
27 | | -from datacustomcode.client import StreamingClient |
| 25 | +from datacustomcode.client import ( |
| 26 | + RunMode, |
| 27 | + StreamingClient, |
| 28 | + get_run_mode, |
| 29 | +) |
28 | 30 |
|
29 | 31 |
|
30 | 32 | def main(): |
| 33 | + target_dlo = "Account_std_copy__dll" |
31 | 34 | client = StreamingClient() |
32 | 35 |
|
33 | | - # Streaming DataFrame over the source DLO's change feed. |
34 | | - deltas = client.read_dlo_deltas() |
35 | | - |
36 | | - # Ordinary PySpark transform. |
37 | | - transformed = deltas.withColumn("description__c", upper(col("description__c"))) |
38 | | - |
39 | | - # Start the streaming write. write_dlo_deltas returns the StreamingQuery; |
40 | | - # the trigger and checkpoint location are provided by the runtime. |
41 | | - query = client.write_dlo_deltas("Account_std_copy__dll", transformed) |
42 | | - |
43 | | - # Drive the query's lifecycle. In the streaming runtime this blocks until |
44 | | - # the job is stopped by the platform. |
45 | | - query.awaitTermination() |
| 36 | + if get_run_mode() == RunMode.DELTA_SYNC: |
| 37 | + # Streaming DataFrame over the source DLO's change feed. |
| 38 | + dataframe = client.read_dlo_deltas() |
| 39 | + # Ordinary PySpark transform. |
| 40 | + transformed = transform(dataframe) |
| 41 | + |
| 42 | + # Start the streaming write. write_dlo_deltas returns the StreamingQuery; |
| 43 | + # the trigger and checkpoint location are provided by the runtime. |
| 44 | + query = client.write_dlo_deltas(target_dlo, transformed) |
| 45 | + |
| 46 | + # Drive the query's lifecycle. In the streaming runtime this blocks until |
| 47 | + # the job is stopped by the platform. |
| 48 | + query.awaitTermination() |
| 49 | + else: |
| 50 | + # initial sync and rebuild read the entire streaming source DLO and |
| 51 | + # write using a server-decided mode based on the run mode |
| 52 | + dataframe = client.read_dlo() |
| 53 | + transformed = transform(dataframe) |
| 54 | + client.auto_write_to_dlo(target_dlo, transformed) |
| 55 | + |
| 56 | + |
| 57 | +def transform(dataframe: DataFrame) -> DataFrame: |
| 58 | + return dataframe.withColumn("description__c", upper(col("description__c"))) |
46 | 59 |
|
47 | 60 |
|
48 | 61 | if __name__ == "__main__": |
|
0 commit comments