File: change_feed_samples.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (121 lines) | stat: -rw-r--r-- 4,380 bytes parent folder | download | duplicates (2)
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
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: change_feed_samples.py
DESCRIPTION:
    This sample demonstrates
    1) list events by page
    2) list all events
    3) list events in a time range
    4) list events starting from a continuation token
USAGE:
    python blob_samples_container.py
    Set the environment variables with your own values before running the sample:
    1) AZURE_STORAGE_ACCOUNT_NAME - the name of the storage account
    2) AZURE_STORAGE_ACCESS_KEY - the storage account access key
"""

import os
from datetime import datetime
from time import sleep

from azure.storage.blob.changefeed import ChangeFeedClient


class ChangeFeedSamples(object):

    ACCOUNT_NAME = os.getenv("AZURE_STORAGE_ACCOUNT_NAME")
    ACCOUNT_KEY = os.getenv("AZURE_STORAGE_ACCESS_KEY")

    def list_events_by_page(self):

        # Instantiate a ChangeFeedClient
        # [START list_events_by_page]
        # [START create_change_feed_client]
        cf_client = ChangeFeedClient("https://{}.blob.core.windows.net".format(self.ACCOUNT_NAME),
                                     credential=self.ACCOUNT_KEY)
        # [END create_change_feed_client]

        change_feed = cf_client.list_changes(results_per_page=10).by_page()

        # print first page of events
        change_feed_page1 = next(change_feed)
        for event in change_feed_page1:
            print(event)

        # print second page of events
        change_feed_page2 = next(change_feed)
        for event in change_feed_page2:
            print(event)
        # [END list_events_by_page]

    def list_all_events(self):
        # [START list_all_events]
        cf_client = ChangeFeedClient("https://{}.blob.core.windows.net".format(self.ACCOUNT_NAME),
                                     credential=self.ACCOUNT_KEY)
        change_feed = cf_client.list_changes()

        # print all events
        for event in change_feed:
            print(event)
    # [END list_all_events]

    def list_range_of_events(self):
        cf_client = ChangeFeedClient("https://{}.blob.core.windows.net".format(self.ACCOUNT_NAME),
                                     credential=self.ACCOUNT_KEY)
        start_time = datetime(2020, 8, 18, 10)
        end_time = datetime(2020, 3, 4)
        change_feed = cf_client.list_changes(start_time=start_time, end_time=end_time)

        # print first page of events
        for event in change_feed:
            print(event)

    def list_events_using_continuation_token(self):

        # Instantiate a ChangeFeedClient
        cf_client = ChangeFeedClient("https://{}.blob.core.windows.net".format(self.ACCOUNT_NAME),
                                     credential=self.ACCOUNT_KEY)
        # to get continuation token
        change_feed = cf_client.list_changes(results_per_page=2).by_page()
        change_feed_page1 = next(change_feed)
        for event in change_feed_page1:
            print(event)
        token = change_feed.continuation_token

        # restart using the continuation token
        change_feed2 = cf_client.list_changes(results_per_page=56).by_page(continuation_token=token)
        change_feed_page2 = next(change_feed2)
        for event in change_feed_page2:
            print(event)

    def list_events_in_live_mode(self):
        # Instantiate a ChangeFeedClient
        cf_client = ChangeFeedClient("https://{}.blob.core.windows.net".format(self.ACCOUNT_NAME),
                                     credential=self.ACCOUNT_KEY)
        token = None
        while True:
            change_feed = cf_client.list_changes(results_per_page=500).by_page(continuation_token=token)

            for page in change_feed:
                for event in page:
                    print(event)
            token = change_feed.continuation_token

            sleep(60)
            print("continue printing events")


if __name__ == '__main__':
    sample = ChangeFeedSamples()
    sample.list_events_by_page()
    sample.list_all_events()
    sample.list_range_of_events()
    sample.list_events_using_continuation_token()
    sample.list_events_in_live_mode()