使用 Python 导出和映射 CSV 输出到 MySQL 表



我有一个用于网络监控的多客户端到单服务器双向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、transfered、bandwidth

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

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

生成SQL脚本,然后运行它

若您不想使用像sqlalchemy这样的复杂解决方案,以下方法是可行的。

  1. 拥有csv数据,将其转换为SQL脚本
  2. 使用mysql命令行工具运行此脚本

在第一次执行此操作之前,请确保在数据库中创建了所需的数据库结构(我留给您)。

我的以下示例使用(只是为了方便起见)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 

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

一般方法是:

  • 定义类,该类具有要存储的所有属性
  • 将类的所有属性映射到数据库列和类型
  • 从csv中读取数据(例如使用csv模块),为每一行创建相应的对象作为之前准备的类,并将其存储

sqlalchemy将为您提供更多的细节和说明,您的需求似乎相当简单。

另一种选择是找到一个现有的csv导入工具,其中一些已经可以与MySQL一起使用,还有很多其他工具。

这可能不是你想要的答案,但如果你通过在线学习基本教程来学习一点sqlite3(一个本地Python模块-"导入sqlite3"),你会意识到你的问题根本不难解决。然后只需使用标准计时器,如time.sleep()来重复该过程。

相关内容

  • 没有找到相关文章

最新更新