def delete_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceNotFoundError from azure.core import MatchConditions table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) # Create entity to delete (to showcase etag) entity_created = table_client.create_entity(entity=self.entity) # show without calling metadata, cannot access etag try: entity_created.etag except AttributeError: print("Need to get metadata of entity") # In order to access etag as a part of the entity, need to call metadata on the entity metadata = entity_created.metadata() # Can now get etag etag = metadata['etag'] try: # will delete if match_condition and etag are satisfied table_client.delete_entity(entity=self.entity, etag=etag, match_condition=MatchConditions.IfNotModified) except ResourceNotFoundError: print("EntityDoesNotExists")
def test_create_service_with_custom_account_endpoint_path(self, resource_group, location, storage_account, storage_account_key): custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token for service_type in SERVICES.items(): conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format( storage_account.name, storage_account_key, custom_account_url) # Act service = service_type[0].from_connection_string(conn_string, table_name="foo") # Assert self.assertEqual(service.account_name, storage_account.name) self.assertEqual(service.credential.account_name, storage_account.name) self.assertEqual(service.credential.account_key, storage_account_key) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') service = TableServiceClient(account_url=custom_account_url) self.assertEqual(service.account_name, None) self.assertEqual(service.credential, None) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path')) service = TableClient(account_url=custom_account_url, table_name="foo") self.assertEqual(service.account_name, None) self.assertEqual(service.table_name, "foo") self.assertEqual(service.credential, None) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path')) service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + self.sas_token) self.assertEqual(service.account_name, None) self.assertEqual(service.table_name, "foo") self.assertEqual(service.credential, None) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path'))
def create_entity(client: TableClient, row_key: str, parameters=''): e = Entity.newEntity(parameters) try: client.create_entity(entity=e) except ResourceExistsError: raise EntityAlreadyExistsError()
def test_create_service_with_custom_account_endpoint_path(self): token = self.generate_sas_token() custom_account_url = "http://local-machine:11002/custom/account/path/" + token for service_type in SERVICES.items(): conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format( self.tables_storage_account_name, self.tables_primary_storage_account_key, custom_account_url) # Act service = service_type[0].from_connection_string(conn_string, table_name="foo") # Assert assert service.account_name == self.tables_storage_account_name assert service.credential.named_key.name == self.tables_storage_account_name assert service.credential.named_key.key == self.tables_primary_storage_account_key assert service._primary_hostname == 'local-machine:11002/custom/account/path' service = TableServiceClient(endpoint=custom_account_url) assert service.account_name == "custom" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient(endpoint=custom_account_url, table_name="foo") assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path') service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + token) assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith('http://local-machine:11002/custom/account/path')
def upsert_entity(self): from azure.data.tables import TableClient from azure.data.tables._models import UpdateMode table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) table_client.upsert_entity(entity=self.entity, mode=UpdateMode.REPLACE)
def read(self, client: TableClient, filter_query=None, parameters=None) -> Iterable: if filter_query is None: return client.list_entities() else: return client.query_entities( filter=filter_query, results_per_page=constants.results_per_page)
def test_create_client_for_cosmos_emulator(self): emulator_credential = AzureNamedKeyCredential( 'localhost', self.tables_primary_cosmos_account_key) emulator_connstr = "DefaultEndpointsProtocol=http;AccountName=localhost;AccountKey={};TableEndpoint=http://localhost:8902/;".format( self.tables_primary_cosmos_account_key) client = TableServiceClient.from_connection_string(emulator_connstr) assert client.url == "http://localhost:8902" assert client.account_name == 'localhost' assert client.credential.named_key.name == 'localhost' assert client.credential.named_key.key == self.tables_primary_cosmos_account_key assert client._cosmos_endpoint assert client.scheme == 'http' client = TableServiceClient("http://localhost:8902/", credential=emulator_credential) assert client.url == "http://localhost:8902" assert client.account_name == 'localhost' assert client.credential.named_key.name == 'localhost' assert client.credential.named_key.key == self.tables_primary_cosmos_account_key assert client._cosmos_endpoint assert client.scheme == 'http' table = TableClient.from_connection_string(emulator_connstr, 'tablename') assert table.url == "http://localhost:8902" assert table.account_name == 'localhost' assert table.table_name == 'tablename' assert table.credential.named_key.name == 'localhost' assert table.credential.named_key.key == self.tables_primary_cosmos_account_key assert table._cosmos_endpoint assert table.scheme == 'http' table = TableClient("http://localhost:8902/", "tablename", credential=emulator_credential) assert table.url == "http://localhost:8902" assert table.account_name == 'localhost' assert table.table_name == 'tablename' assert table.credential.named_key.name == 'localhost' assert table.credential.named_key.key == self.tables_primary_cosmos_account_key assert table._cosmos_endpoint assert table.scheme == 'http' table = TableClient.from_table_url( "http://localhost:8902/Tables('tablename')", credential=emulator_credential) assert table.url == "http://localhost:8902" assert table.account_name == 'localhost' assert table.table_name == 'tablename' assert table.credential.named_key.name == 'localhost' assert table.credential.named_key.key == self.tables_primary_cosmos_account_key assert table._cosmos_endpoint assert table.scheme == 'http'
def update_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceNotFoundError table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) try: # defaults to UpdateMode.MERGE table_client.update_entity(entity=self.entity) except ResourceNotFoundError: print("Entity does not exist")
def create_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceExistsError table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) try: inserted_entity = table_client.create_entity(entity=self.entity) # inserted_entity type is dict[str,object] print(inserted_entity.items()) # print out key-value pair of entity except ResourceExistsError: print("EntityExists")
def get_table_data(connection_string, name, filter, logger): # connect to table table_service = "NOTSET" logger.info("Connecting to DataCatalog table (%s), connect string (%s)", name, connection_string) try: table_client = TableClient.from_connection_string( conn_str=connection_string, table_name=name) except: logger.error( "Failed to connect to Data Catalog table (%s), exiting...", name) logger.error("ERROR: " + str(sys.exc_info()[0])) return None logger.info("Connected to table") # read entries entities = "NOTSET" logger.info("Reading table entries (%s)", name) try: entities = table_client.list_entities(results_per_page=100, select="*") except: logger.error( "Failed to retrieve Data Catalog entries (%s), exiting...", name) logger.error("ERROR: " + str(sys.exc_info()[0])) return None if entities == "NOTSET": logger.error( "No entries read from Data Catalog table (%s), exiting...", name) return None else: logger.error("Catalog entries retrieved from Data Catalog table (%s)", name) return entities
def test_create_service_with_token(self): url = self.account_url(self.tables_storage_account_name, "table") suffix = '.table.core.windows.net' self.token_credential = self.generate_fake_token() service = TableClient(url, credential=self.token_credential, table_name='foo') # Assert assert service is not None assert service.account_name == self.tables_storage_account_name assert service.url.startswith('https://' + self.tables_storage_account_name + suffix) assert service.credential == self.token_credential assert not hasattr(service.credential, 'account_key') assert hasattr(service.credential, 'get_token') service = TableServiceClient(url, credential=self.token_credential, table_name='foo') # Assert assert service is not None assert service.account_name == self.tables_storage_account_name assert service.url.startswith('https://' + self.tables_storage_account_name + suffix) assert service.credential == self.token_credential assert not hasattr(service.credential, 'account_key') assert hasattr(service.credential, 'get_token')
def create_and_get_entities(self): # Instantiate a table service client from azure.data.tables import TableClient with TableClient.from_connection_string(self.connection_string, table_name="mytable3") as table: # Create the Table table.create_table() my_entity = { "PartitionKey": "color", "RowKey": "brand", "text": "Marker", "color": "Purple", "price": 4.99, "last_updated": datetime.today(), "product_id": uuid4(), "inventory_count": 42, "barcode": b"135aefg8oj0ld58" } try: # [START create_entity] created_entity = table.create_entity(entity=my_entity) print("Created entity: {}".format(created_entity)) # [END create_entity] # [START get_entity] # Get Entity by partition and row key got_entity = table.get_entity(partition_key=my_entity["PartitionKey"], row_key=my_entity["RowKey"]) print("Received entity: {}".format(got_entity)) # [END get_entity] finally: # Delete the table table.delete_table()
def update_entity(self, id, amount): from azure.data.tables import TableClient, UpdateMode with TableClient.from_connection_string(self.connection_string, self.table_name) as table_client: try: parameters = { "id": id } name_filter = "id eq @id" #name_filter = u"id eq guid'279cdc13-ff2e-4ebf-88bd-faccdb0015f9'" get_entity = table_client.query_entities(filter=name_filter, parameters=parameters) for entity_chosen in get_entity: # print(entity_chosen) # Merge the entity entity_chosen.balance = amount table_client.update_entity(mode=UpdateMode.MERGE, entity=entity_chosen) # Get the merged entity merged = table_client.get_entity(partition_key=entity_chosen.PartitionKey, row_key=entity_chosen.RowKey) #print("SUCCESSFUL MERGED ENTITY: {}".format(merged)) return (entity_chosen) except HttpResponseError as e: print(e.message)
def sample_query_entities(self): from azure.data.tables import TableClient from azure.core.exceptions import HttpResponseError table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) try: queried_entities = table_client.query_entities(filter=self.name_filter, select="brand,color") # queried_entities type is ItemPaged for entity_chosen in queried_entities: # create a list of the entities and iterate through them to print each one out # calls to the service to get more entities are made without user knowledge print(entity_chosen) except HttpResponseError as e: print(e.message)
def create_and_get_entities(self): # Instantiate a table service client from azure.data.tables import TableClient table = TableClient.from_connection_string(self.connection_string, table_name="mytable3") # Create the Table table.create_table() my_entity = { 'PartitionKey': 'color', 'RowKey': 'crayola', 'text': 'Marker', 'color': 'Purple', 'price': '5' } try: # [START create_entity] created_entity = table.create_entity( table_entity_properties=my_entity) print(created_entity) # [END create_entity] # [START get_entity] # Get Entity by partition and row key got_entity = table.get_entity( partition_key=my_entity['PartitionKey'], row_key=my_entity['RowKey']) print(got_entity) # [END get_entity] finally: # Delete the table table.delete_table()
def create_table_client(self): # Instantiate a TableServiceClient using a connection string # [START create_table_client] from azure.data.tables import TableClient table_client = TableClient.from_connection_string( conn_str=self.connection_string, table_name="tableName") print("Table name: {}".format(table_client.table_name))
def delete_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceNotFoundError, ResourceExistsError from azure.core.credentials import AzureNamedKeyCredential credential = AzureNamedKeyCredential(self.account_name, self.access_key) with TableClient(account_url=self.account_url, credential=credential, table_name=self.table_name) as table_client: # Create entity to delete (to showcase etag) try: resp = table_client.create_entity(entity=self.entity) except ResourceExistsError: print("Entity already exists!") # [START delete_entity] try: table_client.delete_entity( row_key=self.entity["RowKey"], partition_key=self.entity["PartitionKey"]) print("Successfully deleted!") except ResourceNotFoundError: print("Entity does not exists")
def create_and_get_entities(self): # Instantiate a table service client from azure.data.tables import TableClient with TableClient.from_connection_string( self.connection_string, table_name="mytable3") as table: # Create the Table table.create_table() my_entity = { u"PartitionKey": u"color", u"RowKey": u"crayola", u"text": u"Marker", u"color": u"Purple", u"price": u"5", } try: # [START create_entity] created_entity = table.create_entity(entity=my_entity) print("Created entity: {}".format(created_entity)) # [END create_entity] # [START get_entity] # Get Entity by partition and row key got_entity = table.get_entity( partition_key=my_entity["PartitionKey"], row_key=my_entity["RowKey"]) print("Received entity: {}".format(got_entity)) # [END get_entity] finally: # Delete the table table.delete_table()
def getTableClient(table_name): from azure.data.tables import TableClient Validation.validateTableName(table_name) return TableClient.from_connection_string( Config.get_connection_string(), table_name)
class SampleTweets(Resource): client = TableClient.from_connection_string( conn_str=CONNECTION_STRING, table_name=TABLE_NAME) def get(self): start_date = request.args.get("start_date") end_date = request.args.get("end_date") count = int(request.args.get("count")) tweet_rows = self.client.query_entities( filter="CreatedAt ge datetime'" + start_date + "' and CreatedAt lt datetime'" + end_date + "'", select=[u"SearchHashtag", u"Text", u"CognitiveServicesSentimentScore"]) response_content = [] i = 0 for tweet_row in tweet_rows: del tweet_row["_metadata"] response_content.append(tweet_row) if i == count: break i += 1 return response_content
def create_and_get_entities(self): # Instantiate a table service client from azure.data.tables import TableClient with TableClient.from_connection_string(self.connection_string, table_name="mytable3") as table: # Create the Table table.create_table() my_entity = { u'PartitionKey': u'color', u'RowKey': u'crayola', u'text': u'Marker', u'color': u'Purple', u'price': u'5' } try: # [START create_entity] created_entity = table.create_entity(entity=my_entity) print("Created entity: {}".format(created_entity)) # [END create_entity] # [START get_entity] # Get Entity by partition and row key got_entity = table.get_entity( partition_key=my_entity['PartitionKey'], row_key=my_entity['RowKey'] ) print("Received entity: {}".format(got_entity)) # [END get_entity] finally: # Delete the table table.delete_table()
def list_all_entities(self): # Instantiate a table service client from azure.data.tables import TableClient with TableClient.from_connection_string(self.connection_string, table_name="mytable4") as table: # Create the table table.create_table() entity = {u'PartitionKey': u'color2', u'RowKey': u'sharpie', u'text': u'Marker', u'color': u'Purple', u'price': u'5'} entity1 = {u'PartitionKey': u'color2', u'RowKey': u'crayola', u'text': u'Marker', u'color': u'Red', u'price': u'3'} try: # Create entities table.create_entity(entity=entity) table.create_entity(entity=entity1) # [START query_entities] # Query the entities in the table entities = list(table.list_entities()) for i, entity in enumerate(entities): print("Entity #{}: {}".format(entity, i)) # [END query_entities] finally: # Delete the table table.delete_table()
def run(args): connection_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING') with TableClient.from_connection_string(connection_str, args.table) as table_client: try: table_client.create_table() except HttpResponseError: print( "Table already exists, do you want to replace? If no data will be merged. (yes/no)" ) answer = input() if answer == 'yes': table_client.delete_table() print( 'Table deleted, try to run the script after some time...') return entities = create_entities(args.students, False) entities += create_entities(args.admins, True) for i in entities: try: table_client.create_entity(i) except ResourceExistsError: print(f'Entity already exists ${i.PartitionKey}, skipped!')
def insert_random_entities(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceExistsError brands = [u"Crayola", u"Sharpie", u"Chameleon"] colors = [u"red", u"blue", u"orange", u"yellow"] names = [u"marker", u"pencil", u"pen"] entity_template = { u"PartitionKey": u"pk", u"RowKey": u"row", } with TableClient.from_connection_string( self.connection_string, self.table_name) as table_client: try: table_client.create_table() except ResourceExistsError: print(u"Table already exists") for i in range(25): e = copy.deepcopy(entity_template) try: e[u"RowKey"] += unicode(i) except NameError: e[u"RowKey"] += str(i) e[u"Name"] = random.choice(names) e[u"Brand"] = random.choice(brands) e[u"Color"] = random.choice(colors) e[u"Value"] = random.randint(0, 100) table_client.create_entity(entity=e)
def test_create_service_with_custom_account_endpoint_path( self, tables_cosmos_account_name, tables_primary_cosmos_account_key): custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token for service_type in SERVICES.items(): conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format( tables_cosmos_account_name, tables_primary_cosmos_account_key, custom_account_url) # Act service = service_type[0].from_connection_string(conn_string, table_name="foo") # Assert assert service.account_name == tables_cosmos_account_name assert service.credential.account_name == tables_cosmos_account_name assert service.credential.account_key == tables_primary_cosmos_account_key assert service._primary_hostname == 'local-machine:11002/custom/account/path' service = TableServiceClient(account_url=custom_account_url) assert service.account_name == None assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' # mine doesnt have a question mark at the end assert service.url.startswith( 'http://local-machine:11002/custom/account/path') service = TableClient(account_url=custom_account_url, table_name="foo") assert service.account_name == None assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith( 'http://local-machine:11002/custom/account/path') service = TableClient.from_table_url( "http://local-machine:11002/custom/account/path/foo" + self.sas_token) assert service.account_name == None assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith( 'http://local-machine:11002/custom/account/path') if self.is_live: sleep(SLEEP_DELAY)
def test_create_service_with_custom_account_endpoint_path(self): self.sas_token = AzureSasCredential(self.generate_sas_token()) custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token.signature for service_type in SERVICES.items(): conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format( self.tables_cosmos_account_name, self.tables_primary_cosmos_account_key, custom_account_url) # Act service = service_type[0].from_connection_string(conn_string, table_name="foo") # Assert assert service.account_name == self.tables_cosmos_account_name assert service.credential.named_key.name == self.tables_cosmos_account_name assert service.credential.named_key.key == self.tables_primary_cosmos_account_key assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.scheme == 'http' service = TableServiceClient(endpoint=custom_account_url) assert service.account_name == "custom" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' # mine doesnt have a question mark at the end assert service.url.startswith( 'http://local-machine:11002/custom/account/path') assert service.scheme == 'http' service = TableClient(endpoint=custom_account_url, table_name="foo") assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith( 'http://local-machine:11002/custom/account/path') assert service.scheme == 'http' service = TableClient.from_table_url( "http://local-machine:11002/custom/account/path/foo" + self.sas_token.signature) assert service.account_name == "custom" assert service.table_name == "foo" assert service.credential == None assert service._primary_hostname == 'local-machine:11002/custom/account/path' assert service.url.startswith( 'http://local-machine:11002/custom/account/path') assert service.scheme == 'http'
def get(self, pk: str, rk: str): with TableClient.from_connection_string(self.connection_string, self.table_name) as table_client: try: logging.info( f'looking for {pk} - {rk} on table {self.table_name}') return table_client.get_entity(pk, rk) except ResourceNotFoundError: return None
def delete_from_table_client(self): from azure.data.table import TableClient from azure.core.exceptions import HttpResponseError # [START delete_table_from_table_client] with TableClient.from_connection_string(conn_str=self.connection_string, table_name="myTable") as table_client: table_client.delete_table() print("Deleted table {}!".format(table_client.table_name))
def update_entities(self): # Instantiate a table service client from azure.data.tables import TableClient from azure.data.tables import UpdateMode table = TableClient.from_connection_string(self.connection_string, table_name="mytable6") # Create the table and Table Client table.create_table() entity = { 'PartitionKey': 'color', 'RowKey': 'sharpie', 'text': 'Marker', 'color': 'Purple', 'price': '5' } try: # Create entities created = table.create_entity(entity=entity) # [START upsert_entity] # Try Replace and then Insert on Fail insert_entity = table.upsert_entity(mode=UpdateMode.REPLACE, entity=entity1) print("Inserted entity: {}".format(insert_entity)) # Try merge, and merge since already in table created.text = "NewMarker" merged_entity = table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) print("Merged entity: {}".format(merged_entity)) # [END upsert_entity] # [START update_entity] # Update the entity created.text = "NewMarker" table.update_entity(mode=UpdateMode.REPLACE, entity=created) # Get the replaced entity replaced = table.get_entity(partition_key=created.PartitionKey, row_key=created.RowKey) print("Replaced entity: {}".format(replaced)) # Merge the entity replaced.color = "Blue" table.update_entity(mode=UpdateMode.MERGE, entity=replaced) # Get the merged entity merged = table.get_entity(partition_key=replaced.PartitionKey, row_key=replaced.RowKey) print("Merged entity: {}".format(merged)) # [END update_entity] finally: # Delete the table table.delete_table()
def sample_batching(self): # Instantiate a TableServiceClient using a connection string entity1 = { 'PartitionKey': 'pk001', 'RowKey': 'rk001', 'Value': 4, 'day': "Monday", 'float': 4.003 } entity2 = { 'PartitionKey': 'pk001', 'RowKey': 'rk002', 'Value': 4, 'day': "Tuesday", 'float': 4.003 } entity3 = { 'PartitionKey': 'pk001', 'RowKey': 'rk003', 'Value': 4, 'day': "Wednesday", 'float': 4.003 } entity4 = { 'PartitionKey': 'pk001', 'RowKey': 'rk004', 'Value': 4, 'day': "Thursday", 'float': 4.003 } # [START batching] from azure.data.tables import TableClient, UpdateMode, BatchErrorException from azure.core.exceptions import ResourceExistsError self.table_client = TableClient.from_connection_string( conn_str=self.connection_string, table_name=self.table_name) try: self.table_client.create_table() print("Created table") except ResourceExistsError: print("Table already exists") self.table_client.create_entity(entity2) self.table_client.create_entity(entity3) self.table_client.create_entity(entity4) batch = self.table_client.create_batch() batch.create_entity(entity1) batch.delete_entity(entity2['PartitionKey'], entity2['RowKey']) batch.upsert_entity(entity3) batch.update_entity(entity4, mode=UpdateMode.REPLACE) try: self.table_client.send_batch(batch) except BatchErrorException as e: print("There was an error with the batch operation") print("Error: {}".format(e))