关于 azure-data-tables-java
面向使用 Azure Tables SDK 构建表存储应用的 Java 开发者技能,涵盖 Azure Table Storage 和 Cosmos DB Table API 的表与实体操作。
主要功能
- 01创建、列出和删除表
- 02创建和读取实体
- 03合并、替换和插入或更新实体
- 04使用 OData 筛选实体
- 05执行同分区事务
适用场景
- 01构建表存储应用
- 02通过实体 CRUD 管理记录
- 03按分区和属性筛选实体
- 04原子更新多个实体
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-data-tables-java安装后在 Claude Code 中输入/azure-data-tables-java确认已加载。
详细介绍
Java 版 Azure Tables SDK
使用 Java 版 Azure Tables SDK 构建表存储应用。适用于 Azure Table Storage 和 Cosmos DB Table API。
安装
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-data-tables</artifactId>
<version>12.6.0-beta.1</version>
</dependency>
创建客户端
使用连接字符串
import com.azure.data.tables.TableServiceClient;
import com.azure.data.tables.TableServiceClientBuilder;
import com.azure.data.tables.TableClient;
TableServiceClient serviceClient = new TableServiceClientBuilder()
.connectionString("<your-connection-string>")
.buildClient();
使用共享密钥
import com.azure.core.credential.AzureNamedKeyCredential;
AzureNamedKeyCredential credential = new AzureNamedKeyCredential(
"<account-name>",
"<account-key>");
TableServiceClient serviceClient = new TableServiceClientBuilder()
.endpoint("<your-table-account-url>")
.credential(credential)
.buildClient();
使用 SAS 令牌
TableServiceClient serviceClient = new TableServiceClientBuilder()
.endpoint("<your-table-account-url>")
.sasToken("<sas-token>")
.buildClient();
使用 DefaultAzureCredential(仅限 Storage)
import com.azure.identity.DefaultAzureCredentialBuilder;
TableServiceClient serviceClient = new TableServiceClientBuilder()
.endpoint("<your-table-account-url>")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
核心概念
- TableServiceClient:管理表(创建、列出、删除)
- TableClient:管理表中的实体(CRUD)
- Partition Key:将实体分组,以便高效查询
- Row Key:分区内的唯一标识符
- Entity:包含最多 252 个属性的行(Storage 为 1 MB,Cosmos 为 2 MB)
核心模式
示例使用占位凭据。请选择兼容的正式发布版 SDK;上方的 beta 版本仅供历史参考。请将连接字符串和 SAS 保存在密钥存储中。执行写入前,请确认账户、表和删除范围。合并、替换和 Upsert 示例未演示 ETag 并发检查;存在并发写入时,请使用条件更新。
创建表
// Create table (throws if exists)
TableClient tableClient = serviceClient.createTable("mytable");
// Create if not exists (no exception)
TableClient tableClient = serviceClient.createTableIfNotExists("mytable");
获取表客户端
// From service client
TableClient tableClient = serviceClient.getTableClient("mytable");
// Direct construction
TableClient tableClient = new TableClientBuilder()
.connectionString("<connection-string>")
.tableName("mytable")
.buildClient();
创建实体
import com.azure.data.tables.models.TableEntity;
TableEntity entity = new TableEntity("partitionKey", "rowKey")
.addProperty("Name", "Product A")
.addProperty("Price", 29.99)
.addProperty("Quantity", 100)
.addProperty("IsAvailable", true);
tableClient.createEntity(entity);
获取实体
TableEntity entity = tableClient.getEntity("partitionKey", "rowKey");
String name = (String) entity.getProperty("Name");
Double price = (Double) entity.getProperty("Price");
System.out.printf("Product: %s, Price: %.2f%n", name, price);
更新实体
import com.azure.data.tables.models.TableEntityUpdateMode;
// Merge (update only specified properties)
TableEntity updateEntity = new TableEntity("partitionKey", "rowKey")
.addProperty("Price", 24.99);
tableClient.updateEntity(updateEntity, TableEntityUpdateMode.MERGE);
// Replace (replace entire entity)
TableEntity replaceEntity = new TableEntity("partitionKey", "rowKey")
.addProperty("Name", "Product A Updated")
.addProperty("Price", 24.99)
.addProperty("Quantity", 150);
tableClient.updateEntity(replaceEntity, TableEntityUpdateMode.REPLACE);
Upsert 实体
// Insert or update (merge mode)
tableClient.upsertEntity(entity, TableEntityUpdateMode.MERGE);
// Insert or replace
tableClient.upsertEntity(entity, TableEntityUpdateMode.REPLACE);
删除实体
tableClient.deleteEntity("partitionKey", "rowKey");
列出实体
import com.azure.data.tables.models.ListEntitiesOptions;
// List all entities
for (TableEntity entity : tableClient.listEntities()) {
System.out.printf("%s - %s%n",
entity.getPartitionKey(),
entity.getRowKey());
}
// With filtering and selection
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter("PartitionKey eq 'sales'")
.setSelect(java.util.Arrays.asList("Name", "Price"));
for (TableEntity entity : tableClient.listEntities(options, null, null)) {
System.out.printf("%s: %.2f%n",
entity.getProperty("Name"),
entity.getProperty("Price"));
}
使用 OData 筛选器查询
// Filter by partition key
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter("PartitionKey eq 'electronics'");
// Filter with multiple conditions
options.setFilter("PartitionKey eq 'electronics' and Price gt 100");
// Filter with comparison operators
options.setFilter("Quantity ge 10 and Quantity le 100");
// Request page size; stop iteration explicitly if only ten total results are wanted
options.setTop(10);
for (TableEntity entity : tableClient.listEntities(options, null, null)) {
System.out.println(entity.getRowKey());
}
批处理操作(事务)
import com.azure.data.tables.models.TableTransactionAction;
import com.azure.data.tables.models.TableTransactionActionType;
import java.util.Arrays;
// All entities must have same partition key
List<TableTransactionAction> actions = Arrays.asList(
new TableTransactionAction(
TableTransactionActionType.CREATE,
new TableEntity("batch", "row1").addProperty("Name", "Item 1")),
new TableTransactionAction(
TableTransactionActionType.CREATE,
new TableEntity("batch", "row2").addProperty("Name", "Item 2")),
new TableTransactionAction(
TableTransactionActionType.UPSERT_MERGE,
new TableEntity("batch", "row3").addProperty("Name", "Item 3"))
);
tableClient.submitTransaction(actions);
列出表
import com.azure.data.tables.models.TableItem;
import com.azure.data.tables.models.ListTablesOptions;
// List all tables
for (TableItem table : serviceClient.listTables()) {
System.out.println(table.getName());
}
// Filter tables
ListTablesOptions options = new ListTablesOptions()
.setFilter("TableName eq 'mytable'");
for (TableItem table : serviceClient.listTables(options, null, null)) {
System.out.println(table.getName());
}
删除表
serviceClient.deleteTable("mytable");
强类型实体
TableEntity 是具体的 SDK 类,并非供 POJO 实现的接口。请显式映射领域值:
TableEntity product = new TableEntity("electronics", "laptop-001")
.addProperty("Name", "Laptop")
.addProperty("Price", 999.99);
tableClient.createEntity(product);
错误处理
import com.azure.data.tables.models.TableServiceException;
try {
tableClient.createEntity(entity);
} catch (TableServiceException e) {
System.out.println("Status: " + e.getResponse().getStatusCode());
System.out.println("Error: " + e.getMessage());
// 409 = Conflict (entity exists)
// 404 = Not Found
}
环境变量
# Storage Account
AZURE_TABLES_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=...
AZURE_TABLES_ENDPOINT=https://<account>.table.core.windows.net
# Cosmos DB Table API
COSMOS_TABLE_ENDPOINT=https://<account>.table.cosmosdb.azure.com
最佳实践
- 分区键设计:选择能够均匀分配负载的键
- 批处理操作:使用事务原子性地更新多个实体
- 查询优化:尽可能始终按 PartitionKey 筛选
- 选择投影:仅选择所需属性以提升性能
- 实体大小:将实体控制在 1 MB(Storage)或 2 MB(Cosmos)以下
触发短语
- “Azure Tables Java”
- “表存储 SDK”
- “Cosmos DB Table API”
- “NoSQL 键值存储”
- “分区键 行键”
- “表实体 CRUD”
适用场景
当任务与概述中描述的工作流或操作相符时,可使用此技能。
限制
- 仅当任务明确符合上述范围时,才使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。
评论
支持 Markdown