-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.py
37 lines (33 loc) · 1.11 KB
/
mongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from pymongo import MongoClient
from bson import ObjectId
import settings
class BatchedMongoSaver:
def __init__(self, collection, batch_size=1000):
# self.db = settings.db
self.collection = collection
self.buffer = []
self.batch_size = batch_size
def add(self, data):
"""Agrega datos al buffer y guarda en MongoDB si se alcanza el tamaño del lote."""
# if '_id' not in data: data['_id'] = ObjectId()
self.buffer.append(data)
if len(self.buffer) >= self.batch_size:
self.flush()
# return data['_id']
def flush(self):
"""Guarda los datos en MongoDB y limpia el buffer."""
if self.buffer:
try:
self.collection.insert_many(self.buffer,ordered=False)
except:
pass
self.buffer = []
def close(self):
"""Guarda cualquier dato restante y cierra la conexión."""
self.flush()
# self.client.close()
# Uso:
# saver = BatchedMongoSaver("all_data_sync", batch_size=1000)
# for data in data_generator():
# saver.add(data)
# saver.close()