-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_table_demo.py
181 lines (151 loc) · 6.76 KB
/
storage_table_demo.py
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
#modified the file
import string,random,time,azurerm,json
from azure.storage.table import TableService, Entity
# Define variables to handle Azure authentication
auth_token = azurerm.get_access_token_from_cli()
subscription_id = azurerm.get_subscription_from_cli()
# Define variables with random resource group and storage account names
resourcegroup_name = 'klh'+''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(6))
storageaccount_name = 'klh'+''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(6))
location = 'southcentralus'
###
# Create the a resource group for our demo
# We need a resource group and a storage account. A random name is generated, as each storage account name must be globally unique.
###
response = azurerm.create_resource_group(auth_token, subscription_id, resourcegroup_name, location)
if response.status_code == 200 or response.status_code == 201:
print('Resource group: ' + resourcegroup_name + ' created successfully.')
else:
print('Error creating resource group')
# Create a storage account for our demo
response = azurerm.create_storage_account(auth_token, subscription_id, resourcegroup_name, storageaccount_name, location, storage_type='Standard_LRS')
if response.status_code == 202:
print('Storage account: ' + storageaccount_name + ' created successfully.')
time.sleep(2)
else:
print('Error creating storage account')
###
# Use the Azure Storage Storage SDK for Python to create a Table
###
print('\nLet\'s create an Azure Storage Table to store some data.')
raw_input('Press Enter to continue...')
# Each storage account has a primary and secondary access key.
# These keys are used by aplications to access data in your storage account, such as Tables.
# Obtain the primary storage access key for use with the rest of the demo
response = azurerm.get_storage_account_keys(auth_token, subscription_id, resourcegroup_name, storageaccount_name)
storageaccount_keys = json.loads(response.text)
storageaccount_primarykey = storageaccount_keys['keys'][0]['value']
# Create the Table with the Azure Storage SDK and the access key obtained in the previous step
table_service = TableService(account_name=storageaccount_name, account_key=storageaccount_primarykey)
response = table_service.create_table('itemstable')
if response == True:
print('Storage Table: itemstable created successfully.\n')
else:
print('Error creating Storage Table.\n')
time.sleep(1)
###
# Use the Azure Storage Storage SDK for Python to create some entries in the Table
###
print('Now let\'s add some entries to our Table.\nRemember, Azure Storage Tables is a NoSQL datastore, so this is similar to adding records to a database.')
raw_input('Press Enter to continue...')
# Each entry in a Table is called an 'Entity'.
# Here, we add an entry for first pizza with two pieces of data - the name, and the cost
#
# A partition key tracks how like-minded entries in the Table are created and queried.
# A row key is a unique ID for each entity in the partition
# These two properties are used as a primary key to index the Table. This makes queries much quicker.
cars = Entity()
cars.PartitionKey = 'carsonlot'
cars.RowKey = '001'
cars.make = 'BMW'
cars.model = '328i'
cars.year = '2010'
cars.color = 'white'
cars.cost = 9000
table_service.insert_entity('itemstable', cars)
print('Created entry for BMW...')
cars = Entity()
cars.PartitionKey = 'carsonlot'
cars.RowKey = '002'
cars.make = 'Toyota'
cars.model = 'Tacoma'
cars.year = '2012'
cars.color = 'green'
cars.cost = 19000
table_service.insert_entity('itemstable', cars)
print('Created entry for Toyota...')
cars = Entity()
cars.PartitionKey = 'carsonlot'
cars.RowKey = '003'
cars.make = 'Lexus'
cars.model = 'LX470'
cars.year = '2017'
cars.color = 'white'
cars.cost = 69000
table_service.insert_entity('itemstable', cars)
print('Created entry for Lexus...')
# A partition key tracks how like-minded entries in the Table are created and queried.
# A row key is a unique ID for each entity in the partition
# These two properties are used as a primary key to index the Table. This makes queries much quicker.
coffee = Entity()
coffee.PartitionKey = 'coffee'
coffee.RowKey = '001'
coffee.brand = 'dunkin'
coffee.flavor = 'pumpkim'
coffee.size = 'pound'
coffee.cost = 12.99
table_service.insert_entity('itemstable', coffee)
print('Created entry for a dunkin...\n')
time.sleep(1)
coffee = Entity()
coffee.PartitionKey = 'coffee'
coffee.RowKey = '002'
coffee.brand = 'starbucks'
coffee.flavor = 'ginger bread'
coffee.size = 'pound'
coffee.cost = 22.99
table_service.insert_entity('itemstable', coffee)
print('Created entry for a ginger...\n')
time.sleep(1)
coffee = Entity()
coffee.PartitionKey = 'coffee'
coffee.RowKey = '003'
coffee.brand = 'seattle'
coffee.flavor = 'dark roast'
coffee.size = 'pound'
coffee.cost = 15.99
table_service.insert_entity('itemstable', coffee)
print('Created entry for a seattle...\n')
time.sleep(1)
###
# Use the Azure Storage Storage SDK for Python to query for entities in our Table
###
print('With some data in our Azure Storage Table, we can query the data.\nLet\'s see what the car lot looks like.')
raw_input('Press Enter to continue...')
# In this query, you define the partition key to search within, and then which properties to retrieve
# Structuring queries like this improves performance as your application scales up and keeps the queries efficient
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'carsonlot'", select='description,cost')
for item in items:
print('Name: ' + items.make)
print('Cost: ' + str(items.cost) + '\n')
items = table_service.query_entities('itemstable', filter="PartitionKey eq 'coffee'", select='description,price')
for item in items:
print('Name: ' + items.brand)
print('Price: ' + str(items.price) + '\n')
time.sleep(1)
###
# This was a quick demo to see Tables in action.
# Although the actual cost is minimal (fractions of a cent per month) for the three entities we created, it's good to clean up resources when you're done
###
print('\nThis is a basic example of how Azure Storage Tables behave like a database.\nTo keep things tidy, let\'s clean up the Azure Storage resources we created.')
raw_input('Press Enter to continue...')
response = table_service.delete_table('itemstable')
if response == True:
print('Storage table: itemstable deleted successfully.')
else:
print('Error deleting Storage Table')
response = azurerm.delete_resource_group(auth_token, subscription_id, resourcegroup_name)
if response.status_code == 202:
print('Resource group: ' + resourcegroup_name + ' deleted successfully.')
else:
print('Error deleting resource group.')