#coding:utf8 from pymongo import MongoClient import csv import time def connect_mongo(): # mongo_uri = 'mongodb://user_name:password@host:port/Validate the database' # client = pymongo.MongoClient(mongo_uri, readPreference='write as required') client = MongoClient('127.0.0.1', 27017) db = client.test collection = db['second_crimes'] return collection #集合 def insertToMongoDB(set1): with open('second_crimes.csv','r',encoding='utf-8') as csvfile: # Call the DictReader function in csv to directly obtain the data in the form of a dictionary reader = csv.DictReader(csvfile) csv_data = [] # Create a counts to count how many pieces of data have been added counts = 0 index = 1 for each in reader: csv_data.append(each) if index==10000:#Write to MongoDB after 10,000 set1.insert(csv_data) csv_data.clear() index = 0 print("successfully added" + str(counts) + "data") counts+=1 index+=1 if len(csv_data)>0:#remain data set1.insert(csv_data) print("Successfully added %s data"%len(csv_data)) if __name__=='__main__': print(time.strftime('%Y-%m-%d %H:%M:%S'))#for calculating time set1 = connect_mongo() insertToMongoDB(set1) print(time.strftime('%Y-%m-%d %H:%M:%S'))2.第二种方式
# coding=utf-8 from pymongo import MongoClient import csv, time, re # 链接mongodb数据库客户端 client = MongoClient('127.0.0.1', 27017) # 链接mongodb数据库集合 crimes = client['test']['crimes'] # 将所有数据全部插入到对应集合中 # crimes集合 _data = csv.reader(open('crimes.csv', 'r')) count = 0 for _ in list(_data)[1:]: _json = { 'id': _[0], 'case_number': _[1], 'data': _[2], 'block': _[3], 'iucr': _[4], 'primary_type': _[5], 'description': _[6], 'location_description': _[7], 'arrest': _[8], 'domestic': _[9], 'beat': _[10], 'district': _[11], 'word': _[12], 'connuntiy_area': _[13], 'fbi_code': _[14], 'x_coordinate': _[15], 'y_coordinate': _[16], 'year': _[17], 'updated_on': _[18], 'latitude': _[19], 'longitude': _[20], 'location': _[21] } crimes.insert_one(_json) count += 1 print('第' + str(count) + '个数据已经插入成功!' + ' 数据源是:' + str(_json))3.MongoDB 可视化工具链接 点击直接下载: nosqlbooster4mongo-7.0.8.exe. 4.MongoDB window系统安装软件 点击直接下载: mongodb-windows-x86_64-5.0.5.zip.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)