首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有任何方法在Java (DocumentDbRepository)中编写自定义查询或本机查询,同时向azure-cosmosdb发送查询?

是否有任何方法在Java (DocumentDbRepository)中编写自定义查询或本机查询,同时向azure-cosmosdb发送查询?
EN

Stack Overflow用户
提问于 2019-09-12 13:06:45
回答 1查看 2K关注 0票数 0

连接到azure-cosmosdb,并能够启动默认查询,如findAll()findById(String Id)。但是我不能使用@Query注释编写原生查询,因为代码没有考虑它。总是在respository类/接口中考虑函数的名称。我需要一种方法来激发一个自定义或原生查询到azure-cosmos db。?!

尝试使用@Query注释。但不起作用。

  1. 列表findBySessionID(@Param("sessionID")字符串sessionID);
  2. @nativeQuery= true,value = "SELECT * FROM MonitoringSessions M M.sessionID M.sessionID :sessionID")列表findSessions(@Param("sessionID") String sessionID);

findBySessionID()正在按预期工作。findSessions()不起作用。在运行代码时出现了根错误。

由:org.springframework.data.mapping.PropertyReferenceException: No property findSessions found for type MonitoringSessions引起

EN

回答 1

Stack Overflow用户

发布于 2019-09-24 09:04:03

谢谢你的回应。我从下面的链接中得到了我想要的东西。信用归于链接页面的作者。

sql.html

公共班级计划{

代码语言:javascript
复制
 private final ExecutorService executorService;
 private final Scheduler scheduler;
 private AsyncDocumentClient client;

 private final String databaseName = "UniversityDatabase";
 private final String collectionId = "StudentCollection";

 private int numberOfDocuments;

 public Program() {
     // public constructor
     executorService = Executors.newFixedThreadPool(100);
     scheduler = Schedulers.from(executorService);
     client = new AsyncDocumentClient.Builder().withServiceEndpoint("uri")
             .withMasterKeyOrResourceToken("key")
             .withConnectionPolicy(ConnectionPolicy.GetDefault()).withConsistencyLevel(ConsistencyLevel.Eventual)
             .build();

 }

 public static void main(String[] args) throws InterruptedException, JSONException {
     FeedOptions options = new FeedOptions();
     // as this is a multi collection enable cross partition query
     options.setEnableCrossPartitionQuery(true);
     // note that setMaxItemCount sets the number of items to return in a single page
     // result
     options.setMaxItemCount(5);
     String sql = "SELECT TOP 5 s.studentAlias FROM coll s WHERE s.enrollmentYear = 2018 ORDER BY s.studentAlias";
     Program p = new Program();
     Observable<FeedResponse<Document>> documentQueryObservable = p.client
                     .queryDocuments("dbs/" + p.databaseName + "/colls/" + p.collectionId, sql, options);
     // observable to an iterator
     Iterator<FeedResponse<Document>> it = documentQueryObservable.toBlocking().getIterator();

     while (it.hasNext()) {
             FeedResponse<Document> page = it.next();
             List<Document> results = page.getResults();
             // here we iterate over all the items in the page result
             for (Object doc : results) {
                     System.out.println(doc);
             }
     }

 }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57907452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档