Writes ``table`` to a MySQL table. The connector works in two modes: **snapshot** mode and **stream of changes**. In **snapshot** mode, the table maintains the current snapshot of the data. In **stream of changes** mode, the table contains the log of all data updates. For stream of
(
table: Table,
connection_string: str,
table_name: str,
*,
max_batch_size: int | None = None,
init_mode: Literal["default", "create_if_not_exists", "replace"] = "default",
output_table_type: Literal["stream_of_changes", "snapshot"] = "stream_of_changes",
primary_key: list[ColumnReference] | None = None,
name: str | None = None,
sort_by: Iterable[ColumnReference] | None = None,
)
| 245 | @check_arg_types |
| 246 | @trace_user_frame |
| 247 | def write( |
| 248 | table: Table, |
| 249 | connection_string: str, |
| 250 | table_name: str, |
| 251 | *, |
| 252 | max_batch_size: int | None = None, |
| 253 | init_mode: Literal["default", "create_if_not_exists", "replace"] = "default", |
| 254 | output_table_type: Literal["stream_of_changes", "snapshot"] = "stream_of_changes", |
| 255 | primary_key: list[ColumnReference] | None = None, |
| 256 | name: str | None = None, |
| 257 | sort_by: Iterable[ColumnReference] | None = None, |
| 258 | ) -> None: |
| 259 | """Writes ``table`` to a MySQL table. |
| 260 | |
| 261 | The connector works in two modes: **snapshot** mode and **stream of changes**. |
| 262 | In **snapshot** mode, the table maintains the current snapshot of the data. |
| 263 | In **stream of changes** mode, the table contains the log of all data updates. For |
| 264 | stream of changes you also need to have two columns, ``time`` and ``diff`` of the |
| 265 | integer type, where ``time`` stores the transactional minibatch time and ``diff`` |
| 266 | is ``1`` for row insertion or ``-1`` for row deletion. |
| 267 | |
| 268 | Args: |
| 269 | table: Table to be written. |
| 270 | connection_string: \ |
| 271 | `Connection string <https://dev.mysql.com/doc/connector-j/en/connector-j-reference-jdbc-url-format.html>`_ \ |
| 272 | for MySQL database. |
| 273 | table_name: Name of the target table. |
| 274 | max_batch_size: Maximum number of entries allowed to be committed within a |
| 275 | single transaction. |
| 276 | init_mode: ``"default"``: The default initialization mode; |
| 277 | ``"create_if_not_exists"``: initializes the SQL writer by creating the necessary table |
| 278 | if they do not already exist; |
| 279 | ``"replace"``: Initializes the SQL writer by replacing any existing table. |
| 280 | output_table_type: Defines how the output table manages its data. If set to ``"stream_of_changes"`` |
| 281 | (the default), the system outputs a stream of modifications to the target table. |
| 282 | This stream includes two additional integer columns: ``time``, representing the computation |
| 283 | minibatch, and ``diff``, indicating the type of change (``1`` for row addition and |
| 284 | ``-1`` for row deletion). If set to ``"snapshot"``, the table maintains the current |
| 285 | state of the data, updated atomically with each minibatch and ensuring that no partial |
| 286 | minibatch updates are visible. |
| 287 | primary_key: When using snapshot mode, one or more columns that form the primary |
| 288 | key in the target MySQL table. |
| 289 | name: A unique name for the connector. If provided, this name will be used in |
| 290 | logs and monitoring dashboards. |
| 291 | sort_by: If specified, the output will be sorted in ascending order based on the |
| 292 | values of the given columns within each minibatch. When multiple columns are provided, |
| 293 | the corresponding value tuples will be compared lexicographically. |
| 294 | |
| 295 | Returns: |
| 296 | None |
| 297 | |
| 298 | Example: |
| 299 | |
| 300 | To test this connector locally, you will need a MySQL instance. The easiest way to do |
| 301 | this is to run a Docker image. For example, you can use the mysql image. Pull and run |
| 302 | it as follows: |
| 303 | |
| 304 | .. code-block:: bash |
nothing calls this directly
no test coverage detected