Write to a database that provides a `Python DB API2-compliant `_ connector. .. note:: This method writes data in parallel using the DB API2 ``executemany`` method. To learn more about this method, see `PEP 249 <
(
self,
sql: str,
connection_factory: Callable[[], Connection],
ray_remote_args: Optional[Dict[str, Any]] = None,
concurrency: Optional[int] = None,
)
| 5192 | |
| 5193 | @ConsumptionAPI |
| 5194 | def write_sql( |
| 5195 | self, |
| 5196 | sql: str, |
| 5197 | connection_factory: Callable[[], Connection], |
| 5198 | ray_remote_args: Optional[Dict[str, Any]] = None, |
| 5199 | concurrency: Optional[int] = None, |
| 5200 | ) -> None: |
| 5201 | """Write to a database that provides a |
| 5202 | `Python DB API2-compliant <https://peps.python.org/pep-0249/>`_ connector. |
| 5203 | |
| 5204 | .. note:: |
| 5205 | |
| 5206 | This method writes data in parallel using the DB API2 ``executemany`` |
| 5207 | method. To learn more about this method, see |
| 5208 | `PEP 249 <https://peps.python.org/pep-0249/#executemany>`_. |
| 5209 | |
| 5210 | Examples: |
| 5211 | |
| 5212 | .. testcode:: |
| 5213 | |
| 5214 | import sqlite3 |
| 5215 | import ray |
| 5216 | |
| 5217 | connection = sqlite3.connect("example.db") |
| 5218 | connection.cursor().execute("CREATE TABLE movie(title, year, score)") |
| 5219 | dataset = ray.data.from_items([ |
| 5220 | {"title": "Monty Python and the Holy Grail", "year": 1975, "score": 8.2}, |
| 5221 | {"title": "And Now for Something Completely Different", "year": 1971, "score": 7.5} |
| 5222 | ]) |
| 5223 | |
| 5224 | dataset.write_sql( |
| 5225 | "INSERT INTO movie VALUES(?, ?, ?)", lambda: sqlite3.connect("example.db") |
| 5226 | ) |
| 5227 | |
| 5228 | result = connection.cursor().execute("SELECT * FROM movie ORDER BY year") |
| 5229 | print(result.fetchall()) |
| 5230 | |
| 5231 | .. testoutput:: |
| 5232 | |
| 5233 | [('And Now for Something Completely Different', 1971, 7.5), ('Monty Python and the Holy Grail', 1975, 8.2)] |
| 5234 | |
| 5235 | .. testcode:: |
| 5236 | :hide: |
| 5237 | |
| 5238 | import os |
| 5239 | os.remove("example.db") |
| 5240 | |
| 5241 | Arguments: |
| 5242 | sql: An ``INSERT INTO`` statement that specifies the table to write to. The |
| 5243 | number of parameters must match the number of columns in the table. |
| 5244 | connection_factory: A function that takes no arguments and returns a |
| 5245 | Python DB API2 |
| 5246 | `Connection object <https://peps.python.org/pep-0249/#connection-objects>`_. |
| 5247 | ray_remote_args: Keyword arguments passed to :func:`ray.remote` in the |
| 5248 | write tasks. |
| 5249 | concurrency: The maximum number of Ray tasks to run concurrently. Set this |
| 5250 | to control number of tasks to run concurrently. This doesn't change the |
| 5251 | total number of tasks run. By default, concurrency is dynamically |