private Document getDocumentById(String collectionId, String id) { if (collection == null) { return null; } // Retrieve the document using the DocumentClient. List<Document> documentList = documentClient .queryDocuments(collection.getSelfLink(), "SELECT * FROM root r WHERE r.id='" + id + "'", feedOptions) .getQueryIterable() .toList(); if (documentList.size() > 0) { return documentList.get(0); } return null; } }
FeedResponse<Document> feedResponse = null; try { feedResponse = AzureCosmosClient.client.queryDocuments(getDocumentCollectionLink(this.databaseName, table), new SqlQuerySpec(queryText, new SqlParameterCollection(new SqlParameter("@recordcount", recordcount),
@Override public FeedResponse<Document> invoke() throws Exception { return client.queryDocuments(collectionLink, query, options); } });
@Override public FeedResponse<Document> invoke() throws Exception { return client.queryDocuments(collectionLink, query, options, partitionKey); } });
/** * Query for documents in a document collection. * * @param collectionLink the link to the parent document collection. * @param querySpec the SQL query specification. * @param options the feed options. * @return the feed response with the obtained documents. */ public FeedResponse<Document> queryDocuments(String collectionLink, SqlQuerySpec querySpec, FeedOptions options) { return this.queryDocuments(collectionLink, querySpec, options, null); }
/** * Query for documents in a document collection. * * @param collectionLink the link to the parent document collection. * @param query the query. * @param options the feed options. * @return the feed response with the obtained documents. */ public FeedResponse<Document> queryDocuments(String collectionLink, String query, FeedOptions options) { return this.queryDocuments(collectionLink, query, options, null); }
@Override public FeedResponse<Document> invoke() throws Exception { return client.queryDocuments(collectionLink, querySpec, options); } });
@Override public FeedResponse<Document> invoke() throws Exception { return client.queryDocuments(collectionLink, querySpec, options, partitionKey); } });
/** * Query for documents in a document collection with a partitionKey * * @param collectionLink the link to the parent document collection. * @param query the query. * @param options the feed options. * @param partitionKey the partitionKey. * @return the feed response with the obtained documents. */ public FeedResponse<Document> queryDocuments(String collectionLink, String query, FeedOptions options, Object partitionKey) { if (StringUtils.isEmpty(collectionLink)) { throw new IllegalArgumentException("collectionLink"); } if (StringUtils.isEmpty(query)) { throw new IllegalArgumentException("query"); } return queryDocuments(collectionLink, new SqlQuerySpec(query, null), options, partitionKey); }
/** * Query for aggregation values in a document collection. * * @param collectionLink the link to the parent document collection. * @param querySpec the SQL query specification. * @param feedOptions the feed options. * @param partitionKey the partitionKey. * @return a list containing the aggregation values */ public List<Object> queryAggregateValues(String collectionLink, SqlQuerySpec querySpec, FeedOptions feedOptions, Object partitionKey) { List<Document> items = this.queryDocuments(collectionLink, querySpec, feedOptions, partitionKey) .getQueryIterable().toList(); List<Object> values = new ArrayList<>(items.size()); for (Document item : items) { if (item.propertyBag.length() > 0) { values.add(item.propertyBag.get(item.propertyBag.keys().next().toString())); } else { values.add(null); } } return values; }
private FeedResponse<Document> executeQuery(@NonNull SqlQuerySpec sqlQuerySpec, FeedOptions feedOptions, String collectionName) { final String selfLink = getCollectionSelfLink(collectionName); return getDocumentClient().queryDocuments(selfLink, sqlQuerySpec, feedOptions); }
public <T> List<T> find(Query query, Class<T> domainClass, String collectionName) { final SqlQuerySpec sqlQuerySpec = createSqlQuerySpec(query, domainClass); // TODO (wepa) Collection link should be created locally without accessing database, // but currently exception will be thrown if not fetching collection url from database. // Run repository integration test to reproduce. final DocumentCollection collection = getDocCollection(collectionName); final FeedOptions feedOptions = new FeedOptions(); final Optional<Object> partitionKeyValue = getPartitionKeyValue(query, domainClass); if (!partitionKeyValue.isPresent()) { feedOptions.setEnableCrossPartitionQuery(true); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collection.getSelfLink(), sqlQuerySpec, feedOptions) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(domainClass, results.get(i)); entities.add(entity); } return entities; }
private FeedResponse<Document> executeQuery(@NonNull SqlQuerySpec sqlQuerySpec, boolean isCrossPartition, String collectionName) { final FeedOptions feedOptions = new FeedOptions(); final String selfLink = getCollectionSelfLink(collectionName); feedOptions.setEnableCrossPartitionQuery(isCrossPartition); return getDocumentClient().queryDocuments(selfLink, sqlQuerySpec, feedOptions); }
public <T> List<T> findAll(String collectionName, final Class<T> entityClass) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new RuntimeException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collections.get(0).getSelfLink(), "SELECT * FROM c", null) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(entityClass, results.get(i)); entities.add(entity); } return entities; }
public <T> List<T> findAll(String collectionName, final Class<T> entityClass) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new IllegalCollectionException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } final FeedOptions feedOptions = new FeedOptions(); feedOptions.setEnableCrossPartitionQuery(true); final SqlQuerySpec sqlQuerySpec = new SqlQuerySpec("SELECT * FROM root c"); final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collections.get(0).getSelfLink(), sqlQuerySpec, feedOptions) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(entityClass, results.get(i)); entities.add(entity); } return entities; }
@Override public <T> List<T> delete(Query query, Class<T> entityClass, String collectionName) { final SqlQuerySpec sqlQuerySpec = createSqlQuerySpec(query, entityClass); final Optional<Object> partitionKeyValue = getPartitionKeyValue(query, entityClass); final DocumentCollection collection = getDocCollection(collectionName); final FeedOptions feedOptions = new FeedOptions(); if (!partitionKeyValue.isPresent()) { feedOptions.setEnableCrossPartitionQuery(true); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collection.getSelfLink(), sqlQuerySpec, feedOptions).getQueryIterable().toList(); final RequestOptions options = new RequestOptions(); if (partitionKeyValue.isPresent()) { options.setPartitionKey(new PartitionKey(partitionKeyValue.get())); } final List<T> deletedResult = new ArrayList<>(); for (final Document document : results) { try { documentDbFactory.getDocumentClient().deleteDocument((document).getSelfLink(), options); deletedResult.add(getConverter().read(entityClass, document)); } catch (DocumentClientException e) { throw new DocumentDBAccessException( String.format("Failed to delete document [%s]", (document).getSelfLink()), e); } } return deletedResult; }