Bulk upsert of data
YDB supports bulk insert of many rows without atomicity guarantees. The write is split into several independent transactions, each touching a single partition, with parallel execution. This makes the approach more efficient than plain YQL. On success, the BulkUpsert method guarantees that all data passed in the request is inserted.
Warning
When you load data to column-oriented tables using BulkUpsert, you must provide values for all columns, even NULL values.
Below are examples of using the YDB SDK built-in tools for bulk insert:
Go
Java
Python
JavaScript
Native SDK
Native SDK
Native SDK
Native SDK (Asyncio)
SQLAlchemy
import posixpath
import ydb
def bulk_upsert(driver: ydb.Driver, path: str):
column_types = (
ydb.BulkUpsertColumns()
.add_column("id", ydb.PrimitiveType.Uint64)
.add_column("val", ydb.OptionalType(ydb.PrimitiveType.Utf8))
)
rows = [
{"id": 1, "val": "1"},
{"id": 2, "val": "2"},
{"id": 3, "val": "3"},
]
driver.table_client.bulk_upsert(posixpath.join(path, "tablename"), rows, column_types)
import os
import posixpath
import ydb
import asyncio
async def bulk_upsert(driver: ydb.aio.Driver, path: str):
column_types = (
ydb.BulkUpsertColumns()
.add_column("id", ydb.PrimitiveType.Uint64)
.add_column("val", ydb.OptionalType(ydb.PrimitiveType.Utf8))
)
rows = [
{"id": 1, "val": "1"},
{"id": 2, "val": "2"},
{"id": 3, "val": "3"},
]
await driver.table_client.bulk_upsert(
posixpath.join(path, "tablename"), rows, column_types
)
async def main():
async with ydb.aio.Driver(
connection_string=os.environ["YDB_CONNECTION_STRING"],
credentials=ydb.credentials_from_env_variables(),
) as driver:
await driver.wait()
await bulk_upsert(driver, "/local")
asyncio.run(main())
import os
import sqlalchemy as sa
import ydb
engine = sa.create_engine(os.environ["YDB_SQLALCHEMY_URL"])
with engine.connect() as connection:
dbapi_conn = connection.connection
column_types = (
ydb.BulkUpsertColumns()
.add_column("id", ydb.PrimitiveType.Uint64)
.add_column("val", ydb.OptionalType(ydb.PrimitiveType.Utf8))
)
rows = [
{"id": 1, "val": "1"},
{"id": 2, "val": "2"},
{"id": 3, "val": "3"},
]
dbapi_conn.bulk_upsert("tablename", rows, column_types)
This section is under development.