python mongodb MapReduce

來源:互聯網
上載者:User

標籤:

# -*- coding: utf-8 -*-
import os
import csv
import pymongo
from pymongo import MongoClient
from bson.code import Code
from pymongo import MongoClient

 

#建立串連
client = MongoClient(‘10.20.4.79‘, 27017)
#client = MongoClient(‘10.20.66.106‘, 27017)
db_name = ‘ta‘ #資料庫名
db = client[db_name]

 

插入測試資料:

  for i in xrange(1000):
    rID=math.floor(random.random()*10); 
    price = round(random.random()*10,2); 
    if rID < 4:
      db.test.insert({"_id":i,"user":"Joe","product":rID,"price":price}); 
    elif rID>=4 and rID<7:
      db.test.insert({"_id":i,"user":"Josh","product":rID,"price":price}); 
    else:
      db.test.insert({"_id":i,"user":"Ken","product":rID,"price":price});

  結果資料為: 

  { "_id" : 0, "price" : 5.9, "product" : 9, "user" : "Ken" }
  { "_id" : 1, "price" : 7.59, "product" : 7, "user" : "Ken" }
  { "_id" : 2, "price" : 4.72, "product" : 0, "user" : "Joe" }
  { "_id" : 3, "price" : 1.35, "product" : 1, "user" : "Joe" }
  { "_id" : 4, "price" : 2.31, "product" : 0, "user" : "Joe" }
  { "_id" : 5, "price" : 5.29, "product" : 5, "user" : "Josh" }
  { "_id" : 6, "price" : 3.34, "product" : 1, "user" : "Joe" }
  { "_id" : 7, "price" : 7.2, "product" : 4, "user" : "Josh" }
  { "_id" : 8, "price" : 8.1, "product" : 6, "user" : "Josh" }
  { "_id" : 9, "price" : 2.57, "product" : 3, "user" : "Joe" }
  { "_id" : 10, "price" : 0.54, "product" : 2, "user" : "Joe" }
  { "_id" : 11, "price" : 0.66, "product" : 1, "user" : "Joe" }
  { "_id" : 12, "price" : 5.51, "product" : 1, "user" : "Joe" }
  { "_id" : 13, "price" : 3.74, "product" : 6, "user" : "Josh" }
  { "_id" : 14, "price" : 4.82, "product" : 0, "user" : "Joe" }
  { "_id" : 15, "price" : 9.79, "product" : 3, "user" : "Joe" }
  { "_id" : 16, "price" : 9.6, "product" : 5, "user" : "Josh" }
  { "_id" : 17, "price" : 4.06, "product" : 7, "user" : "Ken" }
  { "_id" : 18, "price" : 1.37, "product" : 5, "user" : "Josh" }
  { "_id" : 19, "price" : 6.77, "product" : 9, "user" : "Ken" }

 

測試1、每個使用者各購買了多少個產品?
用SQL語句實現為:select user,count(product) from test group by user

 mapper = Code("""function (){emit(this.user,{count:1})}""")

  reduce = Code("function (key, values) {"
    " var total = 0;"
    " for (var i = 0; i < values.length; i++) {"
    " total += values[i].count;"
    " }"
    " return {count:total};"
    "}")

    result=db.test.map_reduce(mapper,reduce,out =‘myresults‘)

  for doc in db.myresults.find():

    print doc

   測試 2、查詢每個使用者,買了多少商品,總價格,及評價價格   條件是價格大於5的 SQL實現:select user,count(sku),sum(price),      

   round(sum(price)/count(sku),2) as avgPrice from test where prince>5 group by user


  mapper=Code("""function (){emit(this.user,{amount:this.price,count:1,avgPrice:0})}""")

  reduce = Code("function (key, values) {"
    " var res={amount:0,count:0,avgPrice:0};"
    " for (var i = 0; i < values.length; i++) "
    " {"
      " res.count += values[i].count;"
      " res.amount += values[i].amount;"
    " }"
     " res.avgPrice = (res.amount/res.count).toFixed(2);"
    " return res;"
    "}")

  result = db.test.map_reduce(mapper,reduce,out =‘myresults‘,query={‘price‘:{‘$gt‘: 6}})

   for doc in db.myresults.find():
    print doc

 

python mongodb MapReduce

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.