第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 python 中為 firestore 批量處理 500 多個操作?

如何在 python 中為 firestore 批量處理 500 多個操作?

湖上湖 2021-09-25 10:13:02
我正在通過python中的網(wǎng)絡(luò)抓取創(chuàng)建文檔并將它們上傳到Firestore。為此,我將它們添加到字典中,并從 Python 中的 for 循環(huán)中逐個上傳它們(理想情況下最好一次上傳集合,但這似乎不是一種選擇)。我想使用批次,但是它們每批次有 500 個限制,我需要執(zhí)行超過 100,000 次操作。這些操作僅僅是set()操作和一些操作,update() 是否有一個函數(shù)可以知道批處理的當前大小,以便我可以重新初始化它?在 python 中使用批處理進行 500 多個操作的最佳方法是什么?
查看完整描述

2 回答

?
呼喚遠方

TA貢獻1856條經(jīng)驗 獲得超11個贊

Batch 中的最大操作數(shù)為 500。如果您需要更多操作,則需要多個批處理。

沒有 API 可以確定 Batch 中的當前操作數(shù)。如果你需要它,你將不得不自己跟蹤它。


查看完整回答
反對 回復(fù) 2021-09-25
?
蕪湖不蕪

TA貢獻1796條經(jīng)驗 獲得超7個贊

我發(fā)現(xiàn)在使用 python 時處理 500 批限制的最佳方法是將我想要發(fā)送到 Firestore 的所有數(shù)據(jù)放在“平面”字典中,這樣我就可以處理每個唯一的文檔。該字典將每個文檔的鍵作為以下形式:“collection_document_collection_document...”,而該鍵的值將是具有以下內(nèi)容的字典:


{'action': 'set', 'reference': reference, 'document': {}}

'action' 可以是 'set'、'update' 或 'delete','reference' 鍵是實際的 Firestore 引用,而 'document' 只是文檔。例如,這是位于不同位置的 2 個文檔。


{

    'user_data_roger':

    {'action': 'set', 'reference': db.collection('user_data').document('roger'), 'document': {'name': 'Roger', 'age': 37}},

    'user_data_roger_works_april':

    {'action': 'update', 'reference': db.collection('user_data').document('roger').collection('works').document('april'), 'document': {'is_valid': True, 'in_progress': True, 'level':5}},

}

處理完我需要的所有數(shù)據(jù)后,我想將字典拆分為 500 個項目的數(shù)組,然后使用批處理的“操作”鍵將所有這些項目添加到批處理中。


# Convert dictionary to a list

dictionary_list = []

for item in dictionary:

    dictionary_list.append(dictionary.get(item))

# Split List in lists containing 500 items per list

list_to_batch = [dictionary_list[item:item+500] for item in range(0, len(dictionary_list), 500)]

# Finally iterate through the 'list_to_batch' add each item to the batch and commit using a for loop

for item in list_to_batch:

    batch = db.batch()

    for document in item:

        if document['action'] == 'set':

            batch.set(document['reference'], document['value'])

        elif draw['action'] == 'update':

            batch.update(document['reference'], document['value'])

        else:

            batch.delete(document['reference'], document['value'])

    # Finally commit the batch

    batch.commit()

在我處理完我需要的所有數(shù)據(jù)后的特殊情況下,我最終進行了超過 700,000 次操作,因此請注意計費:-D


查看完整回答
反對 回復(fù) 2021-09-25
  • 2 回答
  • 0 關(guān)注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號