ray.data.Dataset.write_sql#
- Dataset.write_sql(sql: str, connection_factory: Callable[[], Any], ray_remote_args: Dict[str, Any] | None = None, concurrency: int | None = None) None [源代码]#
写入一个提供 Python DB API2 兼容 连接器的数据库。
备注
此方法使用 DB API2 的
executemany
方法并行写入数据。要了解更多关于此方法的信息,请参阅 PEP 249。备注
此操作将触发对此数据集执行的延迟转换。
示例
import sqlite3 import ray connection = sqlite3.connect("example.db") connection.cursor().execute("CREATE TABLE movie(title, year, score)") dataset = ray.data.from_items([ {"title": "Monty Python and the Holy Grail", "year": 1975, "score": 8.2}, {"title": "And Now for Something Completely Different", "year": 1971, "score": 7.5} ]) dataset.write_sql( "INSERT INTO movie VALUES(?, ?, ?)", lambda: sqlite3.connect("example.db") ) result = connection.cursor().execute("SELECT * FROM movie ORDER BY year") print(result.fetchall())
[('And Now for Something Completely Different', 1971, 7.5), ('Monty Python and the Holy Grail', 1975, 8.2)]