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 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 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 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))
def create_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceExistsError, HttpResponseError with TableClient.from_connection_string(self.connection_string, self.table_name) as table_client: try: entity = table_client.create_entity(entity=self.entity) return(self.entity) except ResourceExistsError: print("Entity already exists")
def query_for_all_finished_chunks(self): table_client = TableClient.from_connection_string( self.connection_string, self.table_name) parameters = {'status': TenableStatus.finished.value} name_filter = 'jobStatus eq @status' try: return table_client.query_entities(name_filter, parameters=parameters) except HttpResponseError as e: print(e.message) return []
def query_for_finished_chunks_by_partition_key(self, pk): table_client = TableClient.from_connection_string( self.connection_string, self.table_name) parameters = {'key': pk, 'status': TenableStatus.finished.value} name_filter = 'PartitionKey eq @key and jobStatus eq @status' try: return table_client.query_entities(name_filter, parameters=parameters) except HttpResponseError as e: print(e.message) return []
def query_by_partition_key(self, pk): table_client = TableClient.from_connection_string( self.connection_string, self.table_name) parameters = {u"key": pk} name_filter = u"PartitionKey eq @key" try: return table_client.query_entities(name_filter, parameters=parameters) except HttpResponseError as e: print(e.message) return []
def upsert(self, pk: str, rk: str, data: dict = None): with TableClient.from_connection_string(self.connection_string, self.table_name) as table_client: logging.info(f'upserting {pk} - {rk} on table {self.table_name}') entity_template = { 'PartitionKey': pk, 'RowKey': rk, } if data is not None: entity_template.update(data) return table_client.upsert_entity(mode=UpdateMode.REPLACE, entity=entity_template)
def create_from_table_client(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceExistsError # [START create_table_from_table_client] with TableClient.from_connection_string(conn_str=self.connection_string, table_name="myTable") as table_client: try: table_client.create_table() print("Created table {}!".format(table_client.table_name)) except ResourceExistsError: print("Table already exists")
def delete_from_table_client(self): from azure.data.table import TableClient from azure.core.exceptions import ResourceNotFoundError # [START delete_table_from_table_client] with TableClient.from_connection_string(conn_str=self.connection_string, table_name="myTable") as table_client: try: table_client.delete_table() print("Deleted table {}!".format(table_client.table_name)) except ResourceNotFoundError: print("Table could not be found")
def post(self, pk: str, rk: str, data: dict = None): with TableClient.from_connection_string(self.connection_string, self.table_name) as table_client: entity_template = { 'PartitionKey': pk, 'RowKey': rk, } if data is not None: entity_template.update(data) try: table_client.create_entity(entity_template) except Exception as e: logging.warn('could not post entity to table') logging.warn(e) raise e
def query_for_all_processing_chunks(self): table_client = TableClient.from_connection_string( self.connection_string, self.table_name) parameters = { 'failedStatus': TenableStatus.failed.value, 'processingStatus': TenableStatus.processing.value, 'sentStatus': TenableStatus.sent_to_queue.value, 'sendingStatus': TenableStatus.sending_to_queue.value } name_filter = 'jobStatus eq @failedStatus or jobStatus eq @processingStatus or jobStatus eq @sentStatus or jobStatus eq @sendingStatus' try: return table_client.query_entities(name_filter, parameters=parameters) except HttpResponseError as e: print(e.message) return []
def update_entities(self): # Instantiate a table service client from azure.data.tables import TableClient, 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 entity created = table.create_entity(table_entity_properties=entity) # [START update_entity] # Update the entity created.text = "NewMarker" table.update_entity(mode=UpdateMode.replace, table_entity_properties=created) # Get the replaced entity replaced = table.get_entity(partition_key=created.PartitionKey, row_key=created.RowKey) print(replaced) # Merge the entity replaced.color = "Blue" table.update_entity(mode=UpdateMode.MERGE, table_entity_properties=replaced) # Get the merged entity merged = table.get_entity(partition_key=replaced.PartitionKey, row_key=replaced.RowKey) print(merged) # [END update_entity] finally: # Delete the table table.delete_table()