首页 技术 正文
技术 2022年11月18日
0 收藏 631 点赞 3,194 浏览 2668 个字

# -*- 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开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848