PowerBI |使用Python从MongoDB获取数据

时间:2019-05-06 22:56:08

标签: python python-3.x python-2.7 powerbi powerbi-datasource

我一直在尝试使用Python和Pandas从MongoDB检索数据。

我制作了这个脚本,它在Python中运行良好,并且在PowerBI中运行到最后,因为它使用log函数登录文件。

即使脚本运行到最后,PowerBI仍在等待一些东西,如图所示

enter image description here

def log (message):
    if LOG:
        pprint.pprint(message)
    if LOG_TO_FILE:
        global LOG_MESSAGE
        f = open(LOG_FILE,"w+")
        LOG_MESSAGE += '-------------------------------' + datetime.date.today().strftime("%d-%b-%Y-%H:%M:%S") + '----------------------------------\n'
        LOG_MESSAGE += message + "\n"
        f.write(LOG_MESSAGE)
        f.close()

log("Initializing the script")
if (CONNECT_TROUGH_SSH):
    server = SSHTunnelForwarder(
        SSH_HOST,
        ssh_username=MONGO_USER,
        ssh_pkey=HOST_KEY_PATH,
        remote_bind_address=('127.0.0.1', 27017)
    )

    log("Openning SSH Tunnel")
    server.start()
    log("SSH Tunnel Oppened")

log("Connecting to MongoDB")
client = pymongo.MongoClient('127.0.0.1', server.local_bind_port) # server.local_bind_port is assigned local port
log("Connected")

log("Getting Collection")
db = client[MONGO_DB]

log("Running Query")
result = list(db[collection_name].find().limit(1))
log("Query returned with " + str(len(result)) + " lines")

log("Converting it to Pandas DataFrame")
Collection = pandas.DataFrame(data=result)

log("Closing DB connection")
db.close()
log("Closed")

if (CONNECT_TROUGH_SSH):
    log("Closing ssh tunnel")
    server.close()
    log("Closed")

log("Finished work :)")

是否有人尝试使用Python和PB从外部数据库获取数据?

0 个答案:

没有答案