com.mongodb.client.MapReduceIterable Java Examples

The following examples show how to use com.mongodb.client.MapReduceIterable. 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: MDbOperation.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps, 
         boolean includeMetaDataSearchLimit, boolean includeSortExpr )
 {
     if( includeMetaDataSearchLimit )
     {
         Integer searchLimit = getModel().getEffectiveMDSearchLimit( queryProps ); 
         if( searchLimit > 0 )
{
             // Apply to FindIterable or MapReduceIterable
	if ( mongoIterable instanceof FindIterable )
	{
		FindIterable<Document> findIterable = (FindIterable<Document>) mongoIterable;
		findIterable.limit( searchLimit.intValue( ) );
	}
	else if ( mongoIterable instanceof MapReduceIterable )
	{
		MapReduceIterable<Document> mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
		mapReduceIterable.limit( searchLimit.intValue( ) );
	}       
}
     }
     
     applyPropertiesToCursor( mongoIterable, queryProps, includeSortExpr );
 }
 
Example #2
Source File: MongoCollectionMapReduce.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	String map	= getNamedStringParam(argStruct, "map", null );
	if ( map == null )
		throwException(_session, "please specify a map");
	
	String reduce	= getNamedStringParam(argStruct, "reduce", null );
	if ( reduce == null )
		throwException(_session, "please specify a reduce");
	
	String outputcollection	= getNamedStringParam(argStruct, "outputcollection", null );
	if ( outputcollection == null )
		throwException(_session, "please specify a outputcollection");
	
	String action		= getNamedStringParam(argStruct, "type", "replace" ).toLowerCase();
	String finalize	= getNamedStringParam(argStruct, "finalize", null );
	cfData	query		= getNamedParam(argStruct, "query", null );
	
	try{
		MapReduceIterable<Document>	mi	= db.getCollection( collection ).mapReduce( map, reduce );
		
		if ( query != null )
			mi.filter( getDocument( query ) );
		
		if ( finalize != null )
			mi.finalizeFunction( finalize );
		
		mi.collectionName( outputcollection );
		mi.action( MapReduceAction.valueOf( action ) );
		
		
		// Kick start the map reduce
		mi.first();
		
		return cfBooleanData.TRUE;

	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example #3
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
   * Applies data set query properties and hints on DBCursor, except
   * for cursor limit.
   * @see #applyPropertiesToCursor(DBCursor,QueryProperties,boolean,boolean)
   */
  static void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps, 
          boolean includeSortExpr )
  {
BasicDBObject sortExprObj = null;
      if( includeSortExpr )   // normally done only when executing a query to get full result set
      {
          
          try
          {
              sortExprObj = queryProps.getSortExprAsParsedObject();
          }
          catch( OdaException ex )
          {
              // log warning and ignore
              DriverUtil.getLogger().log( Level.WARNING, 
                      Messages.bind( "Unable to parse the user-defined Sort Expression: {0}", queryProps.getSortExpr() ),  //$NON-NLS-1$
                      ex );
          }
         
      }

// Map it to correct iterable object
FindIterable<Document> findIterable = null;
AggregateIterable<Document> aggregateIterable = null;
MapReduceIterable<Document> mapReduceIterable = null;
if ( mongoIterable instanceof FindIterable )
{
	findIterable = (FindIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof AggregateIterable )
{
	aggregateIterable = (AggregateIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof MapReduceIterable )
{
	mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
}
if ( findIterable == null
		&& aggregateIterable == null && mapReduceIterable == null )
{
	// Unknown type, return
}

if ( findIterable != null )
{
	if ( sortExprObj != null )
		findIterable.sort( sortExprObj );

	if ( queryProps.getBatchSize( ) > 0 )
		findIterable.batchSize( queryProps.getBatchSize( ) );

	if ( queryProps.getNumDocsToSkip( ) > 0 )
		findIterable.skip( queryProps.getNumDocsToSkip( ) );

	if ( queryProps.isPartialResultsOk( ) )
		findIterable.partial( true );
	// TODO: Remove hint from the UI
	// TODO: add Time out in the UI
	/*
	 * // hint is deprecated in 3.2 DBObject hintObj =
	 * queryProps.getIndexHintsAsParsedObject(); String hintValue =
	 * queryProps.getIndexHints(); if( hintObj != null )
	 * rowsCursor.hint( hintObj ); else // try to pass the hint string
	 * value as is { String hintValue = queryProps.getIndexHints(); if(
	 * ! hintValue.isEmpty() ) rowsCursor.hint( hintValue ); }
	 * findIterable.maxTime(Bytes.QUERYOPTION_NOTIMEOUT, arg1) if(
	 * queryProps.hasNoTimeOut() ) rowsCursor.addOption(
	 * Bytes.QUERYOPTION_NOTIMEOUT );
	 */
}
if ( aggregateIterable != null )
{
	if ( queryProps.getBatchSize( ) > 0 )
		aggregateIterable.batchSize( queryProps.getBatchSize( ) );

}
if ( mapReduceIterable != null )
{
	if ( sortExprObj != null )
		mapReduceIterable.sort( sortExprObj );

	if ( queryProps.getBatchSize( ) > 0 )
		mapReduceIterable.batchSize( queryProps.getBatchSize( ) );
}
  }
 
Example #4
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
static MapReduceIterable<Document> callMapReduceCmd( MongoCollection<Document> mongoCollection,
          QueryProperties queryProps ) throws OdaException
  {
      if( ! queryProps.hasMapReduceCommand() )
          return null;
      DBObject command = queryProps.getOperationExprAsParsedObject( false );
      if( command == null )
          return null;
      
if ( !( command instanceof BasicDBObject ) )
{
	throw new OdaException( Messages.bind(
			"Unexpected data type ({0}) in Selected Fields property value in MapReduce command",
			command.getClass( ).getSimpleName( ) ) );
}
      
String mapFunction = null;
String reduceFunction = null;
Object object = command.get( QueryModel.MAP_REDUCE_MAP_FUNCTION );
if ( object instanceof String )
{
	mapFunction = (String) object;
}
else
{
	throw new OdaException(
			Messages.bind( "Unexpected data type ({0}) in {1} function",
					command.getClass( ).getSimpleName( ),
					QueryModel.MAP_REDUCE_MAP_FUNCTION ) );
}
object = command.get( QueryModel.MAP_REDUCE_REDUCE_FUNCTION );
if ( object instanceof String )
{
	reduceFunction = (String) object;
}
else
{
	throw new OdaException(
			Messages.bind( Messages.driverUtil_invalidExpr,
					command.getClass( ).getSimpleName( )
							+ " in "
							+ QueryModel.MAP_REDUCE_REDUCE_FUNCTION ) );
}

      // mapReduce command's optional "limit" parameter applies to the number 
      // of documents in the *input* collection, and thus cannot be used to apply
      // the searchLimit property defined for data set
      
      // execute the mapreduce command
      
      try
      {
	MapReduceIterable<Document> mapReduceIterable = mongoCollection
			.mapReduce( mapFunction, reduceFunction );

	object = command.get( "finalize" );
	String finalizeFunction = null;
	if ( object != null )
	{
		if ( object instanceof String )
		{
			finalizeFunction = (String) object;
		}
		else
		{
			throw new OdaException( Messages.bind(
					"Unexpected data type ({0}) in {1} function",
					command.getClass( ).getSimpleName( ),
					"finalize" ) );
		}
	}
	if ( finalizeFunction != null )
	{
		mapReduceIterable = mapReduceIterable
				.finalizeFunction( finalizeFunction );
	}
	return mapReduceIterable;
      }
      catch( RuntimeException ex )
      {
          OdaException odaEx = new OdaException( 
                  Messages.bind( Messages.mDbOp_mapReduceCmdFailed, queryProps.getOperationExpression() ) );
          odaEx.initCause( ex );
          throw odaEx;
      }                
  }
 
Example #5
Source File: MongoClientTemplet.java    From mongodb-orm with Apache License 2.0 4 votes vote down vote up
private <T> List<T> mapReduce(String statement, Object parameter, ResultHandler handler, ReadPreference readPreference) {
  if (logger.isDebugEnabled()) {
    logger.debug("Execute 'mapReduce' mongodb command. Statement '" + statement + "'.");
  }

  MapReduceConfig config = (MapReduceConfig) configuration.getStatement(statement);
  if (config == null) {
    throw new MongoDaoException(statement, "MapReduce statement id '" + statement + "' not found.");
  }

  String collection = config.getCollection();
  Script map = config.getMap();
  Script reduce = config.getReduce();
  NodeEntry field = config.getField();
  
  String m = ScriptUtils.fillScriptParams(map, parameter);
  String r = ScriptUtils.fillScriptParams(reduce, parameter);
  
  MongoDatabase db = getDatabase();
  MongoCollection<Document> coll = db.getCollection(collection).withReadPreference(readPreference);

  MapReduceIterable<Document> iterable = coll.mapReduce(m, r);
  
  List<Document> list = new ArrayList<Document>();
  MongoCursor<Document> iterator = iterable.iterator();
  while (iterator.hasNext()) {
    list.add(iterator.next());
  }

  if (logger.isDebugEnabled()) {
    logger.debug("Execute 'mapReduce' mongodb command. Result set '" + list + "'.");
  }

  if (handler != null) {
    handler.handleResult(new ResultContext() {
      @Override
      public Object getResultObject() {
        return list;
      }

      @Override
      public int getResultCount() {
        return list.size();
      }
    });
    return null;
  }

  List<T> result = new ArrayList<T>(list.size());
  for (Document doc : list) {
    T t = (T) helper.toResult(config.getNamespace(), field, doc);
    result.add(t);
  }
  return result;
}
 
Example #6
Source File: IMongoSession.java    From ymate-platform-v2 with Apache License 2.0 votes vote down vote up
<T extends IEntity, RESULT> MapReduceIterable<RESULT> mapReduce(Class<T> entity, Class<RESULT> resultClass, String mapFunction, String reduceFunction) throws Exception; 
Example #7
Source File: IMongoSession.java    From ymate-platform-v2 with Apache License 2.0 votes vote down vote up
<T extends IEntity> MapReduceIterable<Document> mapReduce(Class<T> entity, String mapFunction, String reduceFunction) throws Exception;