出口与出口使用Python将CSV输出映射到MySQL表

时间:2014-04-25 18:40:14

标签: python mysql csv cron iperf

我有多个客户端用于网络监控的单服务器双向iperf设置。 iperf服务器运行良好,并根据客户端写入的cron作业以CSV格式显示输出。

我希望编写一个python脚本来自动化将这些CSV输出映射到MySQL数据库的过程;反过来,它将定期更新和保存,无需人工干预。

我使用的是Ubuntu 13.10计算机作为iperf服务器。以下是我得到的CSV输出示例。这不会存储到文件中,只是显示在屏幕上。

s1:~$ iperf -s -y C  
20140422105054,172.16.10.76,41065,172.16.10.65,5001,6,0.0-20.0,73138176,29215083
20140422105054,172.16.10.76,5001,172.16.10.65,56254,4,0.0-20.0,46350336,18502933
20140422105100,172.16.10.76,54550,172.16.10.50,5001,8,0.0-20.0,67895296,27129408
20140422105100,172.16.10.76,5001,172.16.10.50,58447,5,0.0-20.1,50937856,20292796
20140422105553,172.16.10.76,5001,172.16.10.65,47382,7,0.0-20.1,51118080,20358083
20140422105553,172.16.10.76,41067,172.16.10.65,5001,5,0.0-20.1,76677120,30524007
20140422105600,172.16.10.76,5001,172.16.10.50,40734,4,0.0-20.0,57606144,23001066
20140422105600,172.16.10.76,54552,172.16.10.50,5001,8,0.0-20.0,70123520,28019115
20140422110053,172.16.10.76,41070,172.16.10.65,5001,5,0.0-20.1,63438848,25284066
20140422110053,172.16.10.76,5001,172.16.10.65,46462,6,0.0-20.1,11321344,4497094

我想要映射到的字段是:timestamp,server_ip,server_port,client_ip,client_port,tag_id,interval,transfer,bandwidth

我想定期将此CSV输出映射到MySQL数据库,为此我明白我必须编写一个Python脚本(在cron作业内)查询和存储在MySQL数据库中。我是Python脚本和数据库查询的初学者。

我在[https://serverfault.com/questions/566737/iperf-csv-output-format]处进行了关于服务器故障的另一次讨论;并希望基于此构建我的查询。

3 个答案:

答案 0 :(得分:0)

如果您计划使用Python,那么我建议您使用sqlalchemy

一般方法是:

  • 定义类,其中包含您要存储的所有属性
  • 将类的所有属性映射到数据库列和类型
  • 从csv中读取您的数据(使用例如csv模块),为每一行创建相应的对象,即之前准备的类,并让它存储。
sqlalchemy将为您提供更多详细信息和说明,您的要求似乎相当容易。

其他选择是查找现有的csv导入工具,其中一些已经可用于MySQL,还有很多其他工具。

答案 1 :(得分:0)

这可能不是您正在寻找的那种答案,但是如果您通过执行基础教程在线,你会发现你的问题根本不难解决。然后只需使用标准计时器,例如time.sleep()来重复该过程。

答案 2 :(得分:0)

生成SQL脚本,然后运行它

如果您不想使用sqlalchemy等复杂解决方案,可以采用以下方法。

  1. 拥有您的csv数据,将它们转换为SQL脚本
  2. 使用mysql命令行工具运行此脚本
  3. 在您第一次这样做之前,请确保在数据库中创建所需的数据库结构(我留给您)。

    我的以下示例使用(仅为方便起见)包docopt,因此您需要安装它:

    $ pip install docopt
    

    CSV到SQL脚本转换实用程序

    csv2sql.py

    """
    Usage:
        csv2sql.py [--table <tablename>] <csvfile>
    
    Options:
        --table <tablename>  Name of table in database to import into [default: mytable]
    
    Convert csv file with iperf data into sql script for importing
    those data into MySQL database.
    """
    from csv import DictReader
    from docopt import docopt
    
    if __name__ == "__main__":
        args = docopt(__doc__)
        fname = args["<csvfile>"]
        tablename = args["--table"]
        headers = ["timestamp",
                   "server_ip",
                   "server_port",
                   "client_ip",
                   "client_port",
                   "tag_id",
                   "interval",
                   "transferred",
                   "bandwidth"
                  ]
        sql = """insert into {tablename}
        values ({timestamp},"{server_ip}",{server_port},"{client_ip}",{client_port},{tag_id},"{interval}",{transferred},{bandwidth});"""
        with open(fname) as f:
            reader = DictReader(f, headers, delimiter=",")
            for rec in reader:
                print(sql.format(tablename=tablename, **rec)) # python <= 2.6 will fail here
    

    将CSV转换为SQL脚本

    首先让转换实用程序介绍:

    $ python csv2sql.py -h
    Usage:
        csv2sql.py [--table <tablename>] <csvfile>
    
    Options:
        --table <tablename>  Name of table in database to import into [default: mytable]
    
    Convert csv file with iperf data into sql script for importing
    those data into MySQL database.
    

    将您的数据保存在文件data.csv中:

    $ python csv2sql.py data.csv 
    insert into mytable
        values (20140422105054,"172.16.10.76",41065,"172.16.10.65",5001,6,"0.0-20.0",73138176,29215083);
    insert into mytable
        values (20140422105054,"172.16.10.76",5001,"172.16.10.65",56254,4,"0.0-20.0",46350336,18502933);
    insert into mytable
        values (20140422105100,"172.16.10.76",54550,"172.16.10.50",5001,8,"0.0-20.0",67895296,27129408);
    insert into mytable
        values (20140422105100,"172.16.10.76",5001,"172.16.10.50",58447,5,"0.0-20.1",50937856,20292796);
    insert into mytable
        values (20140422105553,"172.16.10.76",5001,"172.16.10.65",47382,7,"0.0-20.1",51118080,20358083);
    insert into mytable
        values (20140422105553,"172.16.10.76",41067,"172.16.10.65",5001,5,"0.0-20.1",76677120,30524007);
    insert into mytable
        values (20140422105600,"172.16.10.76",5001,"172.16.10.50",40734,4,"0.0-20.0",57606144,23001066);
    insert into mytable
        values (20140422105600,"172.16.10.76",54552,"172.16.10.50",5001,8,"0.0-20.0",70123520,28019115);
    insert into mytable
        values (20140422110053,"172.16.10.76",41070,"172.16.10.65",5001,5,"0.0-20.1",63438848,25284066);
    insert into mytable
        values (20140422110053,"172.16.10.76",5001,"172.16.10.65",46462,6,"0.0-20.1",11321344,4497094);
    

    将所有内容放入文件data.sql

    $ python csv2sql.py data.csv > data.sql
    

    data.sql应用于MySQL数据库

    最后使用mysql命令(由MySQL提供)导入数据库:

    $ myslq --user username --password password db_name < data.sql