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官网

posted @ 2022-04-15 15:26  专职  阅读(96)  评论(0编辑  收藏  举报