最近做的事情是用mrjob写mapreduce程序,从mongo读取数据。我的做法很容易也很好懂,因为mrjob可以支持sys.stdin的读取,所以我考虑用一个python程序读mongo中的数据,然后同时让mrjob脚本接受输入,处理,输出。

具体方式:

readInMongoDB.py:

#coding:UTF-8
'''
Created on 2014年5月28日 @author: hao
'''
import pymongo
pyconn = pymongo.Connection(host,port=27017)
pycursor = pyconn.userid_cid_score.find().batch_size(30)
for i in pycursor:
userId = i['userId']
cid = i['cid']
score = i['score']
# temp = list()
# temp.append(userId)
# temp.append(cid)
# temp.append(score)
print str(userId)+','+str(cid)+','+str(score)
 

step1.py:

#coding:UTF-8
'''
Created on 2014年5月27日 @author: hao
'''
from mrjob.job import MRJob
# from mrjob import protocol
import pymongo
import logging
import simplejson as sj class step(MRJob):
'''
'''
# logging.c
def parseMatrix(self, _, line):
'''
input one stdin for pymongo onetime search
output contentId, (userId, rating)
'''
line = (str(line))
line=line.split(',')
userId = line[0]
# print userId
cid = line[1]
# print cid
score = float(line[2])
# print score
yield cid, (userId, float(score)) def scoreCombine(self, cid, userRating):
'''
将对同一个内容的(用户,评分)拼到一个list里
'''
userRatings = list()
for i in userRating:
userRatings.append(i)
yield cid, userRatings def userBehavior(self, cid, userRatings):
'''
'''
scoreList = list()
for doc in userRatings:
# 每个combiner结果
for i in doc:
scoreList.append(i)
for user1 in scoreList:
for user2 in scoreList:
if user1[0] == user2[0]:
continue
yield (user1[0], user2[0]), (user1[1], user2[1]) def steps(self):
return [self.mr(mapper = self.parseMatrix,
reducer = self.scoreCombine),
self.mr(reducer = self.userBehavior),] if __name__=='__main__': fp = open('a.txt','w')
fp.write('[')
step.run()
fp.write(']')
fp.close()

然后执行脚本  python readInMongoDB.py | python step1.py >> out.txt

这个方式在本地执行的非常好,没有任何问题(除开mrjob速度的问题,其实在本次应用中影响不大)

原文:http://blog.csdn.net/whzhcahzxh/article/details/29587059

mrjob 使用 mongoldb 数据源【转】的更多相关文章

  1. mrjob 使用 mongodb 作为数据源

    When using a mongoDB collection as input, add the arguments -jobconf mongo.input.uri=<input mongo ...

  2. springmvc 多数据源 SSM java redis shiro ehcache 头像裁剪

    获取下载地址   QQ 313596790  A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:31359679 ...

  3. springmvc SSM shiro redis 后台框架 多数据源 代码生成器

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单 下载地址    ; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类 ...

  4. springmvc SSM 多数据源 shiro redis 后台框架 整合

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单 下载地址    ; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类 ...

  5. 基于注解的Spring多数据源配置和使用

    前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...

  6. spring boot(七):springboot+mybatis多数据源最简解决方案

    说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务.我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解 ...

  7. spring 多数据源一致性事务方案

    spring 多数据源配置 spring 多数据源配置一般有两种方案: 1.在spring项目启动的时候直接配置两个不同的数据源,不同的sessionFactory.在dao 层根据不同业务自行选择使 ...

  8. [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合

    [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合 Datasets can often contain components of that require differe ...

  9. C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)

    目       录 工业物联网和集成系统解决方案的技术路线... 1 前言... 1 第一章           系统架构... 3 1.1           硬件构架图... 3 1.2      ...

随机推荐

  1. 面向移动设备的html5开发框架

    很久以前整理了篇将手机网站做成手机应用的JS框架.时隔一年多,很多新的技术已经出现,下面再来总结下还有哪些框架是适合面向手机设备的开发的. 1.jQuery Mobile jQuery Mobile ...

  2. php 时间加减

    <?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...

  3. knockoutJS学习笔记04:监控属性

    一.语法介绍 先来看一个简单的例子: <span data-bind="text:name"></span> var obj = {name:ko.obse ...

  4. 淘宝WAP版小BUG分析

    前几天发现的一个淘宝WAP版的小BUG,就是用桌面版chrome看的时候产品评价中的图片显示不出来,都是图裂了. 这是什么原因呢?图片为什么会显示不出来呢?淘宝的技术人员.测试人员不可能没发现啊.开启 ...

  5. 如何指定个别属性进行transition过渡

    transition是CSS3新增的动画属性,可以实现属性的平滑过渡,大大提高用户体验,对于多个属性进行过渡的话很多人会这样写 .tr{ transition:all 1s} 很不幸的是如果我只需要对 ...

  6. Windows Live Writer发布测试

    通过Windows Live Writer发布文章测试!!!!

  7. java 异步处理

    详情请看:http://www.cnblogs.com/yezhenhan/archive/2012/01/07/2315645.html 引入ExecutorService 类 private st ...

  8. 【OpenJudge 1665】完美覆盖

    http://noi.openjudge.cn/ch0405/1665/?lang=zh_CN 状压水题,手动转移 #include<cstdio> #include<cstring ...

  9. UI: 多窗口

    1.自定义帮助类,用于简化 SecondaryView 的管理UI/MultipleViews/SecondaryViewHelper.cs /* * SecondaryViewHelper - 自定 ...

  10. Mvc中域的添加和不同域之间的跳转

    一.在新添加的域中中的 AreaRegistration中作如下设置: 二.在原来的Global.asax中设置: 三.不同域之间的跳转 @Url.Action("Index", ...