File: change_feed_management_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (244 lines) | stat: -rw-r--r-- 11,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from datetime import datetime, timezone

from azure.cosmos.aio import CosmosClient
import azure.cosmos.exceptions as exceptions
import azure.cosmos.documents as documents
import azure.cosmos.partition_key as partition_key
import uuid

import asyncio
import config

# ----------------------------------------------------------------------------------------------------------
# Prerequisites -
#
# 1. An Azure Cosmos account -
#    https:#azure.microsoft.com/documentation/articles/documentdb-create-account/
#
# 2. Microsoft Azure Cosmos PyPi package -
#    https://pypi.python.org/pypi/azure-cosmos/
# ----------------------------------------------------------------------------------------------------------
# Sample - demonstrates how to consume the Change Feed and iterate on the results.
# ----------------------------------------------------------------------------------------------------------

HOST = config.settings['host']
MASTER_KEY = config.settings['master_key']
DATABASE_ID = config.settings['database_id']
CONTAINER_ID = config.settings['container_id']


async def create_items(container, size, partition_key_value):
    print("Creating Items with partition key value: {}".format(partition_key_value))

    for i in range(size):
        c = str(uuid.uuid4())
        item_definition = {'id': 'item' + c,
                           'address': {'street': '1 Microsoft Way' + c,
                                       'city': 'Redmond' + c,
                                       'state': partition_key_value,
                                       'zip code': 98052
                                       }
                           }

        await container.create_item(body=item_definition)

async def clean_up(container):
    print('\nClean up the container\n')

    async for item in container.query_items(query='SELECT * FROM c'):
        # Deleting the current item
        await container.delete_item(item, partition_key=item['address']['state'])

async def read_change_feed(container):
    print('\nReading Change Feed from the beginning\n')

    # For a particular Partition Key Range we can use partition_key_range_id]
    # 'is_start_from_beginning = True' will read from the beginning of the history of the container
    # If no is_start_from_beginning is specified, the read change feed loop will pickup the items that happen while the loop / process is active
    await create_items(container, 10, 'WA')
    response_iterator = container.query_items_change_feed(is_start_from_beginning=True)

    # Because the asynchronous client returns an asynchronous iterator object for methods using queries,
    # we do not need to await the function. However, attempting to cast this object into a list directly
    # will throw an error; instead, iterate over the result using an async for loop like shown here
    async for doc in response_iterator:
        print(doc)

    print('\nFinished reading all the change feed\n')


async def read_change_feed_with_start_time(container):
    print('\nReading Change Feed from the start time\n')
    # You can read change feed from a specific time.
    # You must pass in a datetime object for the start_time field.

    # Create items
    await create_items(container, 10, 'WA')
    start_time = datetime.now(timezone.utc)
    time = start_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
    print('\nReading Change Feed from start time of {}\n'.format(time))
    await create_items(container, 5, 'CA')
    await create_items(container, 5, 'OR')

    # Read change feed from the beginning
    response_iterator = container.query_items_change_feed(start_time="Beginning")
    async for doc in response_iterator:
        print(doc)

    # Read change feed from a start time
    response_iterator = container.query_items_change_feed(start_time=start_time)
    async for doc in response_iterator:
        print(doc)

async def read_change_feed_with_partition_key(container):
    print('\nReading Change Feed from the beginning of the partition key\n')
    # Create items
    await create_items(container, 10, 'WA')
    await create_items(container, 5, 'CA')
    await create_items(container, 5, 'OR')

    # Read change feed with partition key with LatestVersion mode.
    # Should only return change feed for the created items with 'CA' partition key
    response_iterator = container.query_items_change_feed(start_time="Beginning", partition_key="CA")
    async for doc in response_iterator:
        print(doc)

async def read_change_feed_with_continuation(container):
    print('\nReading Change Feed from the continuation\n')
    # Create items
    await create_items(container, 10, 'WA')
    response_iterator = container.query_items_change_feed(start_time="Beginning")
    async for doc in response_iterator:
        print(doc)
    continuation_token = container.client_connection.last_response_headers['etag']

    # Create additional items
    await create_items(container, 5, 'CA')
    await create_items(container, 5, 'OR')

    # You can read change feed from a specific continuation token.
    # You must pass in a valid continuation token.
    # From our continuation token above, you will get all items created after the continuation
    response_iterator = container.query_items_change_feed(continuation=continuation_token)
    async for doc in response_iterator:
        print(doc)

