• python写入数据到clickhouse


    环境:

    python3.6

     clickhouse-server-20.8.3.18

    1.加载clickhouse驱动包

    pip3 install clickhouse_driver

    2.表结构

    全局表

    CREATE TABLE db_test.metric_all
    (
        `app` String,
        `block_qps` Int64,
        `count` Int64,
        `exception_qps` Int64,
        `gmt_create` DateTime DEFAULT now(),
        `gmt_modified` DateTime DEFAULT now(),
        `id` Int64,
        `machine_ip` Nullable(String),
        `pass_qps` Int64,
        `resource` String,
        `resource_code` Int64,
        `rt` Float64,
        `success_qps` Int64,
        `timestamp` DateTime DEFAULT now(),
        `day` Date DEFAULT now()
    )
    ENGINE = Distributed('perftest_3shards_1replicas', 'db_test', 'metric_local', id)

    本地表

    CREATE TABLE db_test.metric_local
    (
        `app` String,
        `block_qps` Int64,
        `count` Int64,
        `exception_qps` Int64,
        `gmt_create` DateTime DEFAULT now(),
        `gmt_modified` DateTime DEFAULT now(),
        `id` Int64,
        `machine_ip` Nullable(String),
        `pass_qps` Int64,
        `resource` String,
        `resource_code` Int64,
        `rt` Float64,
        `success_qps` Int64,
        `timestamp` DateTime DEFAULT now(),
        `day` Date DEFAULT now()
    )
    ENGINE = MergeTree
    PARTITION BY day
    ORDER BY id
    SETTINGS index_granularity = 8192

    3.写入数据

    from clickhouse_driver import Client
    
    
    client = Client(host='192.168.1.118',database='db_test',user='dbaadmin' ,password='123456')
    
    for i in range(201, 100000):
        str_i = str(i)
        ##insert_sql = "insert into user_local(id,name) values ('%s','%s')" % (i, "name" + str_i)
        insert_sql = "insert into metric_all(app,block_qps,count,exception_qps,id,machine_ip,pass_qps,resource,resource_code,rt,success_qps) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
        "app" + str_i, i, i, i, i, "machine_ip" + str_i, i, "resource" + str_i, i, i, i)
        ans = client.execute(insert_sql)
  • 相关阅读:
    使用Unity5.1进行VR开发的配置(最新的未必是最好的!!!)
    从单幅深度图识别人体姿态
    工作中编写存储过程小记
    【积累】根据CheckBox的不选中 ,用JQuery 清除 RidaoButtonList 的选中项
    【积累】LinqToSql复合查询结果转DataTable数据
    MSSSQL 脚本收藏
    VS2010历史记录清理
    PowerDesigner16工具学习笔记-建立CDM
    Android 安装过程中的问题
    Unity3D集成SVN进行版本控制
  • 原文地址:https://www.cnblogs.com/hxlasky/p/14679895.html
Copyright © 2020-2023  润新知