In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This paper mainly introduces python how to connect mongodb replica set and read-write separation configuration, and how to test the data synchronization delay of mongodb replica set.
I. python connection mongodb replica set
1. Connect copy set
Use MongoClient connection directly in the new driver
As follows:
From pymongo import MongoClientconn = MongoClient (['192.168.3.11, 27017, 192.168.3.12, 192.168.3.13, 27017])
two。 Read-write separation configuration
From pymongo import ReadPreferencedb = conn.get_database ('hnrtest', read_preference=ReadPreference.SECONDARY_PREFERRED)
There are 5 options for replica set ReadPreference:
PRIMARY: default option to read data from primary nodes
PRIMARY_PREFERRED: read from the primary node first, or from the secondary node available in the cluster if there is no primary node
SECONDARY: reading data from secondary nodes
SECONDARY_PREFERRED: read from the secondary node first, or from the primary node if no secondary node is available
NEAREST: read data from nodes available in the cluster
Second, test the mongodb delay script
1. Principle
Insert a batch of data into the cluster and read it from the secondary node until you read that the total number of data is equal to the total number of inserts.
two。 Script content
#! / usr/bin/python#-*-coding: utf-8-*-import pymongofrom pymongo import MongoClientfrom pymongo import ReadPreferenceimport timeimport argparse# defines the length of execution decorator function def warps (* args): def deco (func): def _ deco (* args, * * kwargs): # record start time start = time.clock () # callback to the original function func (* args * * kwargs) # record end time end = time.clock () # Compute execution time delat = end-start # converted to ms output print ("delay:%sms"% (int (delat*1000) return _ deco return deco# connection replica set conn = MongoClient (['192.168.3.11 return 27017' '192.168.3.12 collection 27017,' 192.168.3.13)) # read / write separation db = conn.get_database ('hnrtest', read_preference=ReadPreference.SECONDARY_PREFERRED) # define the set of connections collection = db.student# create the insert data function def data_insert (num): try: for i in range (1, num): collection.insert ({"name": "student" + str (I) "age": (I% 100), "city": "FuZhou"}) except Exception as e: print ("insert data:", e) # create query data function Reference the decorator function @ warps () def data_select (num): try: count = collection.find () .count () while count! = num-1: count = collection.find () .count () except Exception as e: print ("select data:" E) # create delete data function def data_delete (): try: collection.remove ({}) except Exception as e: print ("delete data:", e) # create calculation delay function def data_delay (num): data_insert (num) data_select (num) if _ _ name__ = ='_ main__': # define the amount of data to be inserted into the script The default value is 1000. Pass the parameter parser = argparse.ArgumentParser (description='insert data to mongodb number') parser.add_argument ('- n, action='store', dest='num', type=int, required=False, default=1000) given_args = parser.parse_args () num = given_args.num data_delete () data_delay (num) via-n
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.