Compartir a través de


TableServiceClient Class

  • java.lang.Object
    • com.azure.data.tables.TableServiceClient

public final class TableServiceClient

Provides a synchronous service client for accessing the Azure Tables service.

Overview

The client encapsulates the URL for the Tables service endpoint and the credentials for accessing the storage or CosmosDB table API account. It provides methods to create, delete, and list tables within the account. These methods invoke REST API operations to make the requests and obtain the results that are returned.

Getting Started

The building and authenticating of instances of this client are handled by TableServiceClientBuilder instances. The following sample shows how to authenticate and build a TableServiceClient using a connection string.

TableServiceClient tableServiceClient = new TableServiceClientBuilder()
     .connectionString("connectionstring")
     .buildClient();

See TableServiceClientBuilder documentation for more information on constructing and authenticating a client.

The following samples show the various ways you can interact with the tables service using this client.


Create a Table

The createTable(String tableName) method can be used to create a new table within an Azure Storage or Azure Cosmos account. It returns a TableClient for the newly created table.

The following sample creates a table with the name "myTable".

TableClient tableClient = tableServiceClient.createTable("myTable");

 System.out.printf("Table with name '%s' was created.", tableClient.getTableName());

Note: for asynchronous sample, refer to TableServiceAsyncClient.


Delete a Table

The deleteTable(String tableName) method can be used to delete a table within an Azure Storage or Azure Cosmos account.

The following sample deletes the table with the name "myTable".

tableServiceClient.deleteTable("myTable");

 System.out.printf("Table with name '%s' was deleted.", "myTable");

Note: for asynchronous sample, refer to TableServiceAsyncClient.


Get a TableClient

The getTableClient(String tableName) method can be used to retrieve a TableClient for a table within an Azure Storage or Azure Cosmos account.

The following sample gets a TableClient for the table with the name "myTable".

TableClient tableClient = tableServiceClient.getTableClient("myTable");

 System.out.printf("Table with name '%s' was retrieved.", tableClient.getTableName());

Note: for asynchronous sample, refer to TableServiceAsyncClient.


List Tables

The listTables() method can be used to list all the tables in an Azure Storage or Azure Cosmos account.

The following samples lists the tables in the Tables service account.

Without filtering, returning all tables:

PagedIterable<TableItem> tableItems = tableServiceClient.listTables();

 tableItems.forEach(tableItem ->
     System.out.printf("Retrieved table with name '%s'.%n", tableItem.getName()));

With filtering:

ListTablesOptions options = new ListTablesOptions().setFilter("TableName eq 'myTable'");

 PagedIterable<TableItem> retrievedTableItems = tableServiceClient.listTables(options, Duration.ofSeconds(5),
     new Context("key1", "value1"));

 retrievedTableItems.forEach(tableItem ->
     System.out.printf("Retrieved table with name '%s'.%n", tableItem.getName()));

Note: for asynchronous sample, refer to TableServiceAsyncClient.


Get Table Properties

The getProperties() method can be used to get the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. This operation is only supported on Azure Storage endpoints.

The following sample gets the properties of the Tables service account.

TableServiceProperties properties = tableServiceClient.getProperties();

 System.out.print("Retrieved service properties successfully.");

Note: for asynchronous sample, refer to TableServiceAsyncClient.


Set Table Properties

The setProperties(TableServiceProperties tableServiceProperties) method can be used to set the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. This operation is only supported on Azure Storage endpoints.

The following sample sets the properties of the Tables service account.

TableServiceProperties properties = new TableServiceProperties()
     .setHourMetrics(new TableServiceMetrics()
         .setVersion("1.0")
         .setEnabled(true)
         .setIncludeApis(true)
         .setRetentionPolicy(new TableServiceRetentionPolicy()
             .setEnabled(true)
             .setDaysToRetain(5)))
     .setLogging(new TableServiceLogging()
         .setAnalyticsVersion("1.0")
         .setReadLogged(true)
         .setRetentionPolicy(new TableServiceRetentionPolicy()
             .setEnabled(true)
             .setDaysToRetain(5)));

 tableServiceClient.setProperties(properties);

 System.out.printf("Set service properties successfully.");

