1. 程式人生 > 其它 >python上傳隨機資料到clickhouse資料庫

python上傳隨機資料到clickhouse資料庫

  1. main.py檔案
from faker import Faker
import time
from clickhouse_driver import Client

f = Faker(locale='zh_CN')

start_time = time.time()


def re_list(start: int, setup: int, header=False):
    l = [{
        "ID": i + start * setup,
        "Name": f.name(),
        "Address": f.address(),
        "Ssn": f.ssn(),
        "Company": f.company(),
        "Job": f.job(),
        "Email": f.email(),
        "Url": f.domain_name(),
        "Card": f.credit_card_number(),
        "PhoneNumber": f.phone_number(),
        "UserName": f.user_name(),
        "Password": f.password(),
        "HeadImg": f.image_url(),
    } for i in range(0, setup)]

    print(l)

    client = Client(host="xxxxxxx.cn", port=9000, user="xxxx", password="xxxx", database="xxxxx")
    # client.execute('INSERT INTO test.upload_data_2_clickhouse (x) VALUES', [[200]])
    client.execute('INSERT INTO test.upload_data_2_clickhouse (ID, Name, Address, Ssn, Company, Job, Email, Url, Card, PhoneNumber, UserName, Password, HeadImg) VALUES', l)

    # select查詢
    # result = client.execute("select * from test.upload_data_2_clickhouse limit 10")

    print(time.time() - start_time)


if __name__ == '__main__':
    for i in range(0, 1):
        re_list(i, 1)
    re_list(0, 1)

  1. Dockerfile檔案
FROM python:3.8

WORKDIR /usr/src/app

COPY ./test01 .

RUN python3 -m pip install --no-cache-dir -U -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

CMD ["/bin/bash", "-c", "/usr/src/app/run.sh"]

  1. run.sh檔案
#!/bin/bash

python ./main.py

  1. requirements.txt檔案
clickhouse-driver
faker

clickhouse-driver官網
faker官網