【问题标题】:Tailable Cursor example in Java using 3.0 driver?使用 3.0 驱动程序的 Java 中的 Tailable Cursor 示例?
【发布时间】:2015-05-04 11:09:26
【问题描述】:

有人可以提供一个完整的 Java 可尾光标示例吗?我正在使用 3.0 驱动程序,所有示例似乎都是 2.x。我的类路径中只有 mongo-java-driver-3.0.0.jar。我想获取插入到我的上限集合中的所有文档。

//this does not work...
MongoCollection<BasicDBObject> col = database.getCollection(colName, BasicDBObject.class);
DBCursor cur = col.find().sort(new BasicDBObject("$natural", 1))
.addOption(Bytes.QUERYOPTION_TAILABLE)
.addOption(Bytes.QUERYOPTION_AWAITDATA);


// And this does not work...
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
builder.add("messageType","STATUS_REQUEST");
DBObject searchQuery = builder.get();
DBObject sortBy = BasicDBObjectBuilder.start("$natural", 1).get();
BasicDBObjectBuilder builderForFields = BasicDBObjectBuilder.start();
DBObject fields = builderForFields.get();
DBCursor cursor = new DBCursor(col, searchQuery, fields, ReadPreference.primary()  );
cursor.sort(sortBy);
cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
cursor.addOption(Bytes.QUERYOPTION_TAILABLE);

//this does work but only returns the messageNumber field. I need the doc.
  MongoCursor<Long> c = database.getCollection(colName).distinct("messageNumber", Long.class).iterator();

我看到 MongoCursor 接口是在 3.0 中添加的。它有什么用?它会取代 DBCursor 吗?

非常感谢

【问题讨论】:

    标签: java mongodb


    【解决方案1】:

    聚会有点晚了,但万一你还需要帮助:

    find(query).projection(fields).cursorType(CursorType.TailableAwait).iterator();
    

    该代码适用于MongoCollection 类。

    CursorType 是一个枚举,它具有以下值:

    Tailable
    TailableAwait
    

    对应旧的 DBCursor addOption Bytes 类型:

    Bytes.QUERYOPTION_TAILABLE
    Bytes.QUERYOPTION_AWAITDATA
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      这就是您可能正在寻找的 - MongoDB 3.0.* 中的事件流式传输,使用新的 api 即 3.0.2

      Document query = new Document(); \\here use { indexedField: { $gt: <lastvalue> } index is not used(except for auto deleting documents) but created in capped collection
      Document projection = new Document();
      MongoCursor<Document> cursor= mongocollection.find(query).projection(projection).cursorType(CursorType.TailableAwait).iterator();  //add .noCursorTimeout(true) here to avoid timeout if you are working on big data
      
      while (cursor.hasNext()){
          Document doc = cursor.next();
          //do what you want with doc
      }
      

      这样 mongo 光标将检查上限集合中的新条目

      【讨论】:

      • 光标是否超时?
      • @user320550 有默认超时。为了避免超时,将其添加到游标 - .noCursorTimeout(true) 所以现在 MongoCursor cursor= mongocollection.find(query).projection(projection).noCursorTimeout(true) .cursorType(CursorType.TailableAwait).iterator() ;
      • 谢谢。您是如何实际实施的?您是否有一个单独的线程继续运行此代码以查找 mongo oplog 的任何新条目?
      【解决方案3】:

      在最后一行中,将 .distinct("messageNumber", Long.class) 替换为 .find()

      distinct(String fieldName, Class&lt;TResult&gt; resultClass) 仅返回您请求的一个字段的唯一值。

      find() 返回集合的所有文档及其所有字段。

      【讨论】:

      • 这似乎不能让它变得可尾。在 v3 中是如何完成的?之前有 DBCursor.addOption(...)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多