123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #-------------------------------------------------------------------------
- # Microsoft Developer & Platform Evangelism
- #
- # Copyright (c) Microsoft Corporation. All rights reserved.
- #
- # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
- # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
- # OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
- #----------------------------------------------------------------------------------
- # The example companies, organizations, products, domain names,
- # e-mail addresses, logos, people, places, and events depicted
- # herein are fictitious. No association with any real company,
- # organization, product, domain name, email address, logo, person,
- # places, or events is intended or should be inferred.
- #--------------------------------------------------------------------------
- import config
- from random_data import RandomData
- from azure.storage import CloudStorageAccount
- from azure.storage.table import TableService, Entity
- #
- # Azure Table Service Sample - Demonstrate how to perform common tasks using the Microsoft Azure Table Service
- # including creating a table, CRUD operations and different querying techniques.
- #
- # Documentation References:
- # - What is a Storage Account - http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/
- # - Getting Started with Tables - https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-table-storage/
- # - Table Service Concepts - http://msdn.microsoft.com/en-us/library/dd179463.aspx
- # - Table Service REST API - http://msdn.microsoft.com/en-us/library/dd179423.aspx
- # - Table Service Python API - http://azure.github.io/azure-storage-ruby/
- # - Storage Emulator - http://azure.microsoft.com/en-us/documentation/articles/storage-use-emulator/
- #
- class TableBasicSamples():
- def __init__(self):
- self.random_data = RandomData()
- # Runs all samples for Azure Storage Table service.
- def run_all_samples(self, account):
- print('Azure Storage Basic Table samples - Starting.')
- table_name = 'tablebasics' + self.random_data.get_random_name(6)
- table_service = None
- try:
- table_service = account.create_table_service()
- # Create a new table
- print('Create a table with name - ' + table_name)
- try:
- table_service.create_table(table_name)
- except Exception as err:
- print('Error creating table, ' + table_name + 'check if it already exists')
-
- # Create a sample entity to insert into the table
- customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '555-555-5555'}
- # Insert the entity into the table
- print('Inserting a new entity into table - ' + table_name)
- table_service.insert_entity(table_name, customer)
- print('Successfully inserted the new entity')
- # Demonstrate how to query the entity
- print('Read the inserted entity.')
- entity = table_service.get_entity(table_name, 'Harp', '1')
- print(entity['email'])
- print(entity['phone'])
- # Demonstrate how to update the entity by changing the phone number
- print('Update an existing entity by changing the phone number')
- customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '425-123-1234'}
- table_service.update_entity(table_name, customer)
- # Demonstrate how to query the updated entity, filter the results with a filter query and select only the value in the phone column
- print('Read the updated entity with a filter query')
- entities = table_service.query_entities(table_name, filter="PartitionKey eq 'Harp'", select='phone')
- for entity in entities:
- print(entity['phone'])
- # Demonstrate how to delete an entity
- print('Delete the entity')
- table_service.delete_entity(table_name, 'Harp', '1')
- print('Successfully deleted the entity')
- except Exception as e:
- if (config.IS_EMULATED):
- print('Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e)
- else:
- print('Error occurred in the sample. Please make sure the account name and key are correct.', e)
- finally:
- # Demonstrate deleting the table, if you don't want to have the table deleted comment the below block of code
- print('Deleting the table.')
- if(table_service.exists(table_name)):
- table_service.delete_table(table_name)
- print('Successfully deleted the table')
- print('\nAzure Storage Basic Table samples - Completed.\n')
|