use of com.azure.cosmos.CosmosAsyncDatabase in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleChangeFeedProcessor method createNewLeaseCollection.
public static CosmosAsyncContainer createNewLeaseCollection(CosmosAsyncClient client, String databaseName, String leaseCollectionName) {
CosmosAsyncDatabase databaseLink = client.getDatabase(databaseName);
CosmosAsyncContainer leaseCollectionLink = databaseLink.getContainer(leaseCollectionName);
CosmosContainerResponse leaseContainerResponse = null;
try {
leaseContainerResponse = leaseCollectionLink.read().block();
if (leaseContainerResponse != null) {
leaseCollectionLink.delete().block();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
} catch (RuntimeException ex) {
if (ex instanceof CosmosException) {
CosmosException CosmosException = (CosmosException) ex;
if (CosmosException.getStatusCode() != 404) {
throw ex;
}
} else {
throw ex;
}
}
CosmosContainerProperties containerSettings = new CosmosContainerProperties(leaseCollectionName, "/id");
CosmosContainerRequestOptions requestOptions = new CosmosContainerRequestOptions();
ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
leaseContainerResponse = databaseLink.createContainer(containerSettings, throughputProperties, requestOptions).block();
if (leaseContainerResponse == null) {
throw new RuntimeException(String.format("Failed to create collection %s in database %s.", leaseCollectionName, databaseName));
}
return databaseLink.getContainer(leaseContainerResponse.getProperties().getId());
}
use of com.azure.cosmos.CosmosAsyncDatabase in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleChangeFeedProcessor method createNewCollection.
public static CosmosAsyncContainer createNewCollection(CosmosAsyncClient client, String databaseName, String collectionName) {
CosmosAsyncDatabase databaseLink = client.getDatabase(databaseName);
CosmosAsyncContainer collectionLink = databaseLink.getContainer(collectionName);
CosmosContainerResponse containerResponse = null;
try {
containerResponse = collectionLink.read().block();
if (containerResponse != null) {
throw new IllegalArgumentException(String.format("Collection %s already exists in database %s.", collectionName, databaseName));
}
} catch (RuntimeException ex) {
if (ex instanceof CosmosException) {
CosmosException CosmosException = (CosmosException) ex;
if (CosmosException.getStatusCode() != 404) {
throw ex;
}
} else {
throw ex;
}
}
CosmosContainerProperties containerSettings = new CosmosContainerProperties(collectionName, "/pk");
CosmosContainerRequestOptions requestOptions = new CosmosContainerRequestOptions();
ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(10000);
containerResponse = databaseLink.createContainer(containerSettings, throughputProperties, requestOptions).block();
if (containerResponse == null) {
throw new RuntimeException(String.format("Failed to create collection %s in database %s.", collectionName, databaseName));
}
return databaseLink.getContainer(containerResponse.getProperties().getId());
}
use of com.azure.cosmos.CosmosAsyncDatabase in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method ManageConflictResolutionPoliciesInAzureCosmosDBCustomAsync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-manage-conflicts
* Resolve conflicts, stored procedure
*/
/**
* Client-side conflict resolution with fully custom policy
*/
public static void ManageConflictResolutionPoliciesInAzureCosmosDBCustomAsync() {
String container_id = "family_container";
String partition_key = "/pk";
CosmosAsyncDatabase database = null;
// <ManageConflictResolutionCustomAsync>
ConflictResolutionPolicy policy = ConflictResolutionPolicy.createCustomPolicy();
CosmosContainerProperties containerProperties = new CosmosContainerProperties(container_id, partition_key);
containerProperties.setConflictResolutionPolicy(policy);
/* ...other container config... */
database.createContainerIfNotExists(containerProperties).block();
// </ManageConflictResolutionCustomAsync>
}
use of com.azure.cosmos.CosmosAsyncDatabase in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method MigrateJavaSDKv4ItemOperationsAsync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
* Item operations
*/
/**
* Item operations
*/
public static void MigrateJavaSDKv4ItemOperationsAsync() {
String container_id = "family_container";
String partition_key = "/pk";
CosmosAsyncDatabase database = null;
// <MigrateItemOpsAsync>
// Container is created. Generate many docs to insert.
int number_of_docs = 50000;
ArrayList<JsonNode> docs = generateManyDocs(number_of_docs);
// Insert many docs into container...
Flux.fromIterable(docs).flatMap(doc -> testContainerAsync.createItem(doc)).subscribe();
// </MigrateItemOpsAsync>
}
use of com.azure.cosmos.CosmosAsyncDatabase in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method ManageConflictResolutionPoliciesInAzureCosmosDBLWWAsync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-manage-conflicts
* Resolve conflicts, LWW policy
*/
/**
* Client-side conflict resolution settings for LWW policy
*/
public static void ManageConflictResolutionPoliciesInAzureCosmosDBLWWAsync() {
String container_id = "family_container";
String partition_key = "/pk";
CosmosAsyncDatabase database = null;
// <ManageConflictResolutionLWWAsync>
ConflictResolutionPolicy policy = ConflictResolutionPolicy.createLastWriterWinsPolicy("/myCustomId");
CosmosContainerProperties containerProperties = new CosmosContainerProperties(container_id, partition_key);
containerProperties.setConflictResolutionPolicy(policy);
/* ...other container config... */
database.createContainerIfNotExists(containerProperties).block();
// </ManageConflictResolutionLWWAsync>
}
Aggregations