第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Luigi 動態(tài)檢查輸出

如何使用 Luigi 動態(tài)檢查輸出

動漫人物 2021-11-09 16:33:10
我意識到我可能需要使用動態(tài)需求來完成以下任務(wù),但是我一直無法理解這在實(shí)踐中會是什么樣子。目標(biāo)是使用 Luigi 生成數(shù)據(jù)并將其添加到數(shù)據(jù)庫中,而無需提前知道將生成什么數(shù)據(jù)。以使用 mongodb 為例:import luigifrom uuid import uuid4from luigi.contrib import mongodbimport pymongo# Make up IDs, though in practice the IDs may be generated from an APIclass MakeID(luigi.Task):    def run(self):        with self.output().open('w') as f:            f.write(','.join([str(uuid4()) for e in range(10)]))    # Write the data to file    def output(self):        return luigi.LocalTarget('data.csv')class ToDataBase(luigi.Task):    def requires(self):        return MakeID()    def run(self):        with self.input().open('r') as f:            ids = f.read().split(',')        # Add some fake data to simulate generating new data         count_data = {key: value for value, key in enumerate(ids)}        # Add data to the database        self.output().write(count_data)    def output(self):        # Attempt to read non-existent file to get the IDs to check if task is complete        with self.input().open('r') as f:            valid_ids = f.read().split(',')        client = pymongo.MongoClient('localhost',                                     27017,                                     ssl=False)        return mongodb.MongoRangeTarget(client,                                        'myDB',                                        'myData',                                        valid_ids,                                        'myField')if __name__ == '__main__':    luigi.run()目標(biāo)是獲取數(shù)據(jù),對其進(jìn)行修改,然后將其添加到數(shù)據(jù)庫中。上面的代碼在運(yùn)行時失敗,因?yàn)閛utput方法在方法ToDataBase之前運(yùn)行,require所以雖然函數(shù)可以訪問輸入,但輸入尚不存在。無論如何,我仍然需要檢查以確保數(shù)據(jù)已添加到數(shù)據(jù)庫中。這個github 問題與我正在尋找的很接近,盡管正如我所提到的,我在實(shí)踐中無法弄清楚這個用例的動態(tài)需求。
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個贊

解決方案是創(chuàng)建第三個任務(wù)(在示例中Dynamic),它產(chǎn)生等待動態(tài)輸入的任務(wù),并使依賴項(xiàng)成為參數(shù)而不是requires方法。


class ToDatabase(luigi.Task):

    fp = luigi.Parameter()


    def output(self):

        with open(self.fp, 'r') as f:

            valid_ids = [str(e) for e in f.read().split(',')]

        client = pymongo.MongoClient('localhost', 27017, ssl=False)

        return mongodb.MongoRangeTarget(client, 'myDB', 'myData',

                                        valid_ids, 'myField')


    def run(self):

        with open(self.fp, 'r') as f:

            valid_ids = [str(e) for e in f.read().split(',')]

        self.output().write({k: 5 for k in valid_ids})



class Dynamic(luigi.Task):

    def output(self):

        return self.input()


    def requires(self):

        return MakeIDs()


    def run(self):

        yield(AddToDatabase(fp=self.input().path))


查看完整回答
反對 回復(fù) 2021-11-09
  • 1 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號