use of com.azure.cosmos.CosmosAsyncContainer 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.CosmosAsyncContainer 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.CosmosAsyncContainer in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method TroubleshootingGuideJavaSDKv4PublishOnSchedulerAsync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/troubleshoot-java-sdk-v4-sql
* Troubleshooting guide - publish on scheduler
* Async only
*/
/**
* Troubleshooting guide - publish on scheduler
*/
public static void TroubleshootingGuideJavaSDKv4PublishOnSchedulerAsync() {
CosmosAsyncContainer container = null;
Scheduler customScheduler = null;
Family family = null;
// <TroubleshootPublishOnSchedulerAsync>
container.createItem(family).publishOn(// Switches the thread.
customScheduler).subscribe();
// </TroubleshootPublishOnSchedulerAsync>
}
use of com.azure.cosmos.CosmosAsyncContainer in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method PerformanceTipsJavaSDKv4NeedsSchedulerAsync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
* Performance tips - needs scheduler
* Async only
*/
/**
* Performance tips - needs scheduler
*/
public static void PerformanceTipsJavaSDKv4NeedsSchedulerAsync() {
CosmosAsyncContainer asyncContainer = null;
CustomPOJO item = null;
// <PerformanceNeedsSchedulerAsync>
Mono<CosmosItemResponse<CustomPOJO>> createItemPub = asyncContainer.createItem(item);
createItemPub.subscribe(itemResponse -> {
// this is executed on eventloop IO netty thread.
// the eventloop thread is shared and is meant to return back quickly.
//
// DON'T do this on eventloop IO netty thread.
veryCpuIntensiveWork();
});
// </PerformanceNeedsSchedulerAsync>
}
use of com.azure.cosmos.CosmosAsyncContainer in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method PerformanceTipsJavaSDKv4AddSchedulerSync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
* Performance tips - add scheduler
* Async only
*/
/**
* Performance tips - add scheduler
*/
public static void PerformanceTipsJavaSDKv4AddSchedulerSync() {
CosmosAsyncContainer asyncContainer = null;
CustomPOJO item = null;
// <PerformanceAddSchedulerAsync>
Mono<CosmosItemResponse<CustomPOJO>> createItemPub = asyncContainer.createItem(item);
createItemPub.subscribeOn(Schedulers.elastic()).subscribe(itemResponse -> {
// this is executed on eventloop IO netty thread.
// the eventloop thread is shared and is meant to return back quickly.
//
// DON'T do this on eventloop IO netty thread.
veryCpuIntensiveWork();
});
// </PerformanceAddSchedulerAsync>
}
Aggregations