Hadoop + Python = Happy

 
Project Information
Starred by 42 user s
Members
colinhev...@gmail.com
Featured
Downloads

Hadoop + Python = Happy

Happy is a framework for writing map-reduce programs for Hadoop using Jython. It files off the sharp edges on Hadoop and makes writing map-reduce programs a breeze.

The current release is 0.1, but we've been using it for a long time at http://www.freebase.com for mining data, so rest assured that it is stable and full-featured.

The 0.1 release is compiled against Hadoop 0.17.2. We'll be releasing a version compiled against 0.18.1 as soon as we upgrade our cluster.

You can download Happy here. Documentation and examples are here.

Join the Happy discussion group here if you have questions or find a bug.

Happy Overview

Happy is a framework that allows Hadoop jobs to be written and run in Python 2.2 using Jython. It is an easy way to write map-reduce programs for Hadoop, and includes some new useful features as well. The current release supports Hadoop 0.17.2.

Map-reduce jobs in Happy are defined by sub-classing happy.HappyJob and implementing a map(records, task) and reduce(key, values, task) function. Then you create an instance of the class, set the job parameters (such as inputs and outputs) and call run().

When you call run(), Happy serializes your job instance and copies it and all accompanying libraries out to the Hadoop cluster. Then for each task in the Hadoop job, your job instance is de-serialized and map or reduce is called.

The task results are written out using a collector, but aggregate statistics and other roll-up information can be stored in the happy.results dictionary, which is returned from the run() call.

Jython modules and Java jar files that are being called by your code can be specified using the environment variable HAPPY_PATH. These are added to the Python path at startup, and are also automatically included when jobs are sent to Hadoop. The path is stored in happy.path and can be edited at runtime.

Obligatory Wordcount Example

Here's an example of word count implemented in Happy:

import sys, happy, happy.log

happy
.log.setLevel("debug")
log
= happy.log.getLogger("wordcount")

class WordCount(happy.HappyJob):  
   
def __init__(self, inputpath, outputpath):
        happy
.HappyJob.__init__(self)
       
self.inputpaths = inputpath
       
self.outputpath = outputpath
       
self.inputformat = "text"
     
   
def map(self, records, task):
       
for _, value in records:
           
for word in value.split():
                task
.collect(word, "1")
   
   
def reduce(self, key, values, task):
        count
= 0;
       
for _ in values: count += 1
        task
.collect(key, str(count))
        log
.debug(key + ":" + str(count))
        happy
.results["words"] = happy.results.setdefault("words", 0) + count
        happy
.results["unique"] = happy.results.setdefault("unique", 0) + 1

if __name__ == "__main__":
   
if len(sys.argv) < 3:
       
print "Usage: <inputpath> <outputpath>"
        sys
.exit(-1)
    wc
= WordCount(sys.argv[1], sys.argv[2])
    results
= wc.run()
   
print str(sum(results["words"])) + " total words"
   
print str(sum(results["unique"])) + " unique words"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值