org.bson.types.BSONTimestamp Java Examples

The following examples show how to use org.bson.types.BSONTimestamp. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatabaseReader.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the query to execute on the collection.
 *
 * @return the query
 */
private Bson createQuery() {
    // timestamps are used as offsets, saved as a concatenation of seconds and order
	Integer timestamp = 0;
    Integer order = 0;
	if(!start.equals("0")){    	
 	final String[] splitted = start.split("_");
 	timestamp = Integer.valueOf(splitted[0]);
     order = Integer.valueOf(splitted[1]);
	}
	
    Bson query = Filters.and(
            Filters.exists("fromMigrate", false),
            Filters.gt("ts", new BSONTimestamp(timestamp, order)),
            Filters.or(
                    Filters.eq("op", "i"),
                    Filters.eq("op", "u"),
                    Filters.eq("op", "d")
            ),
            Filters.eq("ns", db)
    );

    return query;
}
 
Example #2
Source File: MongodbInputDiscoverFieldsImpl.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
protected static int mongoToKettleType( Object fieldValue ) {
  if ( fieldValue == null ) {
    return ValueMetaInterface.TYPE_STRING;
  }

  if ( fieldValue instanceof Symbol || fieldValue instanceof String || fieldValue instanceof Code
        || fieldValue instanceof ObjectId || fieldValue instanceof MinKey || fieldValue instanceof MaxKey ) {
    return ValueMetaInterface.TYPE_STRING;
  } else if ( fieldValue instanceof Date ) {
    return ValueMetaInterface.TYPE_DATE;
  } else if ( fieldValue instanceof Number ) {
    // try to parse as an Integer
    try {
      Integer.parseInt( fieldValue.toString() );
      return ValueMetaInterface.TYPE_INTEGER;
    } catch ( NumberFormatException e ) {
      return ValueMetaInterface.TYPE_NUMBER;
    }
  } else if ( fieldValue instanceof Binary ) {
    return ValueMetaInterface.TYPE_BINARY;
  } else if ( fieldValue instanceof BSONTimestamp ) {
    return ValueMetaInterface.TYPE_INTEGER;
  }

  return ValueMetaInterface.TYPE_STRING;
}
 
Example #3
Source File: Tailer.java    From zerowing with MIT License 6 votes vote down vote up
private DBCursor createCursor() {
  DBCollection oplog = _mongo.getDB("local").getCollection("oplog.rs");
  BSONTimestamp startingTimestamp = getStartingTimestamp();

  DBCursor cursor;
  if (startingTimestamp == null) {
    log.info("Tailing the oplog from the beginning...");
    cursor = oplog.find();
  } else {
    log.info("Tailing the oplog from " + startingTimestamp);
    BasicDBObject query = new BasicDBObject("ts", new BasicDBObject("$gt", startingTimestamp));
    cursor = oplog.find(query);
    cursor.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
  }

  cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
  cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
  cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);

  return cursor;
}
 
Example #4
Source File: Tailer.java    From zerowing with MIT License 6 votes vote down vote up
private BSONTimestamp getStartingTimestamp() {
  Get get = new Get(_tailerID);

  Result res;
  try {
    res = _stateTable.get(get);
  } catch (IOException e) {
    log.error("Failed to get a starting timestamp for tailer ID: " + _tailerID);
    return null;
  }

  if (res.isEmpty()) {
    if(ConfigUtil.getSkipBacklog(_conf)) return new BSONTimestamp((int) (System.currentTimeMillis() / 1000L), 0);
    return null;
  } else {
    byte[] raw_optime = res.getValue(STATE_TABLE_COL_FAMILY, STATE_TABLE_COL_QUALIFIER_OPTIME);
    byte[] raw_inc = res.getValue(STATE_TABLE_COL_FAMILY, STATE_TABLE_COL_QUALIFIER_INC);

    _optime = Integer.parseInt(new String(raw_optime));
    _inc = Integer.parseInt(new String(raw_inc));
    _optimeSet = true;
    return new BSONTimestamp(_optime, _inc);
  }
}
 
Example #5
Source File: Tailer.java    From zerowing with MIT License 5 votes vote down vote up
private void updateOptime(BasicDBObject doc) {
  BSONTimestamp ts = (BSONTimestamp) doc.get("ts");
  int optime = ts.getTime(), inc = ts.getInc();

  // only checkpoint every 60 seconds
  if (!_optimeSet || (optime - _optime) >= 60) {
    _optime = optime;
    _inc = inc;
    _optimeSet = true;

    log.info("optime: " + _optime);
    saveOptime();
  }
}