CosmosContainer クラス
- java.
lang. Object - com.
azure. cosmos. CosmosContainer
- com.
public class CosmosContainer
既存のコンテナーの読み取り、削除、および置き換えのための同期メソッドを提供する 子リソース (項目、スクリプト、競合) と対話するためのメソッドを提供します
メソッドの概要
メソッドの継承元: java.lang.Object
メソッドの詳細
createItem
public CosmosItemResponse
新しい項目を同期的に作成し、それぞれの Cosmos 項目の応答を返します。
Parameters:
Returns:
createItem
public CosmosItemResponse
新しい項目を同期的に作成し、追加のオプションを指定しながら、それぞれの Cosmos 項目の応答を返します。
パーティション キーの値は、アイテムのコンテンツから自動的に抽出されます。
Parameters:
Returns:
createItem
public CosmosItemResponse
新しい項目を同期的に作成し、追加のオプションを指定しながら、それぞれの Cosmos 項目の応答を返します。
Parameters:
Returns:
deleteItem
public CosmosItemResponse
現在のコンテナー内の項目を削除します。
Parameters:
Returns:
patchItem
public CosmosItemResponse
アイテム全体を置き換えることなく、アイテムの特定のプロパティまたはフィールドを変更する部分更新を実行します。
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations
.add("/departure", "SEA")
.increment("/trips", 1);
CosmosItemResponse<Passenger> response = cosmosContainer.patchItem(
passenger.getId(),
new PartitionKey(passenger.getId()),
cosmosPatchOperations,
Passenger.class);
Parameters:
Returns:
patchItem
public CosmosItemResponse
アイテム全体を置き換えることなく、アイテムの特定のプロパティまたはフィールドを変更する部分更新を実行します。
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations
.add("/departure", "SEA")
.increment("/trips", 1);
CosmosItemResponse<Passenger> response = cosmosContainer.patchItem(
passenger.getId(),
new PartitionKey(passenger.getId()),
cosmosPatchOperations,
Passenger.class);
Parameters:
Returns:
queryChangeFeed
public CosmosPagedIterable
を使用して、現在のコンテナーの変更フィード内の項目を照会します CosmosChangeFeedRequestOptions。
CosmosChangeFeedRequestOptions options = CosmosChangeFeedRequestOptions
.createForProcessingFromNow(FeedRange.forFullRange())
.allVersionsAndDeletes();
Iterable<FeedResponse<Passenger>> feedResponses = cosmosContainer.queryChangeFeed(options, Passenger.class)
.iterableByPage();
for (FeedResponse<Passenger> feedResponse : feedResponses) {
List<Passenger> results = feedResponse.getResults();
System.out.println(results);
}
次のページは、前に返されたインスタンスの継続トークンから作成された の CosmosChangeFeedRequestOptions 新しいインスタンスを使用して queryChangeFeed をもう一度呼び出すことによって取得 FeedResponse<T> できます。
Parameters:
Returns:
queryItems
public CosmosPagedIterable
結果を として CosmosPagedIterable<T>返す現在のコンテナー内のクエリ 項目。
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
String query = "SELECT * FROM Passenger p WHERE (p.departure = @departure)";
List<SqlParameter> parameters = Collections.singletonList(new SqlParameter("@departure", "SEA"));
SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(query, parameters);
Iterable<FeedResponse<Passenger>> queryResponses = cosmosContainer.queryItems(sqlQuerySpec, options, Passenger.class)
.iterableByPage();
for (FeedResponse<Passenger> feedResponse : queryResponses) {
List<Passenger> results = feedResponse.getResults();
System.out.println(results);
}
Parameters:
Returns:
queryItems
public CosmosPagedIterable
結果を として CosmosPagedIterable<T>返す現在のコンテナー内のクエリ 項目。
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
String query = "SELECT * FROM Passenger WHERE Passenger.departure IN ('SEA', 'IND')";
Iterable<FeedResponse<Passenger>> queryResponses = cosmosContainer.queryItems(query, options, Passenger.class)
.iterableByPage();
for (FeedResponse<Passenger> feedResponse : queryResponses) {
List<Passenger> results = feedResponse.getResults();
System.out.println(results);
}
Parameters:
Returns:
readAllItems
public CosmosPagedIterable
結果を として CosmosPagedIterable<T>返す論理パーティションのすべての項目を読み取ります。
CosmosPagedIterable<Passenger> passengers = cosmosContainer
.readAllItems(new PartitionKey(partitionKey), Passenger.class);
passengers.forEach(passenger -> {
System.out.println(passenger);
});
Parameters:
Returns:
readAllItems
public CosmosPagedIterable
結果を として CosmosPagedIterable<T>返す論理パーティションのすべての項目を読み取ります。
CosmosPagedIterable<Passenger> passengers = cosmosContainer
.readAllItems(new PartitionKey(partitionKey), Passenger.class);
passengers.forEach(passenger -> {
System.out.println(passenger);
});
Parameters:
Returns:
readItem
public CosmosItemResponse
追加のオプションを指定しながら、現在のコンテナー内の項目を読み取ります。 この操作は、一意識別子 (ID) とパーティション キーに基づいてコンテナーから 1 つの項目を取得するために使用されます。 readItem 操作は、アイテムの ID とパーティション キー値で構成される一意識別子を使用して、特定のアイテムに直接アクセスします。 この操作は、複雑なクエリを必要とせずに、ID とパーティション キーによって既知の項目を取得する場合に効率的です。
Parameters:
Returns:
readItem
public CosmosItemResponse
現在のコンテナー内の項目を読み取ります。 この操作は、一意識別子 (ID) とパーティション キーに基づいてコンテナーから 1 つの項目を取得するために使用されます。 readItem 操作は、アイテムの ID とパーティション キー値で構成される一意識別子を使用して、特定のアイテムに直接アクセスします。 この操作は、複雑なクエリを必要とせずに、ID とパーティション キーによって既知の項目を取得する場合に効率的です。
// Read an item
try {
CosmosItemResponse<Passenger> response = cosmosContainer.readItem(
passenger.getId(),
new PartitionKey(passenger.getId()),
Passenger.class
);
Passenger passengerItem = response.getItem();
} catch (NotFoundException e) {
// catch exception if item not found
System.out.printf("Passenger with item id %s not found\n",
passenger.getId());
} catch (Exception e) {
System.out.println(e.getMessage());
}
// ...
Parameters:
Returns:
readMany
public FeedResponse
多数のドキュメントを読み取ります。 特定の ID とパーティション キーを持つ多くのドキュメントを 1 つの要求で読み取る場合に便利です。 リストのドキュメントがない場合、例外はスローされません。
List<CosmosItemIdentity> itemIdentityList = new ArrayList<>();
itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger1Id), passenger1Id));
itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger2Id), passenger2Id));
FeedResponse<Passenger> passengerFeedResponse = cosmosContainer.readMany(itemIdentityList, Passenger.class);
for (Passenger passenger : passengerFeedResponse.getResults()) {
System.out.println(passenger);
}
Parameters:
Returns:
readMany
public FeedResponse
多数のドキュメントを読み取ります。 特定の ID とパーティション キーを持つ多くのドキュメントを 1 つの要求で読み取る場合に便利です。 リストのドキュメントがない場合、例外はスローされません。
List<CosmosItemIdentity> itemIdentityList = new ArrayList<>();
itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger1Id), passenger1Id));
itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger2Id), passenger2Id));
FeedResponse<Passenger> passengerFeedResponse = cosmosContainer.readMany(itemIdentityList, Passenger.class);
for (Passenger passenger : passengerFeedResponse.getResults()) {
System.out.println(passenger);
}
Parameters:
Returns:
replaceItem
public CosmosItemResponse
コンテナー内の既存の項目を新しい項目に置き換えます。 項目の完全な置換が実行され、そのすべてのプロパティが新しい項目のプロパティに置き換えられます。
CosmosItemResponse<Passenger> response = cosmosContainer.replaceItem(
newPassenger,
oldPassenger.getId(),
new PartitionKey(oldPassenger.getId()),
new CosmosItemRequestOptions());
Parameters:
Returns:
upsertItem
public CosmosItemResponse
現在のコンテナー内の Cosmos 項目をアップサートします。
Parameters:
Returns:
upsertItem
public CosmosItemResponse
追加のオプションを指定しながら、アイテム Cosmos 同期項目をアップサートします。
Parameters:
Returns:
upsertItem
public CosmosItemResponse
追加のオプションを指定しながら、アイテム Cosmos 同期項目をアップサートします。
Parameters:
Returns:
executeBulkOperations
public Iterable<>
一括で操作の一覧を実行します。
Parameters:
Returns:
executeBulkOperations
public Iterable<>
一括で操作の一覧を実行します。
Parameters:
Returns:
delete
public CosmosContainerResponse delete()
現在の Cosmos コンテナーを削除します。
Returns:
delete
public CosmosContainerResponse delete(CosmosContainerRequestOptions options)
If-Match などの追加オプションを指定しながら、現在の Cosmos コンテナーを削除します。
Parameters:
Returns:
deleteAllItemsByPartitionKey
public CosmosItemResponse
指定した partitionKey 値を持つコンテナー内のすべての項目を削除します。 指定した値を使用してコンテナー内のすべての項目を削除する非同期 Cosmos DB バックグラウンド操作を開始します。 非同期 Cosmos DB バックグラウンド操作は、ユーザー RU の割合を使用して実行されます。
Parameters:
Returns:
deleteItem
public CosmosItemResponse
現在のコンテナー内の項目を削除します。
try {
CosmosItemRequestOptions options = new CosmosItemRequestOptions();
CosmosItemResponse<Object> deleteItemResponse = cosmosContainer.deleteItem(
passenger.getId(),
new PartitionKey(passenger.getId()),
options
);
System.out.println(deleteItemResponse);
} catch (NotFoundException e) {
// catch exception if item not found
System.out.printf("Passenger with item id %s not found\n",
passenger.getId());
} catch (Exception e) {
System.out.println(e.getMessage());
}
Parameters:
Returns:
enableGlobalThroughputControlGroup
public void enableGlobalThroughputControlGroup(ThroughputControlGroupConfig groupConfig, GlobalThroughputControlConfig globalControlConfig)
グローバル制御モードでスループット制御グループを有効にします。 定義されたスループット制限は、異なるクライアント間で共有されます。
ThroughputControlGroupConfig groupConfig =
new ThroughputControlGroupConfigBuilder()
.groupName("localControlGroup")
.targetThroughputThreshold(0.1)
.build();
GlobalThroughputControlConfig globalControlConfig =
this.client.createGlobalThroughputControlConfigBuilder(database.getId(), container.getId())
.setControlItemRenewInterval(Duration.ofSeconds(5))
.setControlItemExpireInterval(Duration.ofSeconds(10))
.build();
container.enableGlobalThroughputControlGroup(groupConfig, globalControlConfig);
Parameters:
enableLocalThroughputControlGroup
public void enableLocalThroughputControlGroup(ThroughputControlGroupConfig groupConfig)
ローカル制御モードでスループット制御グループを有効にします。
ThroughputControlGroupConfig groupConfig =
new ThroughputControlGroupConfigBuilder()
.groupName("localControlGroup")
.targetThroughputThreshold(0.1)
.build();
container.enableLocalThroughputControlGroup(groupConfig);
Parameters:
executeCosmosBatch
public CosmosBatchResponse executeCosmosBatch(CosmosBatch cosmosBatch)
トランザクション バッチを実行します。
Parameters:
Returns:
トランザクション バッチが正常に実行された場合、返される応答で によって CosmosBatchResponse#getStatusCode 返される値は 200} に設定されます。
実行中にトランザクション バッチ内の操作が失敗した場合、バッチからの変更はコミットされません。失敗した操作の状態は、例外によってまたは例外によって CosmosBatchResponse#getStatusCode 使用可能になります。 競合、見つからないなどのユーザー エラーが発生した場合に失敗した操作に関する情報を取得するには、応答を列挙できます。 これにより、トランザクション バッチ内の CosmosBatchOperationResult 各操作に対応するインスタンスが、トランザクション バッチに追加された順序で返されます。 トランザクション バッチ内の操作に対応する結果の場合は、 を使用 CosmosBatchOperationResult#getStatusCode して操作の状態にアクセスします。 操作が実行されなかった場合、またはトランザクション バッチ内の別の操作が失敗したために中止された場合、このフィールドの値は 424 になります。バッチが中止された操作の場合、このフィールドの値はエラーの原因を示します。
要求タイムアウト、ゴーン、セッション利用不可、ネットワーク障害などの問題がある場合、またはサービスが何らかの方法で 5xx を返した場合、CosmosBatchResponse を返す代わりに例外がスローされます。
トランザクション バッチが成功したことを確認するには、返された応答で を使用 CosmosBatchResponse#isSuccessStatusCode します。
executeCosmosBatch
public CosmosBatchResponse executeCosmosBatch(CosmosBatch cosmosBatch, CosmosBatchRequestOptions requestOptions)
トランザクション バッチを実行します。
Parameters:
Returns:
トランザクション バッチが正常に実行された場合、返される応答で によって CosmosBatchResponse#getStatusCode 返される値は 200} に設定されます。
実行中にトランザクション バッチ内の操作が失敗した場合、バッチからの変更はコミットされません。失敗した操作の状態は、例外によってまたは例外によって CosmosBatchResponse#getStatusCode 使用可能になります。 競合、見つからないなどのユーザー エラーが発生した場合に失敗した操作に関する情報を取得するには、応答を列挙できます。 これにより、トランザクション バッチ内の CosmosBatchOperationResult 各操作に対応するインスタンスが、トランザクション バッチに追加された順序で返されます。 トランザクション バッチ内の操作に対応する結果の場合は、 を使用 CosmosBatchOperationResult#getStatusCode して操作の状態にアクセスします。 操作が実行されなかった場合、またはトランザクション バッチ内の別の操作が失敗したために中止された場合、このフィールドの値は 424 になります。バッチが中止された操作の場合、このフィールドの値はエラーの原因を示します。
要求タイムアウト、ゴーン、セッション利用不可、ネットワーク障害などの問題がある場合、またはサービスが何らかの方法で 5xx を返した場合、CosmosBatchResponse を返す代わりに例外がスローされます。
トランザクション バッチが成功したことを確認するには、返された応答で を使用 CosmosBatchResponse#isSuccessStatusCode します。
getFeedRanges
getId
public String getId()
現在のコンテナー ID を取得します。
Returns:
getScripts
public CosmosScripts getScripts()
現在のコンテナーをコンテキストとして使用して Cosmos スクリプトを取得します。
Returns:
openConnectionsAndInitCaches
@Deprecated
public void openConnectionsAndInitCaches()
非推奨
現在の読み取りリージョンのキャッシュと接続をウォームアップして、コンテナーを初期化します。
注: この API は、ワークロードの前にアプリケーションの初期化中に 1 回だけ呼び出すのが理想的です。 一時的なエラーが発生した場合、呼び出し元はエラーを使用し、通常のワークロードを続行する必要があります。
openConnectionsAndInitCaches
@Deprecated
public void openConnectionsAndInitCaches(int numProactiveConnectionRegions)
非推奨
事前接続リージョンの指定された no へのキャッシュと接続をウォームアップして、コンテナーを初期化します。 プロアクティブ接続リージョンの詳細については、次を参照してください。 getProactiveConnectionRegionsCount()
注: この API は、ワークロードの前にアプリケーションの初期化中に 1 回だけ呼び出すのが理想的です。 一時的なエラーが発生した場合、呼び出し元はエラーを使用し、通常のワークロードを続行する必要があります。
Parameters:
read
public CosmosContainerResponse read()
現在のコンテナーを読み取ります。
Returns:
read
public CosmosContainerResponse read(CosmosContainerRequestOptions options)
If-Match などの追加のオプションを指定しながら、現在のコンテナーを読み取ります。
Parameters:
Returns:
readThroughput
public ThroughputResponse readThroughput()
現在のコンテナーのスループットを取得します。
try {
ThroughputResponse throughputResponse = cosmosContainer.readThroughput();
System.out.println(throughputResponse);
} catch (CosmosException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Returns:
replace
public CosmosContainerResponse replace(CosmosContainerProperties containerProperties)
現在のコンテナー のプロパティを置き換えます。
Parameters:
Returns:
replace
public CosmosContainerResponse replace(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options)
If-Match などの追加オプションを指定しながら、現在のコンテナーのプロパティを置き換えます。
Parameters:
Returns:
replaceThroughput
public ThroughputResponse replaceThroughput(ThroughputProperties throughputProperties)
現在のコンテナーのスループットを設定します。
ThroughputProperties throughputProperties =
ThroughputProperties.createAutoscaledThroughput(1000);
try {
ThroughputResponse throughputResponse =
cosmosContainer.replaceThroughput(throughputProperties);
System.out.println(throughputResponse);
} catch (CosmosException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Parameters:
Returns:
適用対象
Azure SDK for Java
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示