Luigi Pipeline从S3开始

nan*_*nue 12 python amazon-s3 luigi

我的初始文件在AWS S3.有人能指出我需要在一个设置这个Luigi Task吗?

我查看了文档luigi.S3但发现但不清楚我该如何处理,然后我在网上搜索,只获取mortar-luigiluigi顶部的链接和实现.

UPDATE

按照为@matagus提供的示例(我~/.boto也按照建议创建了文件):

# coding: utf-8

import luigi

from luigi.s3 import S3Target, S3Client

class MyS3File(luigi.ExternalTask):
    def output(self):
        return S3Target('s3://my-bucket/19170205.txt')

class ProcessS3File(luigi.Task):

    def requieres(self):
        return MyS3File()

    def output(self):
        return luigi.LocalTarget('/tmp/resultado.txt')

    def run(self):
        result = None

        for input in self.input():
           print("Doing something ...")
           with input.open('r') as f:
               for line in f:
                   result = 'This is a line'

        if result:
            out_file = self.output().open('w')
            out_file.write(result)
Run Code Online (Sandbox Code Playgroud)

当我执行它时没有任何反应

DEBUG: Checking if ProcessS3File() is complete
INFO: Informed scheduler that task   ProcessS3File()   has status   PENDING
INFO: Done scheduling tasks
INFO: Running Worker with 1 processes
DEBUG: Asking scheduler for work...
DEBUG: Pending tasks: 1
INFO: [pid 21171] Worker Worker(salt=226574718, workers=1, host=heliodromus, username=nanounanue, pid=21171) running   ProcessS3File()
INFO: [pid 21171] Worker Worker(salt=226574718, workers=1, host=heliodromus, username=nanounanue, pid=21171) done      ProcessS3File()
DEBUG: 1 running tasks, waiting for next task to finish
INFO: Informed scheduler that task   ProcessS3File()   has status   DONE
DEBUG: Asking scheduler for work...
INFO: Done
INFO: There are no more tasks to run at this time
INFO: Worker Worker(salt=226574718, workers=1, host=heliodromus, username=nanounanue, pid=21171) was stopped. Shutting down Keep-Alive thread
Run Code Online (Sandbox Code Playgroud)

如您所见,消息Doing something...永远不会打印.怎么了?

mat*_*gus 17

这里的关键是定义一个没有输入的外部任务,哪个输出是你在S3中生活的那些文件.Luigi docs在要求另一项任务中提到这一点:

请注意,requires()无法返回Target对象.如果您有一个在外部创建的简单Target对象,则可以将其包装在Task类中

所以,基本上你最终得到这样的东西:

import luigi

from luigi.s3 import S3Target

from somewhere import do_something_with


class MyS3File(luigi.ExternalTask):

    def output(self):
        return luigi.S3Target('s3://my-bucket/path/to/file')

class ProcessS3File(luigi.Task):

    def requires(self):
        return MyS3File()

    def output(self):
        return luigi.S3Target('s3://my-bucket/path/to/output-file')

    def run(self):
        result = None
        # this will return a file stream that reads the file from your aws s3 bucket
        with self.input().open('r') as f:
            result = do_something_with(f)

        # and the you 
        out_file = self.output().open('w')
        # it'd better to serialize this result before writing it to a file, but this is a pretty simple example
        out_file.write(result)
Run Code Online (Sandbox Code Playgroud)

更新:

Luigi使用boto从AWS S3读取文件和/或将其写入AWS S3,因此为了使此代码有效,您需要在boto配置文件中提供您的凭据(在此处~/boto查找其他可能的配置文件位置):

[Credentials]
aws_access_key_id = <your_access_key_here>
aws_secret_access_key = <your_secret_key_here>
Run Code Online (Sandbox Code Playgroud)