python连接mongodb副本集(PSA)

环境:
Python:3.6.5
mongodb:4.4.22
192.168.1.104:29001 主节点
192.168.1.107:29001 从节点
192.168.1.106:29001 仲裁节点

 

#!/usr/bin/env python
#coding=utf-8
import os, json, urllib, datetime, shutil,random,uuid
from random import choice
import pymysql
import traceback
import time
from pymongo import MongoClient
from pymongo import ReadPreference
from pymongo import InsertOne
import time
import random



gl_mysql_server = "192.168.1.107"
gl_user_name = "root"
gl_password = "mysql"
gl_db_name_msg="db_pushmsg"
gl_port=4000
MongoClient

def insert_data_message01():
    #连接数据库
    conn = MongoClient("mongodb://hxl:hxl123@192.168.1.107:29001,192.168.1.104:29001,192.168.1.106:29001/db_pushmsg?readPreference=secondaryPreferred",maxPoolSize=None)
    my_db = conn['db_pushmsg']
    my_collection = my_db['app_message_all']



    ip_list = ['192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6', '192.168.1.7','192.168.1.8', '192.168.1.9', '192.168.1.10', '192.168.1.11', '192.168.1.12', '192.168.1.13', '192.168.1.14', '192.168.1.15']
    message_type=[0,2,3,6,8,9,16,4096,8196,8448,12292,16385,16386,16387,32770,65536,69632,73728,77824]
    massive_type=[0, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007]
    status = [1000,2000]

    # 批量写
    t0 = time.time()
    data =[]
    for i in range(1, 100001):

        str_i = str(i)
        curr_time = datetime.datetime.now()
        l_user_id = random.randint(10000000, 99999999)
        l_message_type = choice(message_type)
        l_massive_type = choice(massive_type)
        l_status = choice(status)
        l_create_time = str(curr_time)
        l_message_id = random.randint(1000000000000000, 9999999999999999)
        l_sender_seq_no = str(uuid.uuid4())
        l_title = '最新通知,请点击查看' + str_i
        l_message = '亲爱的' + str_i + '家长,镇卫生院电话:'
        l_extra = '{"hospitalId":6347,"vchildCode":"xxx032820161xxx6000xxxx","showMessage":"亲爱的家长,【自2022年8月1日起,门诊接种日变为每周二、】'
        l_send_date = str(curr_time)
        l_deleted = 0

        data.append(InsertOne({"message_id": l_message_id,
                               "user_id": l_user_id,
                               "sender_seq_no": l_sender_seq_no,
                               "title": l_title,
                               "message": l_message,
                               "extra": l_extra,
                               "send_date": l_send_date,
                               "status": l_status,
                               "message_type": l_message_type,
                               "massive_type": l_massive_type,
                               "deleted": l_deleted,
                               "create_time": l_create_time
                               }))
        i+=1
        if (i % 1000) == 0:
            my_collection.bulk_write(data)
            data = []
            i += 1
        else:
            continue
            ##my_collection.bulk_write(data) ##处理不能取mod的数据

    my_collection.bulk_write(data) ##处理不能取mod的数据
    return 0


if __name__ == '__main__':
    print("开始时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    l_flag=insert_data_message01()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

PRIMARY:默认选项,从primary节点读取数据
PRIMARY_PREFERRED:优先从primary节点读取,如果没有primary节点,则从集群中可用的secondary节点读取
SECONDARY:从secondary节点读取数据
SECONDARY_PREFERRED:优先从secondary节点读取,如果没有可用的secondary节点,则从primary节点读取
NEAREST:从集群中可用的节点读取数据

 

posted @ 2023-07-18 11:12  slnngk  阅读(263)  评论(0)    收藏  举报