luigi 学习
1、mac 上安装luigi
pip install luigi
pip install boto3 (luigi依赖 boto3)
2、基本概念
class Streams(luigi.Task):
"""
Faked version right now, just generates bogus data.
"""
date = luigi.DateParameter()
def run(self):
"""
Generates bogus data and writes it into the :py:meth:`~.Streams.output` target.
"""
with self.output().open('w') as output:
for _ in range():
output.write('{} {} {}\n'.format(
random.randint(, ),
random.randint(, ),
random.randint(, )))
def output(self):
"""
Returns the target output for this task.
In this case, a successful execution of this task will create a file in the local file system.
:return: the target output for this task.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(self.date.strftime('data/streams_%Y_%m_%d_faked.tsv'))
class AggregateArtists(luigi.Task):
"""
This task runs over the target data returned by :py:meth:`~/.Streams.output` and
writes the result into its :py:meth:`~.AggregateArtists.output` target (local file).
"""
date_interval = luigi.DateIntervalParameter()
def output(self):
"""
Returns the target output for this task.
In this case, a successful execution of this task will create a file on the local filesystem.
:return: the target output for this task.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget("data/artist_streams_{}.tsv".format(self.date_interval))
def requires(self):
"""
This task's dependencies:
* :py:class:`~.Streams`
:return: list of object (:py:class:`luigi.task.Task`)
"""
return [Streams(date) for date in self.date_interval]
def run(self):
artist_count = defaultdict(int)
for t in self.input():
with t.open('r') as in_file:
for line in in_file:
_, artist, track = line.strip().split()
artist_count[artist] +=
with self.output().open('w') as out_file:
for artist, count in six.iteritems(artist_count):
out_file.write('{}\t{}\n'.format(artist, count))
run()是这个task要执行的内容
requires()是这个task所依赖的任务,这里依赖一系列的Stream
output()是这个task的输出
input()这个是所依赖的task产生的输出
二、使用central planner
先用
luigid --background --pidfile <PATH_TO_PIDFILE> --logdir <PATH_TO_LOGDIR> --state-path <PATH_TO_STATEFILE>
打开liguid server
然后运行任务,比如:
luigi --module top_artists2 Top10Artists --date-interval 2012-06
注意,要去掉 --local-scheduler
然后可以用 localhost:8082来访问现在的任务
如果A -> B,A依赖B,那么B的output可以在A里面直接用input()来使用,如果B的output是若干文件的话,那么在A中的input()也是若干文件,可以用for循环来读取
luigi 学习的更多相关文章
- luigi学习3-使用luigid
--local-scheduler的方式只适用于开发调试阶段,当你真正要把程序部署到一个产品时,我们推荐使用luigid服务. 使用luigid服务不但能提供锁服务(防止一个任务被多个进程重复执行), ...
- luigi学习9--执行模型
luigi的执行和触发模型非常简单. 一.luigi的执行模型 当你执行一个luigi的工作流的时候,worker调度所有的task,并且执行task在一个单独的进程中. 这种scheme最大的好处是 ...
- luigi学习8--使用中央调度器
--local-scheduler一般用在开发阶段,这在一个产品中是不建议这样使用的.使用中央调度器有两个目的: 保证两个相同的task不会同时运行两次 提供一个可视化的界面 注意:中央调度器并不会帮 ...
- luigi学习7--running from command line
最简单去运行一个luigi task的方式是通过luigi命令行工具. 示例代码: # my_module.py, available in your sys.path import luigi cl ...
- luigi学习6--parameters详解
parameter就好比是一个task的构造方法.luigi要求你在类的scope上定义parameter. 如下面就是一个定义parameter的例子: class DailyReport(luig ...
- luigi学习5-task详解
task是代码执行的地方.task通过target互相依赖. 下面是一个典型的task的大纲视图. 一.Task.requires requires方法用来指定本task的依赖的其他task对象,依赖 ...
- luigi学习4-构建工作流
luigi提供了两个基本单元来构造一个工作流,这两个基本单元分别是Task和Target.这两个单元都是抽象类,我们实现他们中的某些方法就可以了.除了这两个基本单元,还有一个重要的概念是Pramete ...
- luigi学习-luigi的配置文件
一.luigi配置文件的加载顺序 /etc/luigi/client.cfg luigi.cfg LUIGI_CONFIG_PATH环境变量 二.配置文件分节 配置文件被分为了多个section,每一 ...
- luigi学习2-在hadoop上运行Top Artists
一.AggregateArtistsHadoop class AggregateArtistsHadoop(luigi.contrib.hadoop.JobTask): date_interval = ...
- luigi学习1
一.luigi介绍 luigi是基于python语言的,可帮助建立复杂流式批处理任务管理系统.这些批处理作业典型的有hadoop job,数据库数据的导入与导出,或者是机器学习算法等等. luigi的 ...
随机推荐
- maven win 安装 与 IntelliJ IDEA 配置Maven【2018-11-14最新最有姿势攻略】
[博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708] http ...
- overload(重载)和override(覆盖)的注意点
使用overload(重载)的几个注意点: 在使用重载时只能通过不同的参数形式.例如:不同的参数类型,不同的参数个数,不同的参数顺序,当然,同一个方法内的几个参数类型必须不一样.例如可以是fun(in ...
- 基于FPGA的1553B通信模块的设计(转)
reference:http://www.21ic.com/app/eda/201808/798483.htm https://www.milstd1553.com/ [导读] 摘 要: 提出一种将F ...
- Master-Worker设计模式介绍
Master-Worker模式是常用的并行设计模式.核心思想是,系统由两个角色组成,Master和Worker,Master负责接收和分配任务,Worker负责处理子任务.任务处理过程中,Master ...
- 事件对象的属性,基于jQuery(jquery针对不同浏览器进行了兼容性的封装)
1. event.type(该方法是获取到事件的类型) $( 'a' ).click( function( event ){ alert( event.type ); //click return f ...
- Hide Data into bitmap with ARGB8888 format
将保存重要信息,如银行卡密码的文本文件隐藏到ARGB8888的A通道. bitmap.h #ifndef BMP_H #define BMP_H #include <fstream> #i ...
- 唯一分解定理(以Minimun Sum LCM UVa 10791为例)
唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...
- 15.python并发编程(线程--进程--协程)
一.进程:1.定义:进程最小的资源单位,本质就是一个程序在一个数据集上的一次动态执行(运行)的过程2.组成:进程一般由程序,数据集,进程控制三部分组成:(1)程序:用来描述进程要完成哪些功能以及如何完 ...
- String引用数据类型
一.String类的第一种方式 (原文地址:https://blog.csdn.net/wangdajiao/article/details/52087302)1.直接赋值 例:String str ...
- PythonStudy——函数的返回值 The return value of the function
# 在函数体中,通过return关键词返回函数的内部数据给外部 """# 一.作用# return作用:1.结束函数:2.将函数的内部数据返回给外部 def fn(): ...