-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.py
More file actions
53 lines (43 loc) · 1.29 KB
/
insert_data.py
File metadata and controls
53 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import psycopg
from faker import Faker
import random
def generate_dummy_data(faker):
vector = [random.uniform(-1, 1) for _ in range(500)]
title = faker.sentence()
body = faker.text()
subtitle = faker.sentence()
return vector, title, body, subtitle
def insert_article(connection, vector, title, body, subtitle):
query = """
INSERT INTO article (vector, title, body, subtitle)
VALUES (%s, %s, %s, %s)
"""
with connection.cursor() as cursor:
cursor.execute(query, (vector, title, body, subtitle))
connection.commit()
def create_table(conn):
with conn.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS article (
id SERIAL PRIMARY KEY,
vector double precision[500],
title VARCHAR(200),
body TEXT,
subtitle TEXT
);
"""
)
conn.commit()
if __name__ == "__main__":
faker = Faker()
with psycopg.connect(
dbname="postgres",
user="postgres",
password="postgres",
host="localhost",
port="5432",
) as conn:
create_table(conn)
for _ in range(50_000):
vector, title, body, subtitle = generate_dummy_data(faker)
insert_article(conn, vector, title, body, subtitle)