Class TableClient
Overview
The client encapsulates the URL for the table within the Tables service endpoint, the name of the table, and the credentials for accessing the storage or CosmosDB table API account. It provides synchronous methods to create and delete the table itself, as well as methods to create, upsert, update, delete, list, and get entities within the table. These methods invoke REST API operations to make the requests and obtain the results that are returned.
Getting Started
Authenticating and building instances of this client are handled by TableClientBuilder
.
This sample shows how to authenticate and build a TableClient instance using the TableClientBuilder
and
a connection string.
TableClient tableClient = new TableClientBuilder() .connectionString("connectionstring") .tableName("myTable") .buildClient();
For more information on building and authenticating, see the TableClientBuilder
documentation.
The following code samples provide examples of common operations preformed with this client.
Create a TableEntity
The createEntity
method can be used to create a table entity within a table in your Azure Storage or Azure Cosmos account.
The sample below creates a TableEntity
with a partition key of "partitionKey" and a row key of "rowKey".
TableEntity tableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.createEntity(tableEntity); System.out.printf("Table entity with partition key '%s' and row key: '%s' was created.", "partitionKey", "rowKey");Note: for asynchronous sample, refer to
the asynchronous client
.
Retrieve a TableEntity
The getEntity
method can be used to retrieve a table entity within a table in your Azure Storage or Azure Cosmos account.
The sample below retrieves a TableEntity
with a partition key of "partitionKey" and a row key of "rowKey".
TableEntity tableEntity = tableClient.getEntity("partitionKey", "rowKey"); System.out.printf("Retrieved entity with partition key '%s' and row key '%s'.", tableEntity.getPartitionKey(), tableEntity.getRowKey());Note: for asynchronous sample, refer to
the asynchronous client
.
Update a TableEntity
The updateEntity
method can be used to update a table entity within a table in your Azure Storage or Azure Cosmos account.
The sample below updates a TableEntity
with a partition key of "partitionKey" and a row key of "rowKey", adding a new property with a key of "Property" and a value of "Value".
TableEntity myTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.updateEntity(myTableEntity, TableEntityUpdateMode.REPLACE); System.out.printf("Table entity with partition key '%s' and row key: '%s' was updated/created.", "partitionKey", "rowKey");Note: for asynchronous sample, refer to
the asynchronous client
.
List TableEntities
The listEntities
method can be used to list the entities within a table in your Azure Storage or Azure Cosmos account.
The following sample lists all TableEntities
within the table without filtering out any entities.
PagedIterable<TableEntity> tableEntities = tableClient.listEntities(); tableEntities.forEach(tableEntity -> System.out.printf("Retrieved entity with partition key '%s' and row key '%s'.%n", tableEntity.getPartitionKey(), tableEntity.getRowKey()));List
TableEntities
with filtering and selecting
The following sample lists TableEntities
within the table, filtering out any entities that do not have a partition key of "partitionKey" and a row key of "rowKey"
and only selects the "name", "lastname", and "age" properties.
List<String> propertiesToSelect = new ArrayList<>(); propertiesToSelect.add("name"); propertiesToSelect.add("lastname"); propertiesToSelect.add("age"); ListEntitiesOptions listEntitiesOptions = new ListEntitiesOptions() .setTop(15) .setFilter("PartitionKey eq 'MyPartitionKey' and RowKey eq 'MyRowKey'") .setSelect(propertiesToSelect); PagedIterable<TableEntity> myTableEntities = tableClient.listEntities(listEntitiesOptions, Duration.ofSeconds(5), null); myTableEntities.forEach(tableEntity -> { System.out.printf("Retrieved entity with partition key '%s', row key '%s' and properties:%n", tableEntity.getPartitionKey(), tableEntity.getRowKey()); tableEntity.getProperties().forEach((key, value) -> System.out.printf("Name: '%s'. Value: '%s'.%n", key, value)); });Note: for asynchronous sample, refer to
the asynchronous client
.
Delete a TableEntity
The deleteEntity
method can be used to delete a table entity within a table in your Azure Storage or Azure Cosmos account.
The sample below deletes a TableEntity
with a partition key of "partitionKey" and a row key of "rowKey".
tableClient.deleteEntity("partitionKey", "rowKey"); System.out.printf("Table entity with partition key '%s' and row key: '%s' was deleted.", "partitionKey", "rowKey");Note: for asynchronous sample, refer to
the asynchronous client
.
Submit a transactional batch
The submitTransaction
method can be used to submit a transactional batch of actions to perform on the table in your Azure Storage or Azure Cosmos account.
The following sample shows how to prepare and submit a transactional batch with multiple actions.
List<TableTransactionAction> transactionActions = new ArrayList<>(); String partitionKey = "markers"; String firstEntityRowKey = "m001"; String secondEntityRowKey = "m002"; TableEntity firstEntity = new TableEntity(partitionKey, firstEntityRowKey) .addProperty("Type", "Dry") .addProperty("Color", "Red"); transactionActions.add(new TableTransactionAction(TableTransactionActionType.CREATE, firstEntity)); System.out.printf("Added create action for entity with partition key '%s', and row key '%s'.%n", partitionKey, firstEntityRowKey); TableEntity secondEntity = new TableEntity(partitionKey, secondEntityRowKey) .addProperty("Type", "Wet") .addProperty("Color", "Blue"); transactionActions.add(new TableTransactionAction(TableTransactionActionType.CREATE, secondEntity)); System.out.printf("Added create action for entity with partition key '%s', and row key '%s'.%n", partitionKey, secondEntityRowKey); TableTransactionResult tableTransactionResult = tableClient.submitTransaction(transactionActions); System.out.print("Submitted transaction. The ordered response status codes for the actions are:"); tableTransactionResult.getTransactionActionResponses().forEach(tableTransactionActionResponse -> System.out.printf("%n%d", tableTransactionActionResponse.getStatusCode()));Note: for asynchronous sample, refer to
the asynchronous client
. - See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
createEntity
(TableEntity entity) Inserts anentity
into the table.com.azure.core.http.rest.Response
<Void> createEntityWithResponse
(TableEntity entity, Duration timeout, com.azure.core.util.Context context) Inserts anentity
into the table.Creates the table within the Tables service.com.azure.core.http.rest.Response
<TableItem> createTableWithResponse
(Duration timeout, com.azure.core.util.Context context) Creates the table within the Tables service.void
deleteEntity
(TableEntity entity) Deletes anentity
from the table.void
deleteEntity
(String partitionKey, String rowKey) Deletes anentity
from the table.com.azure.core.http.rest.Response
<Void> deleteEntityWithResponse
(TableEntity entity, boolean ifUnchanged, Duration timeout, com.azure.core.util.Context context) Deletes anentity
from the table.void
Deletes the table within the Tables service.com.azure.core.http.rest.Response
<Void> deleteTableWithResponse
(Duration timeout, com.azure.core.util.Context context) Deletes the table within the Tables service.generateSas
(TableSasSignatureValues tableSasSignatureValues) Generates a service SAS for the table using the specifiedTableSasSignatureValues
.Retrieves details about any storedaccess policies
specified on the table that may be used with Shared Access Signatures.com.azure.core.http.rest.Response
<TableAccessPolicies> getAccessPoliciesWithResponse
(Duration timeout, com.azure.core.util.Context context) Retrieves details about any storedaccess policies
specified on the table that may be used with Shared Access Signatures.Gets the name of the account containing the table.Gets a singleentity
from the table.com.azure.core.http.rest.Response
<TableEntity> getEntityWithResponse
(String partitionKey, String rowKey, List<String> select, Duration timeout, com.azure.core.util.Context context) Gets a singleentity
from the table.Gets the REST API version used by this client.Gets the endpoint for this table.Gets the name of the table.com.azure.core.http.rest.PagedIterable
<TableEntity> Lists allentities
within the table.com.azure.core.http.rest.PagedIterable
<TableEntity> listEntities
(ListEntitiesOptions options, Duration timeout, com.azure.core.util.Context context) Listsentities
using the parameters in the provided options.void
setAccessPolicies
(List<TableSignedIdentifier> tableSignedIdentifiers) Sets storedaccess policies
for the table that may be used with Shared Access Signatures.com.azure.core.http.rest.Response
<Void> setAccessPoliciesWithResponse
(List<TableSignedIdentifier> tableSignedIdentifiers, Duration timeout, com.azure.core.util.Context context) Sets storedaccess policies
for the table that may be used with Shared Access Signatures.submitTransaction
(List<TableTransactionAction> transactionActions) Executes allactions
within the list inside a transaction.com.azure.core.http.rest.Response
<TableTransactionResult> submitTransactionWithResponse
(List<TableTransactionAction> transactionActions, Duration timeout, com.azure.core.util.Context context) Executes allactions
within the list inside a transaction.void
updateEntity
(TableEntity entity) void
updateEntity
(TableEntity entity, TableEntityUpdateMode updateMode) Updates an existingentity
using the specifiedupdate mode
.com.azure.core.http.rest.Response
<Void> updateEntityWithResponse
(TableEntity entity, TableEntityUpdateMode updateMode, boolean ifUnchanged, Duration timeout, com.azure.core.util.Context context) Updates an existingentity
using the specifiedupdate mode
.void
upsertEntity
(TableEntity entity) com.azure.core.http.rest.Response
<Void> upsertEntityWithResponse
(TableEntity entity, TableEntityUpdateMode updateMode, Duration timeout, com.azure.core.util.Context context) Inserts anentity
into the table if it does not exist, or updates the existingentity
using the specifiedupdate mode
otherwise.
-
Method Details
-
getTableName
Gets the name of the table.- Returns:
- The name of the table.
-
getAccountName
Gets the name of the account containing the table.- Returns:
- The name of the account containing the table.
-
getTableEndpoint
Gets the endpoint for this table.- Returns:
- The endpoint for this table.
-
getServiceVersion
Gets the REST API version used by this client.- Returns:
- The REST API version used by this client.
-
generateSas
Generates a service SAS for the table using the specifiedTableSasSignatureValues
.Note: The client must be authenticated via
AzureNamedKeyCredential
.See
TableSasSignatureValues
for more information on how to construct a service SAS.- Parameters:
tableSasSignatureValues
-TableSasSignatureValues
.- Returns:
- A
String
representing the SAS query parameters. - Throws:
IllegalStateException
- If thisTableClient
is not authenticated with anAzureNamedKeyCredential
.
-
createTable
Creates the table within the Tables service.Code Samples
Creates a table. Prints out the details of the created table.
TableItem tableItem = tableClient.createTable(); System.out.printf("Table with name '%s' was created.", tableItem.getName());
- Returns:
- A
TableItem
that represents the table. - Throws:
TableServiceException
- If a table with the same name already exists within the service.
-
createTableWithResponse
public com.azure.core.http.rest.Response<TableItem> createTableWithResponse(Duration timeout, com.azure.core.util.Context context) Creates the table within the Tables service.Code Samples
Creates a table. Prints out the details of the
HTTP response
and the created table.Response<TableItem> response = tableClient.createTableWithResponse(Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. Table with name '%s' was created.", response.getStatusCode(), response.getValue().getName());
- Parameters:
timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
containing aTableItem
that represents the table. - Throws:
TableServiceException
- If a table with the same name already exists within the service.
-
deleteTable
public void deleteTable()Deletes the table within the Tables service.Code Samples
Deletes a table.
tableClient.deleteTable(); System.out.print("Table was deleted.");
- Throws:
TableServiceException
- If the request is rejected by the service.
-
deleteTableWithResponse
public com.azure.core.http.rest.Response<Void> deleteTableWithResponse(Duration timeout, com.azure.core.util.Context context) Deletes the table within the Tables service.Code Samples
Deletes a table. Prints out the details of the
HTTP response
.Response<Void> response = tableClient.deleteTableWithResponse(Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Table was deleted successfully with status code: %d.", response.getStatusCode());
- Parameters:
timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
. - Throws:
TableServiceException
- If the request is rejected by the service.
-
createEntity
Inserts anentity
into the table.Code Samples
Inserts an
entity
into the table. Prints out the details of the createdentity
.TableEntity tableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.createEntity(tableEntity); System.out.printf("Table entity with partition key '%s' and row key: '%s' was created.", "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to insert.- Throws:
TableServiceException
- If anentity
with the same partition key and row key already exists within the table.IllegalArgumentException
- If the providedentity
isnull
.
-
createEntityWithResponse
public com.azure.core.http.rest.Response<Void> createEntityWithResponse(TableEntity entity, Duration timeout, com.azure.core.util.Context context) Inserts anentity
into the table.Code Samples
Inserts an
entity
into the table. Prints out the details of theHTTP response
and the createdentity
.TableEntity myTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); Response<Void> response = tableClient.createEntityWithResponse(myTableEntity, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. Table entity with partition key '%s' and row key" + " '%s' was created.", response.getStatusCode(), "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to insert.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
. - Throws:
TableServiceException
- If anentity
with the same partition key and row key already exists within the table.IllegalArgumentException
- If the providedentity
isnull
.
-
upsertEntity
Inserts anentity
into the table if it does not exist, or merges theentity
with the existingentity
otherwise.Code Samples
Upserts an
entity
into the table. Prints out the details of the upsertedentity
.TableEntity tableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.upsertEntity(tableEntity); System.out.printf("Table entity with partition key '%s' and row key: '%s' was updated/created.", "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to upsert.- Throws:
IllegalArgumentException
- If the providedentity
isnull
.TableServiceException
- If the request is rejected by the service.
-
upsertEntityWithResponse
public com.azure.core.http.rest.Response<Void> upsertEntityWithResponse(TableEntity entity, TableEntityUpdateMode updateMode, Duration timeout, com.azure.core.util.Context context) Inserts anentity
into the table if it does not exist, or updates the existingentity
using the specifiedupdate mode
otherwise. The defaultupdate mode
isMERGE
.When the
update mode
isMERGE
, the providedentity
's properties will be merged into the existingentity
. When theupdate mode
isREPLACE
, the providedentity
's properties will completely replace those in the existingentity
.Code Samples
Upserts an
entity
into the table with the specifiedupdate mode
if saidentity
already exists. Prints out the details of theHTTP response
and the upsertedentity
.TableEntity myTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); Response<Void> response = tableClient.upsertEntityWithResponse(myTableEntity, TableEntityUpdateMode.REPLACE, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. Table entity with partition key '%s' and row key" + " '%s' was updated/created.", response.getStatusCode(), "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to upsert.updateMode
- The type of update to perform if theentity
already exits.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
. - Throws:
IllegalArgumentException
- If the providedentity
isnull
.TableServiceException
- If the request is rejected by the service.
-
updateEntity
Updates an existingentity
by merging the providedentity
with the existingentity
.Code Samples
Updates a
entity
on the table. Prints out the details of the updatedentity
.TableEntity tableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.updateEntity(tableEntity); System.out.printf("Table entity with partition key '%s' and row key: '%s' was updated/created.", "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to update.- Throws:
IllegalArgumentException
- If the providedentity
isnull
.TableServiceException
- If noentity
with the same partition key and row key exists within the table.
-
updateEntity
Updates an existingentity
using the specifiedupdate mode
. The defaultupdate mode
isMERGE
.When the
update mode
isMERGE
, the providedentity
's properties will be merged into the existingentity
. When theupdate mode
isREPLACE
, the providedentity
's properties will completely replace those in the existingentity
.Code Samples
Updates a
entity
on the table with the specifiedupdate mode
. Prints out the details of the updatedentity
.TableEntity myTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.updateEntity(myTableEntity, TableEntityUpdateMode.REPLACE); System.out.printf("Table entity with partition key '%s' and row key: '%s' was updated/created.", "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to update.updateMode
- The type of update to perform.- Throws:
IllegalArgumentException
- If the providedentity
isnull
.TableServiceException
- If noentity
with the same partition key and row key exists within the table.
-
updateEntityWithResponse
public com.azure.core.http.rest.Response<Void> updateEntityWithResponse(TableEntity entity, TableEntityUpdateMode updateMode, boolean ifUnchanged, Duration timeout, com.azure.core.util.Context context) Updates an existingentity
using the specifiedupdate mode
. The defaultupdate mode
isMERGE
.When the
update mode
isMERGE
, the providedentity
's properties will be merged into the existingentity
. When theupdate mode
isREPLACE
, the providedentity
's properties will completely replace those in the existingentity
.Code Samples
Updates a
entity
on the table with the specifiedupdate mode
if theETags
on bothentities
match. Prints out the details of theHTTP response
updatedentity
.TableEntity someTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); Response<Void> response = tableClient.updateEntityWithResponse(someTableEntity, TableEntityUpdateMode.REPLACE, true, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. Table entity with partition key '%s' and row key" + " '%s' was updated.", response.getStatusCode(), "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to update.updateMode
- The type of update to perform.ifUnchanged
- When true, the ETag of the providedentity
must match the ETag of theentity
in the Table service. If the values do not match, the update will not occur and an exception will be thrown.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
. - Throws:
IllegalArgumentException
- If the providedentity
isnull
.TableServiceException
- If noentity
with the same partition key and row key exists within the table, or ififUnchanged
istrue
and the existingentity
's ETag does not match that of the providedentity
.
-
deleteEntity
Deletes anentity
from the table.Code Samples
Deletes an
entity
on the table. Prints out the entity'spartitionKey
androwKey
.tableClient.deleteEntity("partitionKey", "rowKey"); System.out.printf("Table entity with partition key '%s' and row key: '%s' was deleted.", "partitionKey", "rowKey");
- Parameters:
partitionKey
- The partition key of theentity
.rowKey
- The row key of theentity
.- Throws:
IllegalArgumentException
- If the providedpartitionKey
orrowKey
arenull
or empty.TableServiceException
- If the request is rejected by the service.IllegalArgumentException
- If 'partitionKey' or 'rowKey' is null.
-
deleteEntity
Deletes anentity
from the table.Code Samples
Deletes a
entity
on the table. Prints out the details of the deletedentity
.TableEntity myTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); tableClient.deleteEntity(myTableEntity); System.out.printf("Table entity with partition key '%s' and row key: '%s' was created.", "partitionKey", "rowKey");
- Parameters:
entity
- Theentity
to delete.- Throws:
TableServiceException
- If the request is rejected by the service.
-
deleteEntityWithResponse
public com.azure.core.http.rest.Response<Void> deleteEntityWithResponse(TableEntity entity, boolean ifUnchanged, Duration timeout, com.azure.core.util.Context context) Deletes anentity
from the table.Code Samples
Deletes a
entity
on the table. Prints out the details of theHTTP response
and the deletedentity
.TableEntity someTableEntity = new TableEntity("partitionKey", "rowKey") .addProperty("Property", "Value"); Response<Void> response = tableClient.deleteEntityWithResponse(someTableEntity, true, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. Table entity with partition key '%s' and row key" + " '%s' was deleted.", response.getStatusCode(), "partitionKey", "rowKey");
- Parameters:
entity
- The tableentity
to delete.ifUnchanged
- When true, the ETag of the providedentity
must match the ETag of theentity
in the Table service. If the values do not match, the update will not occur and an exception will be thrown.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
. - Throws:
TableServiceException
- If the request is rejected by the service.IllegalArgumentException
- If the entity has null 'partitionKey' or 'rowKey'.
-
listEntities
Lists allentities
within the table.Code Samples
Lists all
entities
on the table. Prints out the details of the retrievedentities
.PagedIterable<TableEntity> tableEntities = tableClient.listEntities(); tableEntities.forEach(tableEntity -> System.out.printf("Retrieved entity with partition key '%s' and row key '%s'.%n", tableEntity.getPartitionKey(), tableEntity.getRowKey()));
- Returns:
- A
PagedIterable
containing allentities
within the table. - Throws:
TableServiceException
- If the request is rejected by the service.
-
listEntities
public com.azure.core.http.rest.PagedIterable<TableEntity> listEntities(ListEntitiesOptions options, Duration timeout, com.azure.core.util.Context context) Listsentities
using the parameters in the provided options.If the
filter
parameter in the options is set, onlyentities
matching the filter will be returned. If theselect
parameter is set, only the properties included in the select parameter will be returned for eachentity
. If thetop
parameter is set, the maximum number of returnedentities
per page will be limited to that value.Code Samples
Lists all
entities
on the table. Prints out the details of theHTTP response
and all the retrievedentities
.List<String> propertiesToSelect = new ArrayList<>(); propertiesToSelect.add("name"); propertiesToSelect.add("lastname"); propertiesToSelect.add("age"); ListEntitiesOptions listEntitiesOptions = new ListEntitiesOptions() .setTop(15) .setFilter("PartitionKey eq 'MyPartitionKey' and RowKey eq 'MyRowKey'") .setSelect(propertiesToSelect); PagedIterable<TableEntity> myTableEntities = tableClient.listEntities(listEntitiesOptions, Duration.ofSeconds(5), null); myTableEntities.forEach(tableEntity -> { System.out.printf("Retrieved entity with partition key '%s', row key '%s' and properties:%n", tableEntity.getPartitionKey(), tableEntity.getRowKey()); tableEntity.getProperties().forEach((key, value) -> System.out.printf("Name: '%s'. Value: '%s'.%n", key, value)); });
- Parameters:
options
- Thefilter
,select
, andtop
OData query options to apply to this operation.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- A
PagedIterable
containing matchingentities
within the table. - Throws:
IllegalArgumentException
- If one or more of the OData query options inoptions
is malformed.TableServiceException
- If the request is rejected by the service.
-
getEntity
Gets a singleentity
from the table.Code Samples
Gets an
entity
on the table. Prints out the details of the retrievedentity
.TableEntity tableEntity = tableClient.getEntity("partitionKey", "rowKey"); System.out.printf("Retrieved entity with partition key '%s' and row key '%s'.", tableEntity.getPartitionKey(), tableEntity.getRowKey());
- Parameters:
partitionKey
- The partition key of theentity
.rowKey
- The partition key of theentity
.- Returns:
- The
entity
. - Throws:
IllegalArgumentException
- If the providedpartitionKey
orrowKey
arenull
or empty.TableServiceException
- If noentity
with the providedpartitionKey
androwKey
exists within the table.
-
getEntityWithResponse
public com.azure.core.http.rest.Response<TableEntity> getEntityWithResponse(String partitionKey, String rowKey, List<String> select, Duration timeout, com.azure.core.util.Context context) Gets a singleentity
from the table.Code Samples
Gets an
entity
on the table. Prints out the details of theHTTP response
retrievedentity
.List<String> propertiesToSelect = new ArrayList<>(); propertiesToSelect.add("name"); propertiesToSelect.add("lastname"); propertiesToSelect.add("age"); Response<TableEntity> response = tableClient.getEntityWithResponse("partitionKey", "rowKey", propertiesToSelect, Duration.ofSeconds(5), new Context("key1", "value1")); TableEntity myTableEntity = response.getValue(); System.out.printf("Response successful with status code: %d. Retrieved entity with partition key '%s', row key" + " '%s' and properties:", response.getStatusCode(), myTableEntity.getPartitionKey(), myTableEntity.getRowKey()); myTableEntity.getProperties().forEach((key, value) -> System.out.printf("%nName: '%s'. Value: '%s'.", key, value));
- Parameters:
partitionKey
- The partition key of theentity
.rowKey
- The partition key of theentity
.select
- A list of properties to select on theentity
.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
containing theentity
. - Throws:
IllegalArgumentException
- If the providedpartitionKey
orrowKey
arenull
or if theselect
OData query option is malformed.TableServiceException
- If noentity
with the providedpartitionKey
androwKey
exists within the table.
-
getAccessPolicies
Retrieves details about any storedaccess policies
specified on the table that may be used with Shared Access Signatures.This operation is only supported on Azure Storage endpoints.
Code Samples
Gets a table's
access policies
. Prints out the details of the retrievedaccess policies
.TableAccessPolicies accessPolicies = tableClient.getAccessPolicies(); accessPolicies.getIdentifiers().forEach(signedIdentifier -> System.out.printf("Retrieved table access policy with id '%s'.", signedIdentifier.getId()));
- Returns:
- The table's
access policies
. - Throws:
TableServiceException
- If the request is rejected by the service.
-
getAccessPoliciesWithResponse
public com.azure.core.http.rest.Response<TableAccessPolicies> getAccessPoliciesWithResponse(Duration timeout, com.azure.core.util.Context context) Retrieves details about any storedaccess policies
specified on the table that may be used with Shared Access Signatures.This operation is only supported on Azure Storage endpoints.
Code Samples
Gets a table's
access policies
. Prints out the details of theHTTP response
and the retrievedaccess policies
.List<String> propertiesToSelect = new ArrayList<>(); propertiesToSelect.add("name"); propertiesToSelect.add("lastname"); propertiesToSelect.add("age"); Response<TableAccessPolicies> response = tableClient.getAccessPoliciesWithResponse(Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. Retrieved table access policies with the following" + " IDs:", response.getStatusCode()); response.getValue().getIdentifiers().forEach(signedIdentifier -> System.out.printf("%n%s", signedIdentifier.getId()));
- Parameters:
timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- An
HTTP response
containing the table'saccess policies
. - Throws:
TableServiceException
- If the request is rejected by the service.
-
setAccessPolicies
Sets storedaccess policies
for the table that may be used with Shared Access Signatures.This operation is only supported on Azure Storage endpoints.
Code Samples
Sets stored
access policies
on a table.List<TableSignedIdentifier> signedIdentifiers = new ArrayList<>(); signedIdentifiers.add(new TableSignedIdentifier("id1") .setAccessPolicy(new TableAccessPolicy() .setStartsOn(OffsetDateTime.of(2021, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)) .setExpiresOn(OffsetDateTime.of(2022, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)) .setPermissions("r"))); signedIdentifiers.add(new TableSignedIdentifier("id2") .setAccessPolicy(new TableAccessPolicy() .setStartsOn(OffsetDateTime.of(2021, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)) .setExpiresOn(OffsetDateTime.of(2021, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC)) .setPermissions("raud"))); tableClient.setAccessPolicies(signedIdentifiers); System.out.print("Set table access policies.");
- Parameters:
tableSignedIdentifiers
- Theaccess policies
for the table.- Throws:
TableServiceException
- If the request is rejected by the service.
-
setAccessPoliciesWithResponse
public com.azure.core.http.rest.Response<Void> setAccessPoliciesWithResponse(List<TableSignedIdentifier> tableSignedIdentifiers, Duration timeout, com.azure.core.util.Context context) Sets storedaccess policies
for the table that may be used with Shared Access Signatures.This operation is only supported on Azure Storage endpoints.
Code Samples
Sets stored
access policies
on a table. Prints out details of theHTTP response
.List<TableSignedIdentifier> mySignedIdentifiers = new ArrayList<>(); mySignedIdentifiers.add(new TableSignedIdentifier("id1") .setAccessPolicy(new TableAccessPolicy() .setStartsOn(OffsetDateTime.of(2021, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)) .setExpiresOn(OffsetDateTime.of(2022, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)) .setPermissions("r"))); mySignedIdentifiers.add(new TableSignedIdentifier("id2") .setAccessPolicy(new TableAccessPolicy() .setStartsOn(OffsetDateTime.of(2021, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)) .setExpiresOn(OffsetDateTime.of(2021, 1, 2, 0, 0, 0, 0, ZoneOffset.UTC)) .setPermissions("raud"))); Response<Void> response = tableClient.setAccessPoliciesWithResponse(mySignedIdentifiers, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Set table access policies successfully with status code: %d.", response.getStatusCode());
- Parameters:
tableSignedIdentifiers
- Theaccess policies
for the table.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- The
HTTP response
. - Throws:
TableServiceException
- If the request is rejected by the service.
-
submitTransaction
Executes allactions
within the list inside a transaction. When the call completes, either allactions
in the transaction will succeed, or if a failure occurs, allactions
in the transaction will be rolled back.Actions
are executed sequentially. Eachaction
must operate on a distinct row key. Attempting to pass multipleactions
that share the same row key will cause an error.Code Samples
Submits a transaction that contains multiple
actions
to be applied toentities
on a table. Prints out details of eachaction
'sHTTP response
.List<TableTransactionAction> transactionActions = new ArrayList<>(); String partitionKey = "markers"; String firstEntityRowKey = "m001"; String secondEntityRowKey = "m002"; TableEntity firstEntity = new TableEntity(partitionKey, firstEntityRowKey) .addProperty("Type", "Dry") .addProperty("Color", "Red"); transactionActions.add(new TableTransactionAction(TableTransactionActionType.CREATE, firstEntity)); System.out.printf("Added create action for entity with partition key '%s', and row key '%s'.%n", partitionKey, firstEntityRowKey); TableEntity secondEntity = new TableEntity(partitionKey, secondEntityRowKey) .addProperty("Type", "Wet") .addProperty("Color", "Blue"); transactionActions.add(new TableTransactionAction(TableTransactionActionType.CREATE, secondEntity)); System.out.printf("Added create action for entity with partition key '%s', and row key '%s'.%n", partitionKey, secondEntityRowKey); TableTransactionResult tableTransactionResult = tableClient.submitTransaction(transactionActions); System.out.print("Submitted transaction. The ordered response status codes for the actions are:"); tableTransactionResult.getTransactionActionResponses().forEach(tableTransactionActionResponse -> System.out.printf("%n%d", tableTransactionActionResponse.getStatusCode()));
Shows how to handle a transaction with a failing
action
via the providedexception
, which contains the index of the first failing action in the transaction.tableAsyncClient.submitTransaction(transactionActions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .doOnError(TableTransactionFailedException.class, e -> { // If the transaction fails, the resulting exception contains the index of the first action that failed. int failedActionIndex = e.getFailedTransactionActionIndex(); // You can use this index to modify the offending action or remove it from the list of actions to send // in the transaction, for example. transactionActions.remove(failedActionIndex); // And then retry submitting the transaction. }) .subscribe(tableTransactionResult -> { System.out.print("Submitted transaction. The ordered response status codes for the actions are:"); tableTransactionResult.getTransactionActionResponses().forEach(tableTransactionActionResponse -> System.out.printf("%n%d", tableTransactionActionResponse.getStatusCode())); });
- Parameters:
transactionActions
- AList
ofactions
to perform onentities
in a table.- Returns:
- A
List
ofsub-responses
that correspond to eachaction
in the transaction. - Throws:
IllegalArgumentException
- If noactions
have been added to the list.TableServiceException
- If the request is rejected by the service.TableTransactionFailedException
- If anyaction
within the transaction fails. See the documentation for the client methods inTableClient
to understand the conditions that may cause a givenaction
to fail.
-
submitTransactionWithResponse
public com.azure.core.http.rest.Response<TableTransactionResult> submitTransactionWithResponse(List<TableTransactionAction> transactionActions, Duration timeout, com.azure.core.util.Context context) Executes allactions
within the list inside a transaction. When the call completes, either allactions
in the transaction will succeed, or if a failure occurs, allactions
in the transaction will be rolled back.Actions
are executed sequentially. Eachaction
must operate on a distinct row key. Attempting to pass multipleactions
that share the same row key will cause an error.Code Samples
Submits a transaction that contains multiple
actions
to be applied toentities
on a table. Prints out details of theHTTP response
for the operation, as well as eachaction
's correspondingHTTP response
.List<TableTransactionAction> myTransactionActions = new ArrayList<>(); String myPartitionKey = "markers"; String myFirstEntityRowKey = "m001"; String mySecondEntityRowKey = "m002"; TableEntity myFirstEntity = new TableEntity(myPartitionKey, myFirstEntityRowKey) .addProperty("Type", "Dry") .addProperty("Color", "Red"); myTransactionActions.add(new TableTransactionAction(TableTransactionActionType.CREATE, myFirstEntity)); System.out.printf("Added create action for entity with partition key '%s', and row key '%s'.%n", myPartitionKey, myFirstEntityRowKey); TableEntity mySecondEntity = new TableEntity(myPartitionKey, mySecondEntityRowKey) .addProperty("Type", "Wet") .addProperty("Color", "Blue"); myTransactionActions.add(new TableTransactionAction(TableTransactionActionType.CREATE, mySecondEntity)); System.out.printf("Added create action for entity with partition key '%s', and row key '%s'.%n", myPartitionKey, mySecondEntityRowKey); Response<TableTransactionResult> response = tableClient.submitTransactionWithResponse(myTransactionActions, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. The ordered response status codes of the submitted" + " actions are:", response.getStatusCode()); response.getValue().getTransactionActionResponses().forEach(tableTransactionActionResponse -> System.out.printf("%n%d", tableTransactionActionResponse.getStatusCode()));
Shows how to handle a transaction with a failing
action
via the providedexception
, which contains the index of the first failing action in the transaction.try { Response<TableTransactionResult> transactionResultResponse = tableClient.submitTransactionWithResponse(myTransactionActions, Duration.ofSeconds(5), new Context("key1", "value1")); System.out.printf("Response successful with status code: %d. The ordered response status codes of the" + " submitted actions are:", transactionResultResponse.getStatusCode()); transactionResultResponse.getValue().getTransactionActionResponses() .forEach(tableTransactionActionResponse -> System.out.printf("%n%d", tableTransactionActionResponse.getStatusCode())); } catch (TableTransactionFailedException e) { // If the transaction fails, the resulting exception contains the index of the first action that failed. int failedActionIndex = e.getFailedTransactionActionIndex(); // You can use this index to modify the offending action or remove it from the list of actions to send in // the transaction, for example. myTransactionActions.remove(failedActionIndex); // And then retry submitting the transaction. }
- Parameters:
transactionActions
- AList
oftransaction actions
to perform onentities
in a table.timeout
- An optional timeout value beyond which aRuntimeException
will be raised.context
- AdditionalContext
that is passed through theHTTP pipeline
during the service call.- Returns:
- An
HTTP response
produced for the transaction itself. The response's value will contain aList
ofsub-responses
that correspond to eachaction
in the transaction. - Throws:
IllegalArgumentException
- If noactions
have been added to the list.TableServiceException
- If the request is rejected by the service.TableTransactionFailedException
- If anyaction
within the transaction fails. See the documentation for the client methods inTableClient
to understand the conditions that may cause a givenaction
to fail.
-