MongoDB基本函數(shù)應(yīng)用
__author__ = 'tianmh'
from pymongo import MongoClient
def get_db():
#建立鏈接
client = MongoClient("localhost", 27017)
#test,還有其他寫法
db = client.test
return db
def get_collection(db):
#選擇集合(mongo中collection和database都是lazy創(chuàng)建的,具體可以google下)
collection = db['posts']
print collection
def insert_one_doc(db):
#插入一個document
posts = db.posts
post = {"name":"lzz", "age":25, "weight":"55"}
post_id = posts.insert(post)
print post_id
def insert_mulit_docs(db):
#批量插入documents,插入一個數(shù)組
posts = db.posts
post = [ {"name":"nine", "age":28, "weight":"55"},
{"name":"jack", "age":25, "weight":"55"}]
obj_ids = posts.insert(post)
print obj_ids
##查詢,可以對整個集合查詢,可以根ObjectId查詢,可以根據(jù)某個字段查詢等
def get_all_colls(db):
#獲得一個數(shù)據(jù)庫中的所有集合名稱
print db.collection_names()
def get_one_doc(db):
#有就返回一個,沒有就返回None
posts = db.posts
print posts.find_one()
print posts.find_one({"name":"jack"})
print posts.find_one({"name":"None"})
return
def get_one_by_id(db):
#通過objectid來查找一個doc
posts = db.posts
obj = posts.find_one()
obj_id = obj["_id"]
print "_id 為ObjectId類型 :"
print posts.find_one({"_id":obj_id})
#需要注意這里的obj_id是一個對象,不是一個str,使用str類型作為_id的值無法找到記錄
print "_id 為str類型 "
print posts.find_one({"_id":str(obj_id)})
#可以通過ObjectId方法把str轉(zhuǎn)成ObjectId類型
from bson.objectid import ObjectId
print "_id 轉(zhuǎn)換成ObjectId類型"
print posts.find_one({"_id":ObjectId(str(obj_id))})
def get_many_docs(db):
#mongo中提供了過濾查找的方法,可以通過各
#種條件篩選來獲取數(shù)據(jù)集,還可以對數(shù)據(jù)進行計數(shù),排序等處理
posts = db.posts
#所有數(shù)據(jù),按年齡排序, -1是倒序
all =? posts.find().sort("age", -1)
count = posts.count()
print "集合中所有數(shù)據(jù) %s個"%int(count)
for i in all:
print i
#條件查詢
count = posts.find({"name":"lzz"}).count()
print "lzz: %s"%count
for i in? posts.find({"name":"lzz", "age":{"$lt":20}}):
print i
def clear_coll_datas(db):
#清空一個集合中的所有數(shù)據(jù)
db.posts.remove({})
if __name__ == "__main__":
db = get_db()
obj_id = insert_one_doc(db)
obj_ids = insert_mulit_docs(db)
#get_all_colls(db)
#get_one_doc(db)
#get_one_by_id(db)
#get_many_docs(db)
clear_coll_datas(db)
性能測試總結(jié):
盡管Mongodb單機性能不如帶PCIE卡的SQLServer,但是一旦分片,性能將提升幾倍
在分4片的情況下,寫入可達60000條/s
在分5片的情況下,寫入可達80000條/s
值得一提的是,由于分片不均勻,導(dǎo)致90%的數(shù)據(jù)僅使用了60%的時間,剩下10%的數(shù)據(jù)僅在單點插入,所以性能下降的很厲害,如果分片足夠多,我們的片鍵足夠散列,讓每片存儲平均,性能還能有進一步提升。
另外有大神知道,怎么讓分片的數(shù)據(jù)散列的更平均一點呢?因為mongodb分片用的一致性哈希,好像每個片節(jié)點無法設(shè)置虛擬節(jié)點,利用他原來的objectid也無法很平均的分片,望告知一二。
3、匯總總結(jié):
想要達到1億條數(shù)據(jù)10分鐘插入,必須達到插入速度在16萬/秒,所以如果我們將分片達到10片,理論上就可以達到1億條數(shù)據(jù)10分鐘的插入了,由于機器有限無法完成測試,不過最終20分鐘的插入速度也讓人滿意了
簡單案例分析:將10w個數(shù)據(jù)寫入mongodb數(shù)據(jù)庫內(nèi)部,簡單測試
代碼:
__author__ = 'tianmh'
import pymongo
import json
import datetime,time
import copy
import sys, os
def getTimestampFromDatetime(d=None):
if d is None:
d = datetime.time.now()
return time.mktime(d.timetuple())
if __name__ == '__name__':
url = 'localhost'
port = 27017
max_pool_size = 50
start = getTimestampFromDatetime()
client = pymongo.MongoClient(url, port, max_pool_size)
db = client.testdb
saveData = []? #直接添加要存儲的數(shù)據(jù)
for i in range(0, 100000):
saveData.append(
{
'count':i
}
)
db.test2.insert(saveData)
end = getTimestampFromDatetime()
print('time : {0}s'.format(end-start))