MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / ingest

Function ingest

misc/python/materialize/workload_replay/ingest.py:338–500  ·  view source on GitHub ↗

Ingest data into a source (Postgres, MySQL, Kafka, SQL Server).

(
    c: Composition,
    child: dict[str, Any],
    source: dict[str, Any],
    columns: list[Column],
    num_rows: int,
    rng: random.Random,
)

Source from the content-addressed store, hash-verified

336
337
338def ingest(
339 c: Composition,
340 child: dict[str, Any],
341 source: dict[str, Any],
342 columns: list[Column],
343 num_rows: int,
344 rng: random.Random,
345) -> None:
346 """Ingest data into a source (Postgres, MySQL, Kafka, SQL Server)."""
347 if source["type"] == "postgres":
348 ref_database, ref_schema, ref_table = get_postgres_reference_db_schema_table(
349 child
350 )
351 conn = psycopg.connect(
352 host="127.0.0.1",
353 port=c.default_port("postgres"),
354 user="postgres",
355 password="postgres",
356 dbname=ref_database,
357 )
358 conn.autocommit = True
359
360 col_names = [col.name for col in columns]
361
362 with conn.cursor() as cur:
363 copy_stmt = SQL("COPY {}.{} ({}) FROM STDIN").format(
364 Identifier(ref_schema),
365 Identifier(ref_table),
366 SQL(", ").join(map(Identifier, col_names)),
367 )
368
369 with cur.copy(copy_stmt) as copy:
370 for _ in range(num_rows):
371 row = [col.value(rng, in_query=False) for col in columns]
372 copy.write_row(row)
373
374 elif source["type"] == "mysql":
375 ref_database, ref_table = get_mysql_reference_db_table(child)
376
377 conn = pymysql.connect(
378 host="127.0.0.1",
379 user="root",
380 password=MySql.DEFAULT_ROOT_PASSWORD,
381 database=ref_database,
382 port=c.default_port("mysql"),
383 autocommit=False,
384 )
385
386 value_funcs = [col.value for col in columns]
387 rows_sql = []
388 for _ in range(num_rows):
389 row = [fn(rng) for fn in value_funcs]
390 rows_sql.append("(" + ", ".join(row) + ")")
391
392 stmt = f"INSERT INTO {ref_table} VALUES " + ", ".join(rows_sql)
393
394 with conn.cursor() as cur:
395 cur.execute(stmt)

Callers 3

Calls 15

get_kafka_topicFunction · 0.90
fnFunction · 0.85
get_kafka_objectsFunction · 0.85
ValueErrorClass · 0.85
default_portMethod · 0.80
cursorMethod · 0.80
kafka_valueMethod · 0.80
connectMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected