Python 用luigi任务替换表加载函数

Python 用luigi任务替换表加载函数,python,luigi,Python,Luigi,我有一个python函数,它将数据从另外两个表加载到sql server表中 def load_table(date1,date2): strDate1 = date1.strftime('%m/%d/%Y') strDate2 = date2.strftime('%m/%d/%Y') stmt = "insert into Agent_Queue (ID) select distinct Send_Location_ID from Pretty_Txns where

我有一个python函数,它将数据从另外两个表加载到sql server表中

def load_table(date1,date2):
    strDate1 = date1.strftime('%m/%d/%Y')
    strDate2 = date2.strftime('%m/%d/%Y')
    stmt = "insert into Agent_Queue (ID)  select distinct Send_Location_ID from Pretty_Txns where Send_Date >= '%s' and Send_Date <= '%s' and Send_Location_ID is not null union select distinct Pay_Location_ID  from Pretty_Txns where Pay_Date >= '%s' and Pay_Date <= '%s' and Pay_Location_ID is not null" % (strDate1,strDate2,strDate1,strDate2)
    cnx1= connection string
    self.curs=cnx1.cursor()
    self.curs.execute(stmt)
    self.curs.commit()
我正在定义DateTask,因此错误让我感到困惑

另外,是否所有任务都需要所有3个requires()运行输出

此外,是否需要始终将输出写入文件?
当谈到使用luIgi时,它是全新的,因此非常感谢您的任何意见

我认为这样做会更好:

class LoadTask(luigi.Task):
    date1 = luigi.DateParameter()
    date2 = luigi.DateParameter()        

    def requires(self):
        return None

    def output(self):
        return luigi.LocalTarget("{0}-{1}.txt".format(self.date1, self.date2))

    def run(self):
        strDate1 = self.date1.strftime('%m/%d/%Y')
        strDate2 = self.date2.strftime('%m/%d/%Y')
        stmt = "insert into Agent_Queue (ID)  select distinct Send_Location_ID from Pretty_Txns where Send_Date >= '%s' and Send_Date <= '%s' and Send_Location_ID is not null union select distinct Pay_Location_ID  from Pretty_Txns where Pay_Date >= '%s' and Pay_Date <= '%s' and Pay_Location_ID is not null" % (strDate1,strDate2,strDate1,strDate2)
        curs=cnx1.cursor()
        curs.execute(stmt)
        curs.commit()
        curs.close()
        with self.output().open('w') as out_file:
            print >> out_file, strDate1, strDate2
另外,是否所有任务都需要所有3个requires()、run和output

对。尽管有一些任务类型,例如
luigi.WrapperTask
不需要
output()
,如果您是链中的第一个任务,则可以从
requires()
返回
None
,等等

此外,是否需要始终将输出写入文件

不可以。例如,SQL Alchemy contrib模块定义了一个目标子类,您可以将其用作数据库中的目标。

我认为这样做会更好:

class LoadTask(luigi.Task):
    date1 = luigi.DateParameter()
    date2 = luigi.DateParameter()        

    def requires(self):
        return None

    def output(self):
        return luigi.LocalTarget("{0}-{1}.txt".format(self.date1, self.date2))

    def run(self):
        strDate1 = self.date1.strftime('%m/%d/%Y')
        strDate2 = self.date2.strftime('%m/%d/%Y')
        stmt = "insert into Agent_Queue (ID)  select distinct Send_Location_ID from Pretty_Txns where Send_Date >= '%s' and Send_Date <= '%s' and Send_Location_ID is not null union select distinct Pay_Location_ID  from Pretty_Txns where Pay_Date >= '%s' and Pay_Date <= '%s' and Pay_Location_ID is not null" % (strDate1,strDate2,strDate1,strDate2)
        curs=cnx1.cursor()
        curs.execute(stmt)
        curs.commit()
        curs.close()
        with self.output().open('w') as out_file:
            print >> out_file, strDate1, strDate2
另外,是否所有任务都需要所有3个requires()、run和output

对。尽管有一些任务类型,例如
luigi.WrapperTask
不需要
output()
,如果您是链中的第一个任务,则可以从
requires()
返回
None
,等等

此外,是否需要始终将输出写入文件

不可以。例如,SQL Alchemy contrib模块定义了一个目标子类,您可以将其用作数据库中的目标。

任务类定义中有一个输入错误:它应该是
DateTask
,而不是
DateTask
(t必须是大写!),除此之外,DateTask的方法输出必须返回
luigi.Target
的子类,而不是参数.ohh..typo很愚蠢..我读过关于使用luigi.Target在本地将其写入某个文本文件的内容。没有其他方法可以访问date1和date2?任务类定义中有一个输入错误:它应该是
DateTask
,而不是
DateTask
(t必须是大写!),除此之外,DateTask的方法输出必须返回
luigi.Target
的子类,而不是参数.ohh..typo很愚蠢..我读过关于使用luigi.Target在本地将其写入某个文本文件的内容。没有其他方法可以访问date1和date2?
class LoadTask(luigi.Task):
    date1 = luigi.DateParameter()
    date2 = luigi.DateParameter()        

    def requires(self):
        return None

    def output(self):
        return luigi.LocalTarget("{0}-{1}.txt".format(self.date1, self.date2))

    def run(self):
        strDate1 = self.date1.strftime('%m/%d/%Y')
        strDate2 = self.date2.strftime('%m/%d/%Y')
        stmt = "insert into Agent_Queue (ID)  select distinct Send_Location_ID from Pretty_Txns where Send_Date >= '%s' and Send_Date <= '%s' and Send_Location_ID is not null union select distinct Pay_Location_ID  from Pretty_Txns where Pay_Date >= '%s' and Pay_Date <= '%s' and Pay_Location_ID is not null" % (strDate1,strDate2,strDate1,strDate2)
        curs=cnx1.cursor()
        curs.execute(stmt)
        curs.commit()
        curs.close()
        with self.output().open('w') as out_file:
            print >> out_file, strDate1, strDate2
luigi --module load_task LoadTask --date1 2017-01-01 --date2 2017-01-02 --local-scheduler