Note: for asynchronous sample, refer to TableServiceAsyncClient.


Get Table Statistics

The getStatistics() method can be used to retrieve statistics related to replication for the account's Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account. This operation is only supported on Azure Storage endpoints.

The following sample gets the statistics of the Tables service account.

TableServiceStatistics statistics = tableServiceClient.getStatistics();

 System.out.print("Retrieved service statistics successfully.");

Note: for asynchronous sample, refer to TableServiceAsyncClient.

Method Summary

Modifier and Type Method and Description
TableClient createTable(String tableName)

Creates a table within the Tables service.

TableClient createTableIfNotExists(String tableName)

Creates a table within the Tables service if the table does not already exist.

Response<TableClient> createTableIfNotExistsWithResponse(String tableName, Duration timeout, Context context)

Creates a table within the Tables service if the table does not already exist.

Response<TableClient> createTableWithResponse(String tableName, Duration timeout, Context context)

Creates a table within the Tables service.

void deleteTable(String tableName)

Deletes a table within the Tables service.

Response<Void> deleteTableWithResponse(String tableName, Duration timeout, Context context)

Deletes a table within the Tables service.

String generateAccountSas(TableAccountSasSignatureValues tableAccountSasSignatureValues)

Generates an account SAS for the Azure Storage account using the specified TableAccountSasSignatureValues.

String getAccountName()

Gets the name of the account containing the table.

TableServiceProperties getProperties()

Gets the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

Response<TableServiceProperties> getPropertiesWithResponse(Duration timeout, Context context)

Gets the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

String getServiceEndpoint()

Gets the endpoint for the Tables service.

TableServiceVersion getServiceVersion()

Gets the REST API version used by this client.

TableServiceStatistics getStatistics()

Retrieves statistics related to replication for the account's Table service.

Response<TableServiceStatistics> getStatisticsWithResponse(Duration timeout, Context context)

Retrieves statistics related to replication for the account's Table service.

TableClient getTableClient(String tableName)

Gets a TableClient instance for the table in the account with the provided tableName.

PagedIterable<TableItem> listTables()

Lists all tables within the account.

PagedIterable<TableItem> listTables(ListTablesOptions options, Duration timeout, Context context)

If the filter parameter in the options is set, only tables matching the filter will be returned.

void setProperties(TableServiceProperties tableServiceProperties)

Sets the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

Response<Void> setPropertiesWithResponse(TableServiceProperties tableServiceProperties, Duration timeout, Context context)

Sets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

Methods inherited from java.lang.Object

Method Details

createTable

public TableClient createTable(String tableName)

Creates a table within the Tables service.

Code Samples

Creates a table. Prints out the details of the created table.

TableClient tableClient = tableServiceClient.createTable("myTable");

 System.out.printf("Table with name '%s' was created.", tableClient.getTableName());

Parameters:

tableName - The name of the table to create.

Returns:

A TableClient for the created table.

createTableIfNotExists

public TableClient createTableIfNotExists(String tableName)

Creates a table within the Tables service if the table does not already exist. If the table already exists, a TableClient for the existing table is returned.

Code Samples

Creates a table if it does not already exist. Prints out the details of the created table.

TableClient tableClient = tableServiceClient.createTableIfNotExists("myTable");

 System.out.printf("Table with name '%s' was created.", tableClient.getTableName());

Parameters:

tableName - The name of the table to create.

Returns:

A TableClient for the created table.

createTableIfNotExistsWithResponse

public Response createTableIfNotExistsWithResponse(String tableName, Duration timeout, Context context)

Creates a table within the Tables service if the table does not already exist. If the table already exists, a TableClient for the existing table is returned.

Code Samples

Creates a table if it does not already exist. Prints out the details of the Response<T> and the created table.

Response<TableClient> response =
     tableServiceClient.createTableIfNotExistsWithResponse("myTable", 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().getTableName());

Parameters:

tableName - The name of the table to create.
timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

