在本快速入門中,您會使用適用於 Java 的 Azure SDK 來部署適用於資料表的基本 Azure Cosmos DB 應用程式。 Azure Cosmos DB for Table 是無架構的數據存放區,可讓應用程式將結構化數據表數據儲存在雲端中。 您將瞭解如何使用適用於 Java 的 Azure SDK,在 Azure Cosmos DB 資源內建立資料表、數據列及執行基本工作。
API 參考文件 | 程式庫原始程式碼 | 封裝 (Maven) | Azure Developer CLI
必要條件
- Azure Developer CLI
- Docker 桌面
- 爪哇21
如果您沒有 Azure 帳戶,請在您開始之前先建立 免費帳戶。
初始化專案
使用 Azure 開發人員 CLI (azd) 建立適用於資料表帳戶的 Azure Cosmos DB,並部署容器化範例應用程式。 應用程式範例使用用戶端程式庫管理、建立、讀取和查詢樣本資料。
在空的目錄中開啟終端機。
如果您尚未通過驗證,請使用
azd auth login向 Azure 開發人員 CLI 進行驗證。 依照工具指定的步驟,使用您慣用的 Azure 認證向 CLI 進行驗證。azd auth login使用
azd init來初始化專案。azd init --template cosmos-db-table-java-quickstart在初始化期間,請設定唯一的環境名稱。
使用
azd up部署 Azure Cosmos DB 帳戶。 Bicep 範本也會部署範例 Web 應用程式。azd up在布建程式期間,選取您的訂用帳戶、所需的位置和目標資源群組。 等候佈建程序完成。 此流程「大約需要五分鐘」的時間。
Azure 資源佈建完成後,輸出將包含正在執行的 Web 應用程式的 URL。
Deploying services (azd deploy) (✓) Done: Deploying service web - Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io> SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.請使用主控台中的 URL,以在瀏覽器中導覽至您的 Web 應用程式。 觀察執行中應用程式的輸出。
安裝用戶端程式庫
用戶端程式庫可透過 Maven 以 azure-data-tables 封裝形式提供。
瀏覽到
/src/web資料夾,然後開啟 pom.xml 檔案。cd ./src如果尚未存在,請為
azure-data-tables封裝新增一個項目。<dependency> <groupId>com.azure</groupId> <artifactId>azure-data-tables</artifactId> </dependency>
匯入程式庫
將所有必要的命名空間匯入您的應用程式程序代碼。
import com.azure.core.http.rest.PagedFlux;
import com.azure.data.tables.TableAsyncClient;
import com.azure.data.tables.TableClientBuilder;
import com.azure.data.tables.models.ListEntitiesOptions;
import com.azure.data.tables.models.TableEntity;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
物件模型
| 名稱 | 描述 |
|---|---|
TableServiceAsyncClient |
此類型是主要客戶端類型,可用來管理全帳戶元數據或資料庫。 |
TableAsyncClient |
此類型代表帳戶內數據表的用戶端。 |
程式碼範例
範本中的範例程式代碼會使用名為 的 cosmicworks-products數據表。 數據表 cosmicworks-products 包含詳細數據,例如名稱、類別、數量、價格、唯一標識碼,以及每個產品的銷售旗標。 容器會使用唯一 標識符* 作為數據列索引鍵,而 類別目錄 則作為分割區索引鍵。
驗證用戶端
此範例會建立 類別的新實例 TableServiceAsyncClient 。
DefaultAzureCredential azureTokenCredential = new DefaultAzureCredentialBuilder()
.build();
TableServiceAsyncClient client = new TableServiceClientBuilder()
.endpoint("<azure-cosmos-db-table-account-endpoint>")
.credential(credential)
.buildAsyncClient();
取得資料表
此範例會使用 TableAsyncClient 類別的 GetTableClient 方法,建立 TableServiceClient 類別的實例。
TableAsyncClient table = client
.getTableClient("<azure-cosmos-db-table-name>");
建立實體
在資料表中建立新實體最簡單的方式是使用 createEntity。
String rowKey = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb";
String partitionKey = "gear-surf-surfboards";
TableEntity entity = new TableEntity(partitionKey, rowKey)
.addProperty("Name", "Yamba Surfboard")
.addProperty("Quantity", 12)
.addProperty("Price", 850.00)
.addProperty("Sale", false);
使用 upsertEntity在集合中建立實體。
Mono<Void> response = table.upsertEntity(entity);
取得實體
您可以使用 從資料表 getEntity擷取特定實體。
String rowKey = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb";
String partitionKey = "gear-surf-surfboards";
TableEntity entity = table.getEntity(partitionKey, rowKey);
查詢實體
插入實體之後,您也可以執行查詢,以取得使用 listEntities 和 ListEntitiesOptions 類別符合特定篩選的所有實體。 使用 setFilter 方法來指定字串 OData 篩選條件。
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter("PartitionKey eq 'gear-surf-surfboards'");
PagedFlux<TableEntity> tableEntities = table.listEntities(options, null, null);
使用訂用帳戶剖析查詢的編頁結果。
tableEntities
.DoOnNext(entity -> {
// Do something
});
清除資源
當您不再需要範例應用程式或資源時,請移除對應的部署和所有資源。
azd down