how to capture changes with Azure function Cosmos DB trigger?

749 Views Asked by At

I have the following requirement i want to capture record from cosmosDB and push it to blog storage.

This above problem i have solved with azure function cosmos DB trigger (that will capture any changes made to record in cosmos DB container).

How to capture full load?

requirement is i have to get all the record from cosmos DB container and push it to blog storage.

so the problem is cosmosDB trigger only capture if there is some change in record, so how i can achieve this full load.

For Full load i have create one python script also.

Reference: https://github.com/Azure/azure-cosmos-python

Reference Script: How To Update Record in Cosmos Db using python?

query_data = client.QueryItems(collection_link,
                                   'SELECT * FROM ' + COSMOS_DB_COLLECTION_ID,
                                   {'enableCrossPartitionQuery': True})
    for item in query_data:
        client.ReplaceItem(item['_self'], item, options=None)

so what actual my script is doing its taking all record and i am calling ReplaceItem for each record, but what i observed is azure function cosmos DB trigger won't capture changes of all record its capture few record only.

so is there problem with the script?

1

There are 1 best solutions below

1
Cindy Pau On

First, I think client dont have 'QueryItems'.

Second, there is no changed in your document.

You should do like below:

url = os.environ['ACCOUNT_URI']
key = os.environ['ACCOUNT_KEY']
client = cosmos_client.CosmosClient(url, {'masterKey': key})
database_name = "testbowman"
container_name = "testbowman"
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
for item in container.query_items('SELECT * FROM c', enable_cross_partition_query=True):
    logging.info("This is Query database!" + json.dumps(item))
    item['Description'] = "!!!!!!!!!!!!!!"
    container.replace_item(item['id'],item)