The Response<T> containing a TableClient for the created table.

createTableWithResponse

public Response createTableWithResponse(String tableName, Duration timeout, Context context)

Creates a table within the Tables service.

Code Samples

Creates a table. Prints out the details of the Response<T> and the created table.

Response<TableClient> response = tableServiceClient.createTableWithResponse("myTable", 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().getTableName());

Parameters:

tableName - The name of the table to create.
timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

The Response<T> containing a TableClient for the created table.

deleteTable

public void deleteTable(String tableName)

Deletes a table within the Tables service.

Code Samples

Deletes a table.

tableServiceClient.deleteTable("myTable");

 System.out.printf("Table with name '%s' was deleted.", "myTable");

Parameters:

tableName - The name of the table to delete.

deleteTableWithResponse

public Response deleteTableWithResponse(String tableName, Duration timeout, Context context)

Deletes a table within the Tables service.

Code Samples

Deletes a table. Prints out the details of the Response<T>.

Response<Void> response = tableServiceClient.deleteTableWithResponse("myTable", Duration.ofSeconds(5),
     new Context("key1", "value1"));

 System.out.printf("Response successful with status code: %d. Table with name '%s' was deleted.",
     response.getStatusCode(), "myTable");

Parameters:

tableName - The name of the table to delete.
timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

generateAccountSas

public String generateAccountSas(TableAccountSasSignatureValues tableAccountSasSignatureValues)

Generates an account SAS for the Azure Storage account using the specified TableAccountSasSignatureValues.

Note: The client must be authenticated via AzureNamedKeyCredential.

See TableAccountSasSignatureValues for more information on how to construct an account SAS.

Parameters:

tableAccountSasSignatureValues - TableAccountSasSignatureValues.

Returns:

A String representing the SAS query parameters.

getAccountName

public String getAccountName()

Gets the name of the account containing the table.

Returns:

The name of the account containing the table.

getProperties

public TableServiceProperties getProperties()

Gets the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

This operation is only supported on Azure Storage endpoints.

Code Samples

Gets the properties of the account's Table service.

TableServiceProperties properties = tableServiceClient.getProperties();

 System.out.print("Retrieved service properties successfully.");

Returns:

The TableServiceProperties of the account's Table service.

getPropertiesWithResponse

public Response getPropertiesWithResponse(Duration timeout, Context context)

Gets the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

This operation is only supported on Azure Storage endpoints.

Code Samples

Gets the properties of the account's Table service. Prints out the details of the Response<T>.

Response<TableServiceProperties> response =
     tableServiceClient.getPropertiesWithResponse(Duration.ofSeconds(5), new Context("key1", "value1"));

 System.out.printf("Retrieved service properties successfully with status code: %d.", response.getStatusCode());

Parameters:

timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

The Response<T> and the TableServiceProperties of the account's Table service.

getServiceEndpoint

public String getServiceEndpoint()

Gets the endpoint for the Tables service.

Returns:

The endpoint for the Tables service.

getServiceVersion

public TableServiceVersion getServiceVersion()

Gets the REST API version used by this client.

Returns:

The REST API version used by this client.

getStatistics

public TableServiceStatistics getStatistics()

Retrieves statistics related to replication for the account's Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account.

This operation is only supported on Azure Storage endpoints.

Code Samples

Gets the replication statistics of the account's Table service.

TableServiceStatistics statistics = tableServiceClient.getStatistics();

 System.out.print("Retrieved service statistics successfully.");

Returns:

TableServiceStatistics for the account's Table service.

getStatisticsWithResponse

public Response getStatisticsWithResponse(Duration timeout, Context context)

Retrieves statistics related to replication for the account's Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account.

This operation is only supported on Azure Storage endpoints.

Code Samples

Gets the replication statistics of the account's Table service. Prints out the details of the Response<T>.

Response<TableServiceStatistics> response = tableServiceClient.getStatisticsWithResponse(Duration.ofSeconds(5),
     new Context("key1", "value1"));

 System.out.printf("Retrieved service statistics successfully with status code: %d.",
     response.getStatusCode());

