Partilhar via


com.azure.data.tables

Azure Tables is a NoSQL key-value storage service offered by Microsoft Azure, which provides a highly scalable and cost-effective solution for storing structured data. It is a fully managed service that is designed to handle large volumes of structured data in a distributed environment, with low latency and high availability. Azure Tables stores data in tables, which are schema-less and can contain any type of data. Each table can have a partition key and a row key, which together form a unique primary key that can be used to retrieve individual records. This enables fast, efficient querying of data, especially when combined with Azure's indexing and query features.

The Azure Tables client library enables Java developers to easily interact with Azure Tables Storage Service from their Java applications. This library provides a set of APIs that abstract the low-level details of working with the Azure Tables Storage Service and allows developers to perform common operations such as creating tables, inserting, updating and deleting entities, querying data, and managing access control on tables and entities. The library supports both Azure Storage and Azure Cosmos tables. It offers both synchronous and asynchronous programming models. It also provides features such as retries, automatic pagination, and parallelism to enable efficient and reliable interactions with Azure Tables Storage Service.

Getting Started

Prerequisites

The client library package requires the following:

  • Java 8 or later
  • An Azure subscription
  • An existing Azure Storage or Azure Cosmos account

Authenticate a Client

In order to build a valid table client or table service client, you will need to authenticate the client using an accepted method of authentication. The supported forms of authentication are:

See client builder class documentation TableServiceClientBuilder and TableClientBuilder for examples of authenticating a client.

For more information on authentication types, see the identity documentation.

Table service clients utilize their authentication information to create table clients. Table clients created via a table service client will inherit the authentication information of the table service client.


Overview

The TableServiceClient and TableServiceAsyncClient provide access to the tables within an Azure Storage or Azure Cosmos account. A table service client can create, list, and delete tables. It also provides access to a table client that can be used to perform CRUD operations on entities within a table. You can instantiate a table service client using an instance of TableServiceClientBuilder.

The TableClient and TableAsyncClient provide access to a specific table within an Azure Storage or Azure Cosmos account. A table client can be used to perform CRUD and query operations on entities within a table. Table clients can also create* new tables and delete the table they reference from the Azure Storage or Cosmos acount. An instance of a table client can be returned via a table service client or can be instantiated using an instance of TableClientBuilder.

* Tables created from a table client do not return a new TableClient instance. Table client instances cannot change the table they reference. To reference the newly created table, a new table client instance must be instantiated referencing the table.

See methods in client level class below to explore all capabilities the library provides.


Table Service Client Usage

Create a TableServiceClient using a TableServiceClientBuilder

The TableServiceClient and TableServiceAsyncClient both provide access to the tables within an Azure Storage or Azure Cosmos account. A table service client can create, list, and delete tables. It also provides access to a table client that can be used to perform CRUD operations on entities within a table. You can instantiate a table service client using an instance of TableServiceClientBuilder.

Here's an example of creating a synchronous table service client using the buildClient() TableServiceClientBuilder buildClient} method:

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

Note: To create an asynchronous table service client, call buildAsyncClient() instead of buildClient().


Create a Table using TableServiceClient

The createTable(String tableName) method can be used to create an new table within your Azure Storage or Azure Cosmos account.

The following example creates a table with the name "tableName".

tableServiceClient.createTable("tableName");

Note: For asynchronous sample, refer to TableServiceAsyncClient


List Tables using TableServiceClient

The listTables() method can be used to list the tables that are within an Azure Storage or Azure Cosmos account.

The following example lists all the tables in the account without any filtering of Tables.

tableServiceClient.listTables().forEach(table -> {
     String tableName = table.getName();
     System.out.println("Table name: " + tableName);
 });

Note: For asynchronous sample, refer to TableServiceAsyncClient


Delete a Table using TableServiceClient

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

The following example deletes a table with the name "tableName".

tableServiceClient.deleteTable("tableName");

Note: For asynchronous sample, refer to TableServiceAsyncClient


Table Client Usage

The TableClient and TableAsyncClient provide access to a specific table within an Azure Storage or Azure Cosmos account. A table client can be used to perform CRUD and query operations on entities within a table. Table clients can also create* new tables and delete the table they reference from the Azure Storage or Cosmos acount. An instance of a table client can be returned via a table service client or can be instantiated using an instance of TableClientBuilder.

Retrieve a TableClient from a TableServiceClient

The getTableClient(String tableName) TableServiceClient getTableClient} method can be used to retrieve a TableClient for a specified table that is stored within your Azure Storage or Azure Cosmos account.

The following example returns a table client for a table with the name "tableName".

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

Note: For asynchronous sample, refer to TableServiceAsyncClient


Create a TableClient using a TableClientBuilder

The TableClient and TableAsyncClient provide access to a specific table within an Azure Storage or Azure Cosmos account. The buildClient() method can be used to create a TableClient

Here's an example of creating a TableClient using a builder:

TableClient tableClient = new TableClientBuilder()
     .connectionString("connectionstring")
     .tableName("tableName")
     .buildClient();

Note: To create an TableAsyncClient, call buildAsyncClient() instead of buildClient().


Create an Entity using TableClient

The createEntity(TableEntity entity) method can be used to create a table entity within a table in your Azure Storage or Azure Cosmos account.

The following example creates an entity with a partition key of "partitionKey" and a row key of "rowKey".

tableClient.createEntity(new TableEntity("partitionKey", "rowKey")
     .addProperty("property", "value"));

Note: for asynchronous sample, refer to TableAsyncClient


Update an Entity using TableClient

The updateEntity(TableEntity entity) method can be used to update a table entity within a table in your Azure Storage or Azure Cosmos account.

The following example updates an entity with a partition key of "partitionKey" and a row key of "rowKey", adding an additional property with the name "newProperty" and the value "newValue".

TableEntity entity = tableClient.getEntity("partitionKey", "rowKey");
 entity.addProperty("newProperty", "newValue");
 tableClient.updateEntity(entity);

Note: for asynchronous sample, refer to TableAsyncClient


List Entities

The listEntities() method can be used to list the entities that are within a table in your Azure Storage or Azure Cosmos account.

The following example lists all entities in a table without any filtering.

tableClient.listEntities().forEach(entity -> {
     String partitionKey = entity.getPartitionKey();
     String rowKey = entity.getRowKey();
     System.out.println("Partition key: " + partitionKey + ", Row key: " + rowKey);
 });

Note: for asynchronous sample, refer to TableAsyncClient


Delete an Entity

The deleteEntity(TableEntity entity) method can be used to delete an entity that is within a table in your Azure Storage or Azure Cosmos account.

The following example deletes an entity with a partition key of "partitionKey" and a row key of "rowKey".

tableClient.deleteEntity("partitionKey", "rowKey");

Note: for asynchronous sample, refer to TableAsyncClient

Classes

TableAsyncClient

Provides an asynchronous service client for accessing a table in the Azure Tables service.

TableAzureNamedKeyCredentialPolicy

Policy that adds the SharedKey into the request's Authorization header.

TableClient

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

TableClientBuilder

Provides a fluent builder API to help aid the configuration and instantiation of a TableClient and TableAsyncClient.

TableScrubEtagPolicy

Wraps any potential error responses from the service and applies post processing of the response's eTag header to standardize the value.

TableServiceAsyncClient

Provides an asynchronous service client for accessing the Azure Tables service.

TableServiceClient

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

TableServiceClientBuilder

Provides a fluent builder API to help aid the configuration and instantiation of TableServiceClient and TableServiceAsyncClient.

Enums

TableServiceVersion

The versions of Tables REST API supported by this client library.