简体   繁体   English

Python中的ETL脚本将数据从另一个服务器.csv文件加载到mysql中

[英]ETL script in Python to load data from another server .csv file into mysql

I work as a Business Analyst and new to Python. 我是业务分析师,也是Python的新手。 In one of my project, I want to extract data from .csv file and load that data into my MySQL DB (Staging). 在我的项目之一中,我想从.csv文件中提取数据并将该数据加载到我的MySQL DB(暂存)中。 Can anyone guide me with a sample code and frameworks I should use? 谁能指导我使用我应该使用的示例代码和框架?

If that's a properly formatted CSV file you can use the LOAD DATA INFILE MySQL command and you won't need any python. 如果这是格式正确的CSV文件,则可以使用LOAD DATA INFILE MySQL命令,并且不需要任何python。 Then after it is loaded in the staging area (without processing) you can continue transforming it using sql/etl tool of choice. 然后,将其加载到暂存区中(不进行处理)后,您可以继续使用所选的sql / etl工具对其进行转换。

https://dev.mysql.com/doc/refman/8.0/en/load-data.html https://dev.mysql.com/doc/refman/8.0/en/load-data.html

A problem with that is that you need to add all columns but still even if you have data you don't need you might prefer to load everything in the staging. 这样做的问题是,您需要添加所有列,但是即使您有不需要的数据,您也可能更喜欢在登台中加载所有内容。

Simple program to create sqllite. 创建sqllite的简单程序。 You can read the CSV file and use dynamic_entry to insert into your desired target table. 您可以读取CSV文件,并使用dynamic_entry插入所需的目标表中。

import sqlite3
import time
import datetime
import random

conn = sqlite3.connect('test.db')
c = conn.cursor()

def create_table():
    c.execute('create table if not exists stuffToPlot(unix REAL, datestamp TEXT, keyword TEXT, value REAL)')

def data_entry():
    c.execute("INSERT INTO stuffToPlot VALUES(1452549219,'2016-01-11 13:53:39','Python',6)")
    conn.commit()
    c.close()
    conn.close()

def dynamic_data_entry():
    unix = time.time();
    date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S'))
    keyword = 'python'
    value = random.randrange(0,10)
    c.execute("INSERT INTO stuffToPlot(unix,datestamp,keyword,value) values(?,?,?,?)",
              (unix,date,keyword,value))
    conn.commit()

def read_from_db():
    c.execute('select * from stuffToPlot')
    #data = c.fetchall()
    #print(data)

    for row in c.fetchall():
        print(row)

read_from_db()

c.close()
conn.close()

You can iterate through the data in CSV and load into sqllite3. 您可以遍历CSV中的数据并加载到sqllite3中。 Please refer below link as well. 请同时参考以下链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM