Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anthonyeden/a423d5b6b88fe6af9fe7691993e74fda to your computer and use it in GitHub Desktop.
Save anthonyeden/a423d5b6b88fe6af9fe7691993e74fda to your computer and use it in GitHub Desktop.
"""
Azure Table Storage - Python Batching Example
(C) 2016 MediaRealm.com.au
Needs v0.30.0 of the Azure Storage Python SDK
https://github.com/Azure/azure-storage-python/releases/tag/v0.30.0
"""
from azure.storage.table import TableService, Entity, TableBatch
table_service = TableService(
account_name = '---MYACCOUNTNAME---',
account_key = '---MYSECRETKEY---'
)
# Create a new batch
batch = TableBatch()
# Count how many items are stored in the batch
inBatch = 0
# Loop over all the data we want to insert
for x in dataToStore:
# Insert the entity into the batch
batch.insert_entity({
'PartitionKey': 'PARTITION1',
'RowKey': str(x['rowkey']),
'someKey': x['someValue'],
'someOtherKey': x['someOtherValue']
})
# Increment the batch item counter
inBatch += 1
# We can only send batches with up to 100 records
if inBatch > 99:
# Commit the batch (send to Azure)
table_service.commit_batch('tablename', batch)
# Reset the batch so it doesn't contain any old items
batch = TableBatch()
inBatch = 0
if inBatch > 0:
# If there's still anything in the batch, send it to Azure now
table_service.commit_batch('tablename', batch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment