use of com.azure.cosmos.util.CosmosPagedFlux in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedureAsync method readAllSprocs.
private void readAllSprocs() throws Exception {
CosmosPagedFlux<CosmosStoredProcedureProperties> fluxResponse = container.getScripts().readAllStoredProcedures();
final CountDownLatch completionLatch = new CountDownLatch(1);
fluxResponse.flatMap(storedProcedureProperties -> {
logger.info(String.format("Stored Procedure: %s\n", storedProcedureProperties.getId()));
return Mono.empty();
}).subscribe(s -> {
}, err -> {
if (err instanceof CosmosException) {
// Client-specific errors
CosmosException cerr = (CosmosException) err;
cerr.printStackTrace();
logger.info(String.format("Read Item failed with %s\n", cerr));
} else {
// General errors
err.printStackTrace();
}
completionLatch.countDown();
}, () -> {
completionLatch.countDown();
});
completionLatch.await();
}
use of com.azure.cosmos.util.CosmosPagedFlux in project kafka-connect-cosmosdb by microsoft.
the class CosmosDBSourceTaskTest method setup.
@Before
// Need to maintain Typed objects
@SuppressWarnings("unchecked")
public void setup() throws IllegalAccessException {
testTask = new CosmosDBSourceTask();
// Configure settings
sourceSettings = CosmosDBSourceConfigTest.setupConfigs();
sourceSettings.put(CosmosDBSourceConfig.COSMOS_CONTAINER_TOPIC_MAP_CONF, topicName + "#" + containerName);
sourceSettings.put(CosmosDBSourceConfig.COSMOS_DATABASE_NAME_CONF, databaseName);
sourceSettings.put(CosmosDBSourceConfig.COSMOS_ASSIGNED_CONTAINER_CONF, containerName);
sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_POLL_INTERVAL_CONF, "500");
sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_BATCH_SIZE_CONF, "1");
sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_BUFFER_SIZE_CONF, "5000");
sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_TIMEOUT_CONF, "1000");
config = new CosmosDBSourceConfig(sourceSettings);
FieldUtils.writeField(testTask, "config", config, true);
// Create the TransferQueue
LinkedTransferQueue<JsonNode> queue = new LinkedTransferQueue<>();
FieldUtils.writeField(testTask, "queue", queue, true);
// Set the running flag to true
AtomicBoolean running = new AtomicBoolean();
running.set(true);
FieldUtils.writeField(testTask, "running", running, true);
// Mock the Cosmos SDK
mockCosmosClient = Mockito.mock(CosmosAsyncClient.class);
CosmosAsyncDatabase mockDatabase = Mockito.mock(CosmosAsyncDatabase.class);
when(mockCosmosClient.getDatabase(anyString())).thenReturn(mockDatabase);
mockFeedContainer = Mockito.mock(CosmosAsyncContainer.class);
when(mockDatabase.getContainer("feed")).thenReturn(mockFeedContainer);
mockLeaseContainer = Mockito.mock(CosmosAsyncContainer.class);
when(mockDatabase.getContainer("lease")).thenReturn(mockLeaseContainer);
// Mock query results and iterator for getting lease container token
CosmosPagedFlux<JsonNode> mockLeaseQueryResults = (CosmosPagedFlux<JsonNode>) Mockito.mock(CosmosPagedFlux.class);
when(mockLeaseContainer.queryItems(anyString(), any(), eq(JsonNode.class))).thenReturn(mockLeaseQueryResults);
Iterable<JsonNode> mockLeaseQueryIterable = (Iterable<JsonNode>) Mockito.mock(Iterable.class);
when(mockLeaseQueryResults.toIterable()).thenReturn(mockLeaseQueryIterable);
Iterator<JsonNode> mockLeaseQueryIterator = (Iterator<JsonNode>) Mockito.mock(Iterator.class);
when(mockLeaseQueryIterable.iterator()).thenReturn(mockLeaseQueryIterator);
when(mockLeaseQueryIterator.hasNext()).thenReturn(false);
FieldUtils.writeField(testTask, "client", mockCosmosClient, true);
FieldUtils.writeField(testTask, "leaseContainer", mockLeaseContainer, true);
}
use of com.azure.cosmos.util.CosmosPagedFlux in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleCRUDQuickstartAsync method queryItems.
private void queryItems() {
// <QueryItems>
// Set some common query options
// We'll use this later
int preferredPageSize = 10;
CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
// Set populate query metrics to get metrics around query executions
queryOptions.setQueryMetricsEnabled(true);
CosmosPagedFlux<Family> pagedFluxResponse = container.queryItems("SELECT * FROM Family WHERE Family.lastName IN ('Andersen', 'Wakefield', 'Johnson')", queryOptions, Family.class);
try {
pagedFluxResponse.byPage(preferredPageSize).flatMap(fluxResponse -> {
logger.info("Got a page of query result with " + fluxResponse.getResults().size() + " items(s)" + " and request charge of " + fluxResponse.getRequestCharge());
logger.info("Item Ids " + fluxResponse.getResults().stream().map(Family::getId).collect(Collectors.toList()));
return Flux.empty();
}).blockLast();
} catch (Exception err) {
if (err instanceof CosmosException) {
// Client-specific errors
CosmosException cerr = (CosmosException) err;
cerr.printStackTrace();
logger.error(String.format("Read Item failed with %s\n", cerr));
} else {
// General errors
err.printStackTrace();
}
}
// </QueryItems>
}
use of com.azure.cosmos.util.CosmosPagedFlux in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class CosmosDiagnosticsQuickStartAsync method queryDocuments.
private void queryDocuments() throws Exception {
logger.info("Query documents in the container : {}", containerName);
String sql = "SELECT * FROM c WHERE c.lastName = 'Witherspoon'";
CosmosPagedFlux<Family> filteredFamilies = container.queryItems(sql, new CosmosQueryRequestOptions(), Family.class);
// Add handler to capture diagnostics
filteredFamilies = filteredFamilies.handle(familyFeedResponse -> {
logger.info("Query Item diagnostics through handler : {}", familyFeedResponse.getCosmosDiagnostics());
});
// Or capture diagnostics through byPage() APIs.
filteredFamilies.byPage().toIterable().forEach(familyFeedResponse -> {
logger.info("Query item diagnostics through iterableByPage : {}", familyFeedResponse.getCosmosDiagnostics());
});
logger.info("Done.");
}
use of com.azure.cosmos.util.CosmosPagedFlux in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleSubpartitioningAsync method queryItems.
// Add the
private void queryItems() {
// <QueryItems>
// Set some common query options
// We'll use this later
int preferredPageSize = 10;
CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
// Set populate query metrics to get metrics around query executions
queryOptions.setQueryMetricsEnabled(true);
/**
* Subpartitioning support:
* Specifying partial partition key values in query, will only route the query to
* the subset of physical partitions on which the documents with this partition key
* value exist.
*/
CosmosPagedFlux<UserSession> pagedFluxResponse = container.queryItems("SELECT * FROM t WHERE t.TenantId IN ('Microsoft')", queryOptions, UserSession.class);
try {
pagedFluxResponse.byPage(preferredPageSize).flatMap(fluxResponse -> {
logger.info("Got a page of query result with " + fluxResponse.getResults().size() + " items(s)" + " and request charge of " + fluxResponse.getRequestCharge());
logger.info("Item Ids " + fluxResponse.getResults().stream().map(UserSession::getId).collect(Collectors.toList()));
return Flux.empty();
}).blockLast();
} catch (Exception err) {
if (err instanceof CosmosException) {
// Client-specific errors
CosmosException cerr = (CosmosException) err;
cerr.printStackTrace();
logger.error(String.format("Read Item failed with %s\n", cerr));
} else {
// General errors
err.printStackTrace();
}
}
// </QueryItems>
}
Aggregations