table_basic_samples.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #-------------------------------------------------------------------------
  2. # Microsoft Developer & Platform Evangelism
  3. #
  4. # Copyright (c) Microsoft Corporation. All rights reserved.
  5. #
  6. # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. # OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. #----------------------------------------------------------------------------------
  10. # The example companies, organizations, products, domain names,
  11. # e-mail addresses, logos, people, places, and events depicted
  12. # herein are fictitious. No association with any real company,
  13. # organization, product, domain name, email address, logo, person,
  14. # places, or events is intended or should be inferred.
  15. #--------------------------------------------------------------------------
  16. import config
  17. from random_data import RandomData
  18. from azure.storage import CloudStorageAccount
  19. from azure.storage.table import TableService, Entity
  20. #
  21. # Azure Table Service Sample - Demonstrate how to perform common tasks using the Microsoft Azure Table Service
  22. # including creating a table, CRUD operations and different querying techniques.
  23. #
  24. # Documentation References:
  25. # - What is a Storage Account - http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/
  26. # - Getting Started with Tables - https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-table-storage/
  27. # - Table Service Concepts - http://msdn.microsoft.com/en-us/library/dd179463.aspx
  28. # - Table Service REST API - http://msdn.microsoft.com/en-us/library/dd179423.aspx
  29. # - Table Service Python API - http://azure.github.io/azure-storage-ruby/
  30. # - Storage Emulator - http://azure.microsoft.com/en-us/documentation/articles/storage-use-emulator/
  31. #
  32. class TableBasicSamples():
  33. def __init__(self):
  34. self.random_data = RandomData()
  35. # Runs all samples for Azure Storage Table service.
  36. def run_all_samples(self, account):
  37. print('Azure Storage Basic Table samples - Starting.')
  38. table_name = 'tablebasics' + self.random_data.get_random_name(6)
  39. table_service = None
  40. try:
  41. table_service = account.create_table_service()
  42. # Create a new table
  43. print('Create a table with name - ' + table_name)
  44. try:
  45. table_service.create_table(table_name)
  46. except Exception as err:
  47. print('Error creating table, ' + table_name + 'check if it already exists')
  48. # Create a sample entity to insert into the table
  49. customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '555-555-5555'}
  50. # Insert the entity into the table
  51. print('Inserting a new entity into table - ' + table_name)
  52. table_service.insert_entity(table_name, customer)
  53. print('Successfully inserted the new entity')
  54. # Demonstrate how to query the entity
  55. print('Read the inserted entity.')
  56. entity = table_service.get_entity(table_name, 'Harp', '1')
  57. print(entity['email'])
  58. print(entity['phone'])
  59. # Demonstrate how to update the entity by changing the phone number
  60. print('Update an existing entity by changing the phone number')
  61. customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '425-123-1234'}
  62. table_service.update_entity(table_name, customer)
  63. # Demonstrate how to query the updated entity, filter the results with a filter query and select only the value in the phone column
  64. print('Read the updated entity with a filter query')
  65. entities = table_service.query_entities(table_name, filter="PartitionKey eq 'Harp'", select='phone')
  66. for entity in entities:
  67. print(entity['phone'])
  68. # Demonstrate how to delete an entity
  69. print('Delete the entity')
  70. table_service.delete_entity(table_name, 'Harp', '1')
  71. print('Successfully deleted the entity')
  72. except Exception as e:
  73. if (config.IS_EMULATED):
  74. print('Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e)
  75. else:
  76. print('Error occurred in the sample. Please make sure the account name and key are correct.', e)
  77. finally:
  78. # Demonstrate deleting the table, if you don't want to have the table deleted comment the below block of code
  79. print('Deleting the table.')
  80. if(table_service.exists(table_name)):
  81. table_service.delete_table(table_name)
  82. print('Successfully deleted the table')
  83. print('\nAzure Storage Basic Table samples - Completed.\n')