0

我试图编写一个 python 脚本来将数据加载到 mongodb 但它失败了。我正在使用 mongodb 地图集。我正在从 ubuntu 执行脚本。它能够建立连接。但永远不会看到正在创建的数据库或集合。

我写的代码:

from pymongo import MongoClient
import urllib
import sys
import pandas as pd
import pymongo
import json
import os

def import_content(filepath):

    mng_client = pymongo.MongoClient("mongodb://sathish1000:siva123.A@cluster0-shard-00-00-vgsfk.mongodb.net:27017/<DATABASE>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin")

    mng_db = mng_client['local']
    collection_name = 'collection_name'
    db_cm = mng_db[collection_name]
    print(db_cm)
    cdir = os.path.dirname(__file__)
    file_res = os.path.join(cdir, filepath)
    data = pd.read_csv(file_res)
    data_json = json.loads(data.to_json(orient='records'))
    print(db_cm.find() )
    i = 0
    for data in data_json:
        i = i+1
        print(i)
        business = {"value":i}
        db_cm.insert_one(data)

if __name__ == "__main__":
    filepath = '/home/sathish/Downloads/Train.csv'
    import_content(filepath)

我得到的错误如下:

Traceback (most recent call last):
  File "LoadMongo.py", line 32, in <module>
    import_content(filepath)
  File "LoadMongo.py", line 28, in import_content
    db_cm.insert_one(data)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 654, in insert_one
    with self._socket_for_writes() as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/mongo_client.py", line 825, in _get_socket
    with server.get_socket(self.__all_credentials) as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/server.py", line 168, in get_socket
    with self.pool.get_socket(all_credentials, checkout) as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 792, in get_socket
    sock_info.check_auth(all_credentials)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 512, in check_auth
    auth.authenticate(credentials, self)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/auth.py", line 470, in authenticate
    auth_func(credentials, sock_info)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/auth.py", line 450, in _authenticate_default
    return _authenticate_scram_sha1(credentials, sock_info)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/auth.py", line 229, in _authenticate_scram_sha1
    res = sock_info.command(source, cmd)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 424, in command
    self._raise_connection_failure(error)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 552, in _raise_connection_failure
    raise error
  File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 419, in command
    collation=collation)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/network.py", line 110, in command
    response, codec_options=codec_options)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py", line 128, in _unpack_response
    if error_object["$err"].startswith("not master"):
KeyError: '$err'

有人可以帮我弄这个吗 ?

4

1 回答 1

0

这是因为您正试图将数据写入或插入本地数据库。该local数据库用于存储复制过程的数据以及其他特定于实例的数据。数据库对local复制不可见,导致本地数据库中的集合不被复制。

您应该修改您的代码,如下例所示:

m_client = pymongo.MongoClient("mongodb://...")
m_db = m_client["database_name"]
m_collection = m_db["collection_name"]

您可以进行的另一项改进是使用批量写入操作,而不是一次写入一个文档。例如:如果您的文件中有 1000 行,而不是发送 1000 insert_one(),您可以发送 1 个批量插入。

于 2017-05-05T01:29:00.140 回答