async def read_change_feed_with_all_versions_and_delete_mode(container):
    print('\nReading Change Feed with AllVersionsAndDeletes mode\n')
    # Read the initial change feed with 'AllVersionsAndDeletes' mode.
    # This initial call was made to store a point in time in a 'continuation' token
    response_iterator = container.query_items_change_feed(mode="AllVersionsAndDeletes")
    async for doc in response_iterator:
        print(doc)
    continuation_token = container.client_connection.last_response_headers['etag']

    # Read all change feed with 'AllVersionsAndDeletes' mode after create items from a continuation
    await create_items(container, 10, 'CA')
    await create_items(container, 10, 'OR')
    response_iterator = container.query_items_change_feed(mode="AllVersionsAndDeletes", continuation=continuation_token)
    async for doc in response_iterator:
        print(doc)

    # Read all change feed with 'AllVersionsAndDeletes' mode after delete items from a continuation
    await clean_up(container)
    response_iterator = container.query_items_change_feed(mode="AllVersionsAndDeletes", continuation=continuation_token)
    async for doc in response_iterator:
        print(doc)

async def read_change_feed_with_all_versions_and_delete_mode_with_partition_key(container):
    print('\nReading Change Feed with AllVersionsAndDeletes mode from the partition key\n')

    # Read the initial change feed with 'AllVersionsAndDeletes' mode with partition key('CA').
    # This initial call was made to store a point in time and 'partition_key' in a 'continuation' token
    response_iterator = container.query_items_change_feed(mode="AllVersionsAndDeletes", partition_key="CA")
    async for doc in response_iterator:
        print(doc)
    continuation_token = container.client_connection.last_response_headers['etag']

    await create_items(container, 10, 'CA')
    await create_items(container, 10, 'OR')
    # Read change feed 'AllVersionsAndDeletes' mode with 'CA' partition key value from the previous continuation.
    # Should only print the created items with 'CA' partition key value
    response_iterator = container.query_items_change_feed(mode='AllVersionsAndDeletes', continuation=continuation_token)
    async for doc in response_iterator:
        print(doc)
    continuation_token = container.client_connection.last_response_headers['etag']

    await clean_up(container)
    # Read change feed 'AllVersionsAndDeletes' mode with 'CA' partition key value from the previous continuation.
    # Should only print the deleted items with 'CA' partition key value
    response_iterator = container.query_items_change_feed(mode='AllVersionsAndDeletes', continuation=continuation_token)
    async for doc in response_iterator:
        print(doc)

async def run_sample():
    async with CosmosClient(HOST, MASTER_KEY) as client:
        # Delete pre-existing database
        try:
            await client.delete_database(DATABASE_ID)
        except exceptions.CosmosResourceNotFoundError:
            pass

        try:
            # setup database for this sample
            try:
                db = await client.create_database(id=DATABASE_ID)
            except exceptions.CosmosResourceExistsError:
                raise RuntimeError("Database with id '{}' already exists".format(DATABASE_ID))

            # setup container for this sample
            try:
                container = await db.create_container(
                    id=CONTAINER_ID,
                    partition_key=partition_key.PartitionKey(path='/address/state', kind=documents.PartitionKind.Hash),
                    offer_throughput = 11000
                )
                print('Container with id \'{0}\' created'.format(CONTAINER_ID))

            except exceptions.CosmosResourceExistsError:
                raise RuntimeError("Container with id '{}' already exists".format(CONTAINER_ID))

            # Read change feed from beginning
            await read_change_feed(container)
            await clean_up(container)

            # Read Change Feed from timestamp
            await read_change_feed_with_start_time(container)
            await clean_up(container)

            # Read Change Feed from continuation
            await read_change_feed_with_continuation(container)
            await clean_up(container)

            # Read Change Feed by partition_key
            await read_change_feed_with_partition_key(container)
            await clean_up(container)

            # Read change feed with 'AllVersionsAndDeletes' mode after create/delete item
            await read_change_feed_with_all_versions_and_delete_mode(container)
            await clean_up(container)

            # Read change feed with 'AllVersionsAndDeletes' mode with partition key for create/delete items.
            await read_change_feed_with_all_versions_and_delete_mode_with_partition_key(container)
            await clean_up(container)

            # cleanup database after sample
            try:
                await client.delete_database(db)
            except exceptions.CosmosResourceNotFoundError:
                pass

        except exceptions.CosmosHttpResponseError as e:
            print('\nrun_sample has caught an error. {0}'.format(e.message))

        finally:
            print("\nrun_sample done")


if __name__ == '__main__':
    asyncio.run(run_sample())