Parameters:

timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

An Response<T> containing TableServiceStatistics for the account's Table service.

getTableClient

public TableClient getTableClient(String tableName)

Gets a TableClient instance for the table in the account with the provided tableName. The resulting TableClient will use the same HttpPipeline and TableServiceVersion as this TableServiceClient.

Parameters:

tableName - The name of the table.

Returns:

A TableClient instance for the table in the account with the provided tableName.

listTables

public PagedIterable listTables()

Lists all tables within the account.

Code Samples

Lists all tables. Prints out the details of the retrieved tables.

PagedIterable<TableItem> tableItems = tableServiceClient.listTables();

 tableItems.forEach(tableItem ->
     System.out.printf("Retrieved table with name '%s'.%n", tableItem.getName()));

Returns:

A PagedIterable<T> containing all tables within the account.

listTables

public PagedIterable listTables(ListTablesOptions options, Duration timeout, Context context)

If the filter parameter in the options is set, only tables matching the filter will be returned. If the top parameter is set, the maximum number of returned tables per page will be limited to that value.

Code Samples

Lists all tables that match the filter. Prints out the details of the retrieved tables.

ListTablesOptions options = new ListTablesOptions().setFilter("TableName eq 'myTable'");

 PagedIterable<TableItem> retrievedTableItems = tableServiceClient.listTables(options, Duration.ofSeconds(5),
     new Context("key1", "value1"));

 retrievedTableItems.forEach(tableItem ->
     System.out.printf("Retrieved table with name '%s'.%n", tableItem.getName()));

Parameters:

options - The filter and top OData query options to apply to this operation.
timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

A PagedIterable<T> containing matching tables within the account.

setProperties

public void setProperties(TableServiceProperties tableServiceProperties)

Sets the properties of the account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

This operation is only supported on Azure Storage endpoints.

Code Samples

Sets the properties of the account's Table service.

TableServiceProperties properties = new TableServiceProperties()
     .setHourMetrics(new TableServiceMetrics()
         .setVersion("1.0")
         .setEnabled(true)
         .setIncludeApis(true)
         .setRetentionPolicy(new TableServiceRetentionPolicy()
             .setEnabled(true)
             .setDaysToRetain(5)))
     .setLogging(new TableServiceLogging()
         .setAnalyticsVersion("1.0")
         .setReadLogged(true)
         .setRetentionPolicy(new TableServiceRetentionPolicy()
             .setEnabled(true)
             .setDaysToRetain(5)));

 tableServiceClient.setProperties(properties);

 System.out.printf("Set service properties successfully.");

Parameters:

tableServiceProperties - The TableServiceProperties to set.

setPropertiesWithResponse

public Response setPropertiesWithResponse(TableServiceProperties tableServiceProperties, Duration timeout, Context context)

Sets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.

This operation is only supported on Azure Storage endpoints.

Code Samples

Sets the properties of the account's Table service. Prints out the details of the Response<T>.

TableServiceProperties myProperties = new TableServiceProperties()
     .setHourMetrics(new TableServiceMetrics()
         .setVersion("1.0")
         .setEnabled(true)
         .setIncludeApis(true)
         .setRetentionPolicy(new TableServiceRetentionPolicy()
             .setEnabled(true)
             .setDaysToRetain(5)))
     .setLogging(new TableServiceLogging()
         .setAnalyticsVersion("1.0")
         .setReadLogged(true)
         .setRetentionPolicy(new TableServiceRetentionPolicy()
             .setEnabled(true)
             .setDaysToRetain(5)));

 Response<Void> response = tableServiceClient.setPropertiesWithResponse(myProperties, Duration.ofSeconds(5),
     new Context("key1", "value1"));

 System.out.printf("Retrieved service properties successfully with status code: %d.", response.getStatusCode());

Parameters:

tableServiceProperties - The TableServiceProperties to set.
timeout - An optional timeout value beyond which a RuntimeException will be raised.
context - Additional Context that is passed through the HttpPipeline during the service call.

Returns:

Applies to