Python+mongodb Use Example

Source: Internet
Author: User
Tags mongoclient mongodb

This blog originates from Hubaud Master's junior-class NoSQL course design, using Python+mongodb combination method, the data from TXT file into MongoDB, and then take it out to the drawing. The main technology is the use of Python and MongoDB combined storage reading scheme, so this blog intercepted part of the lesson set, the main explanation of Python operation MongoDB implementation, in order to want to learn PYTHON+MONGODB programming development of students learning practiced hand.

Design ideas

1, the Python programming language and MongoDB have the driver packet Pymongo connection, the use of Pymongo to the MongoDB programming development, data reading, as well as the subsequent drawing analysis and data visualization processing

2, the use of data files sample.txt.txt.

Design Content

1, First use datatomongo.py to clean the file into MongoDB .

(1) Create and connect to a nosql database in MongoDB ;

(2) Create and connect a collection of NoSQL databases to sample;

2, visual processing of the data read .

(1) the main Python package used for drawing is matplotlib.plot.

Design Steps and Results testing

1, first use datatomongo.py to clean the file into MongoDB.

(1) Drive with pymongo Connection

(2) mongoclient class connection with pymongo mongod

(3) the MONGO Common syntax used in Python is basically the same as the JSON syntax of MONGO .

MongoDB Boot:

Launch the Python script datatomongo.py to read the data out of the file and into MongoDB :

Note To use the following command (written using python+hadoop, the UNIX input and output stream is used to read the data):

# cat Sample.txt.txt |./datatomongo.py

Data successfully deposited Mongodb:

datatomongo.py

#!/usr/bin/python3#-*-coding:utf-8-*-#This file is used to store data in MongoDB database NoSQL#collection for sample (Db.sample.find ())#The format is: "Line1": [Data1,data2 ...] fromPymongoImportmongoclientImportSYS#Connect to Mongod, with DB: ' NoSQL ', use COLLECTION: ' Sample 'conn = Mongoclient ('127.0.0.1', 27017)#Connection Mongoddb = Conn.nosql#connect to ' NoSQL ' database, not automatically createdSample = Db.sample#using the sample collection, none is automatically createdN= 1Data=list () forLineinchSys.stdin:data=Line.strip (). Split () LineNum=" Line"+STR (N)#LineNum is a string of line strings + numbers, which is used when you insert a data set.    ifN==1:        Pass                            #The first row of data is useless, clean out    elifn==2: Data= Data[4:]#the measured observation data starts with the fourth dataData[0] ='-3.1415926536'       #The original data is ' real=-3.1415926536 ', so the data is modified under         forIndex,iteminchEnumerate (data): Data[index]= Float (item)#Converting data that was originally a string to float typeSample.insert ({#inserting data into (insert) a sample collection in MongoDB            " Line": LineNum,#I've inserted a key-value pair for the {line:linenum} for subsequent graphing purposes .            "value":d ATA#{Value:data} Key-value pair, data is a list variable oh        })    Else: Data= data[2:]#and the second row of the data is not the same, the actual observation, sometimes programming is to step-by-step observation of the modified         forIndex,iteminchEnumerate (data):#Test your own hands-on ability, how to observe the realization of their own waysData[index] =Float (item) Sample.insert ({" Line": LineNum,"value":d ATA}) N+=1

2, visual processing of the data read.

Paint Script: main.py

(1) The first diagram:

The Blue Line is at 1.0*10^9 frequency,phi=0

The Orange Line is at 1.0*10^9 frequency,phi=1.5707963268.

the horizontal axis is Theta (degree), range from -pi ~pi; the ordinate is an electric field value.

at the same frequency,the higher the Theta angle, the smaller the electric field value .

main.py

#!/usr/bin/python3#-*-coding:utf-8-*-#file:main.py#This file is used to read the NoSQL database in MongoDB. Sample collection Data#after reading the drawing fromPymongoImportmongoclientImportMatplotlib.pyplot as Plt#Connect to Mongod, with DB: ' NoSQL ', use COLLECTION: ' Sample 'conn = Mongoclient ('127.0.0.1', 27017)#Connection Mongoddb = Conn.nosql#connect to ' NoSQL ' database, not automatically createdSample = Db.sample#using the sample collection, none is automatically createdAngle_theta_radian=sample.find_one ({" Line":"line2"})['value']#use Find_one to find the 2nd row of data, taking the value of the key value pair named ' Value 'Data_phi0=sample.find_one ({" Line":"Line3"})['value']#use Find_one to find the 3rd row of data, taking the value of the key value pair named ' Value 'Data_phi90=sample.find_one ({" Line":"line48"})['value']#use Find_one to find the 48th row of data, taking the value of the key value pair named ' Value 'PLT1,= Plt.plot (angle_theta_radian,data_phi0)#drawing 1PLT2, = Plt.plot (Angle_theta_radian,data_phi90)#Drawing 2Plt.legend ([plt1,plt2],['electrial Field1','electrial Field2'],loc='Upper Right')#Set the legendPlt.xlabel ('Theta (degree)')#set X-axis labelsPlt.ylabel ('electrical Field (v/m)')#set Y-axis labelsPlt.show ()#The above show up together

(2) The second figure:

The Blue Line is, at 1.0*10^9 frequency, phi=0;

The Orange Line is, at 1.02*10^9 frequency, phi=0; (line 93rd: line93)

The horizontal axis is theta (degree), the range from-pi ~pi; the ordinate is the electric field value.

At the same theat angle, the higher the frequency, the greater the absolute value of the electric field.

Implementation: The main.py script file in the

Data_phi90=sample.find_one ({"line":"line48"}) [' value ']

Modified to:

Data_phi90=sample.find_one ({"line":"line93"}) [' value ']

can be drawn.

Data File Description:

1, Data File Overview: Is the test of the electric field strength data file.

Includes the electric field intensity of multiple frequency tests, and the data for each frequency is a block of data.

The data that is tested at a frequency point (inside each piece of data) also includes multiple rows of data (each row is a theta angle of data) and multiple columns of data (each column is a phi angle).

2. Data file Meaning:

3. The relationship between experimental test and data is as follows

For file Sample.txt.txt, there are only two frequencies (109 frequency and 1.2*109 frequency).

thanks to teacher Li's data file, if there are any infringement issues, bloggers can immediately delete.

Blog seems to be unable to send files, if you need data files, please leave a message mailbox, see sent to you.

Python+mongodb Use Example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.