Import Sys
Import JSON
Import Pymongo
Import datetime
From Pymongo import mongoclient
Client = mongoclient (' mongodb://192.168.1.31:20000,192.168.1.34:20000 ')
db = client. Rhy
Collection = db. St_river_r
f = open ("D:/bigdata/st_river_r.csv")
line = F.readline ()
Print (line)
FieldNames = Line.split (', ')
# STCD,TM,Z,Q,XSA,XSAVV,XSMXV,FLWCHRCD,WPTN,MSQMT,MSAMT,MSVMT
line = F.readline ()
Count = 0
Records = []
Insertcount = 0
While line:
#
Count = Count + 1
Fieldvalues = Line.split (', ')
If Len (fieldvalues) = = or Fieldvalues[0].strip ()! = ":
Insertobj = {}
STCD = Fieldvalues[0]
insertobj[' stcd '] = Stcd
TM = fieldvalues[1]
If Tm.strip ()! = ":
TM = Datetime.datetime.strptime (tm, '%y-%m-%d%h:%m:%s ')
Insertobj[' TM '] = TM
Z = fieldvalues[2]
If Z.strip ()! = ":
z = float (z)
Insertobj[' z '] = Z
Q = fieldvalues[3]
If Q.strip ()! = ":
Q = float (q)
insertobj[' q '] = Q
# XSA
XSA = Fieldvalues[4]
If Xsa.strip ()! = ":
XSA = float (XSA)
insertobj[' XSA '] = XSA
# XSAVV
XSAVV = fieldvalues[5]
If Xsavv.strip ()! = ":
XSAVV = float (XSAVV)
insertobj[' xsavv '] = XSAVV
#
XSMXV = Fieldvalues[6]
If Xsmxv.strip ()! = ":
XSMXV = float (XSMXV)
insertobj[' xsmxv '] = XSMXV
#
FLWCHRCD = Fieldvalues[7]
If Flwchrcd.strip ()! = ":
insertobj[' flwchrcd '] = FLWCHRCD
#
WPTN = Fieldvalues[8]
If Wptn.strip ()! = ":
insertobj[' wptn '] = WPTN
#
MSQMT = fieldvalues[9]
If Msqmt.strip ()! = ":
insertobj[' msqmt '] = msqmt
#
Msamt = fieldvalues[10]
If Msamt.strip ()! = ":
insertobj[' msamt '] = Msamt
#
MSVMT = fieldvalues[11]
If Msvmt.strip ()! = ":
insertobj[' msvmt '] = MSVMT
#
# Collection.insert_one (insertobj)
# Collection.insert_many (new_posts)
Records.append (Insertobj)
If Len (records) = = 1000:
Insertcount = Insertcount + 1
If Count > 1451000:
Collection.insert_many (Records)
Print (str (count) + ' + str (insertcount))
Print (count)
Records = []
Else
Print (line)
#
line = F.readline ()
F.close ()
Client.close ()
Importing data into a MongoDB cluster