lbp0200 / blog

1 stars 0 forks source link

Python数据从bdb到mongodb #15

Open lbp0200 opened 8 years ago

lbp0200 commented 8 years ago
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import bsddb
import json
from pymongo import MongoClient

def updateData(db,data):
    print 'fuck'

client = MongoClient()
mongodb = client.test
mongodb.restaurants.delete_many({})

db = bsddb.btopen('/home/lbp/tmp/20160317_day.bdb', 'c')
f = db.first()
print f
data = {'date': '20160317', 'uri': f[0]}
data.update(json.loads(f[1]))
result = mongodb.restaurants.update({'date': data['date'], 'uri': data['uri']}, data, True)
print result
while 1:
    f = db.next()
    if not f:
        break
    else:
        data = {'date': '20160317', 'uri': f[0]}
        data.update(json.loads(f[1]))
        result = mongodb.restaurants.update({'date': data['date'], 'uri': data['uri']}, data, True)
        print result
print mongodb.restaurants.count()
lbp0200 commented 8 years ago
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import bsddb
import json
import time
from pymongo import MongoClient

client = MongoClient()
mongodb = client.test
mongodb.bdb.drop()

db = bsddb.btopen('/home/lbp/tmp/20160317_day.bdb', 'c')
index = 0
dlist = []
startTime = time.time()
while 1:
    try:
        f = db.next()
    except :
        mongodb.bdb.insert_many(dlist)
        break
    else:
        data = {'date': '20160317', 'uri': f[0]}
        data.update(json.loads(f[1]))
        dlist.append(data)
        index = index + 1
        if index >= 10000:
            tTime = time.time()
            mongodb.bdb.insert_many(dlist)
            print("--- %s seconds ---" % (time.time() - tTime))
            dlist = []
            index = 0
            print mongodb.bdb.count()
            # print result
print("--- %s seconds ---" % (time.time() - startTime))