Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb DBCursor hasNext.

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:com.gigaspaces.persistency.MongoClientConnector.java

License:Open Source License

public Collection<SpaceTypeDescriptor> loadMetadata() {

    DBCollection metadata = getCollection(METADATA_COLLECTION_NAME);

    DBCursor cursor = metadata.find(new BasicDBObject());

    while (cursor.hasNext()) {
        DBObject type = cursor.next();/*from   www.  ja v a 2 s .c o m*/

        Object b = type.get(TYPE_DESCRIPTOR_FIELD_NAME);

        readMetadata(b);
    }

    return getSortedTypes();
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

public List<MongoField> discoverFields(String db, String collection, String query, String fields,
        boolean isPipeline, int docsToSample) throws KettleException {
    DBCursor cursor = null;
    try {//  w w w .  ja  va  2s  .  co m
        int numDocsToSample = docsToSample;
        if (numDocsToSample < 1) {
            numDocsToSample = 100; // default
        }

        List<MongoField> discoveredFields = new ArrayList<MongoField>();
        Map<String, MongoField> fieldLookup = new HashMap<String, MongoField>();
        try {
            DB database = getDb(db);

            if (Const.isEmpty(collection)) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.NoCollectionSpecified")); //$NON-NLS-1$
            }
            DBCollection dbcollection = database.getCollection(collection);

            Iterator<DBObject> pipeSample = null;

            if (isPipeline) {
                pipeSample = setUpPipelineSample(query, numDocsToSample, dbcollection);
            } else {
                if (Const.isEmpty(query) && Const.isEmpty(fields)) {
                    cursor = dbcollection.find().limit(numDocsToSample);
                } else {
                    DBObject dbObject = (DBObject) JSON.parse(Const.isEmpty(query) ? "{}" //$NON-NLS-1$
                            : query);
                    DBObject dbObject2 = (DBObject) JSON.parse(fields);
                    cursor = dbcollection.find(dbObject, dbObject2).limit(numDocsToSample);
                }
            }

            int actualCount = 0;
            while (cursor != null ? cursor.hasNext() : pipeSample.hasNext()) {
                actualCount++;
                DBObject nextDoc = (cursor != null ? cursor.next() : pipeSample.next());
                docToFields(nextDoc, fieldLookup);
            }

            postProcessPaths(fieldLookup, discoveredFields, actualCount);

            return discoveredFields;
        } catch (Exception e) {
            throw new KettleException(e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (Exception ex) {
        if (ex instanceof KettleException) {
            throw (KettleException) ex;
        } else {
            throw new KettleException(
                    BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.UnableToDiscoverFields"), ex); //$NON-NLS-1$
        }
    }
}

From source file:com.github.niltz.maven.plugins.mongodb.MongoDBUpdateMojo.java

License:Open Source License

/**
 * {@inheritDoc}//  w w  w.  j  av a  2 s  .c  o  m
 */
@Override
public void executeInternal() throws MojoExecutionException, MojoFailureException {

    try {
        for (int i = 0; i < getDatabaseSettings().length; i++) {
            DatabaseSettings databaseSettings = getDatabaseSettings()[i];
            getLog().info("Updating '" + databaseSettings.getConnectionSettings().getDatabase() + "'");

            ConnectionSettings connectionSettings = databaseSettings.getConnectionSettings();
            Mongo mongo = openConnection(connectionSettings);
            DB db = getDatabase(connectionSettings, mongo);

            //
            // Get a list of currently applied updates
            //
            ArrayList<String> appliedUpdates = new ArrayList<String>();
            currentAppliedUpdatesCollection = db.getCollection(databaseSettings.getUpdatesCollection());
            DBCursor cursor = currentAppliedUpdatesCollection.find();
            while (cursor.hasNext()) {
                DBObject appliedupdate = cursor.next();
                appliedUpdates.add(
                        (String) appliedupdate.get(databaseSettings.getUpdatesCollectionScriptNameField()));
            }

            //
            // Execute the scripts in the directory ignoring the ones that
            // have been applied
            //
            executeScriptsInDirectory(connectionSettings, "update", db, appliedUpdates.toArray(new String[0]));
        }

    } catch (IOException ioe) {
        throw new MojoExecutionException("Error executing update scripts", ioe);
    }
}

From source file:com.google.api.ads.adwords.awreporting.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

@Override
public <T, V> List<T> get(Class<T> classT, String key, V value) {
    BasicDBObject query = new BasicDBObject();
    if (key != null && value != null) {
        query.put(key, value);/*from  ww  w  .  j  a  va 2 s.co m*/
    }

    DBCursor cur = getCollection(classT).find(query);
    List<T> list = new ArrayList<T>();
    while (cur.hasNext()) {
        DBObject dbObject = cur.next();
        list.add(gson.fromJson(com.mongodb.util.JSON.serialize(dbObject), classT));
    }

    return list;
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister
 *      #get(java.lang.Class, java.util.Map, int, int)
 *///from ww w .  j a v a2 s.c om
@Override
public <T, V> List<T> get(Class<T> t, Map<String, V> keyValueList, int numToSkip, int limit) {
    DBCollection dbcollection = getDBCollection(t, true);

    BasicDBObject query = new BasicDBObject();
    if (keyValueList != null) {
        for (String key : keyValueList.keySet()) {
            query.put(key, keyValueList.get(key));
        }
    }
    DBCursor cur = dbcollection.find(query);

    if (limit > 0) {
        cur.limit(limit);
    }
    if (numToSkip > 0) {
        cur.skip(numToSkip);
    }

    List<T> list = new ArrayList<T>();
    while (cur.hasNext()) {
        DBObject dbObject = cur.next();
        list.add(gson.fromJson(com.mongodb.util.JSON.serialize(dbObject), t));
    }
    return list;
}

From source file:com.google.api.services.samples.analytics.cmdline.CoreReportingApiReferenceSample.java

License:Apache License

private static void insertVisitAttributesData(GaData gaData, DBCollection collection, Date d,
        DBCollection centerMapping) throws JSONException {
    if (gaData.getTotalResults() > 0) {
        System.out.println("Data Table:" + collection);

        String[] columns = (VISIT_ATTRIBUTES_METRICS + "," + VISIT_ATTRIBUTES_DIMENSIONS).split(",");
        HashMap<String, Integer> columnLookUp = new HashMap<String, Integer>();
        List<ColumnHeaders> columnHeaders = gaData.getColumnHeaders();
        for (String column : columns) {
            for (int i = 0; i < columnHeaders.size(); i++) {
                if (columnHeaders.get(i).getName().equals(column)) {
                    columnLookUp.put(column, i);
                    break;
                }//www. ja  v a  2  s.c o  m
            }
        }

        if (!gaData.getContainsSampledData()) {
            for (List<String> rowValues : gaData.getRows()) {
                String demandBaseId = rowValues.get(columnLookUp.get("ga:dimension11"));
                String clientId = rowValues.get(columnLookUp.get("ga:dimension2"));
                String pagePath = rowValues.get(columnLookUp.get("ga:pagePath"));
                String source = rowValues.get(columnLookUp.get("ga:source"));
                String medium = rowValues.get(columnLookUp.get("ga:medium"));
                //                    String visits = rowValues.get(columnLookUp.get("ga:visits"));
                //                    String users = rowValues.get(columnLookUp.get("ga:users"));
                //                    String pageViews = rowValues.get(columnLookUp.get("ga:pageviews"));
                //                    String sessionDuration = rowValues.get(columnLookUp.get("ga:sessionDuration"));

                HashMap<Object, Object> map = new HashMap<Object, Object>();
                map.put("demandbase_sid", demandBaseId);
                map.put("clientId", clientId);
                String[] split = pagePath.split("\\?"); // remove all characters after the URL parameters
                String[] withoutMobileUrl = split[0].split("regus.com");
                String strippedPagePath = withoutMobileUrl[withoutMobileUrl.length - 1];

                String product = "", centerLookUp = "", centerId = "";
                String[] locations = strippedPagePath.split("locations/");
                if (locations.length > 1) {
                    int index = locations[1].indexOf("/");
                    product = locations[1].substring(0, index);
                    centerLookUp = locations[1].substring(index + 1);

                    HashMap<Object, Object> centerLookUpMap = new HashMap<Object, Object>();
                    centerLookUpMap.put("CentreURLName", centerLookUp);
                    BasicDBObject objectToRemove = new BasicDBObject(centerLookUpMap);
                    DBCursor cursor = centerMapping.find(objectToRemove);
                    if (cursor.hasNext()) {
                        centerId = cursor.next().get("CentreID").toString();
                    }

                }
                map.put("pagePath", strippedPagePath);
                map.put("source", source);
                map.put("medium", medium);
                map.put("product", product);
                map.put("centerId", centerId);
                //                    map.put("visits", visits);
                //                    map.put("users", users);
                //                    map.put("pageViews", pageViews);
                //                    map.put("sessionDuration", sessionDuration);
                map.put("date", new SimpleDateFormat("yyyy/MM/dd").format(d));
                BasicDBObject objectToInsert = new BasicDBObject(map);
                collection.insert(objectToInsert);
            }
        } else {
            System.out.println(" Excluding analytics data since it has sample data");
        }
    } else {
        System.out.println("No data");
    }
}

From source file:com.grallandco.mongodb.beersample.BeerServlet.java

License:Open Source License

/**
 * Handle the /beers action./*www  .  j av a 2 s . c  o  m*/
 *
 * @param request the HTTP request object.
 * @param response the HTTP response object.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
private void handleIndex(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    DBCursor cursor = db.getCollection("beer").find().sort(BasicDBObjectBuilder.start("name", 1).get())
            .limit(20);
    ArrayList<HashMap<String, String>> beers = new ArrayList<HashMap<String, String>>();
    while (cursor.hasNext()) {
        DBObject row = cursor.next();
        HashMap<String, String> beer = new HashMap<String, String>();
        beer.put("id", (String) row.get("_id"));
        beer.put("name", (String) row.get("name"));
        beer.put("brewery", (String) row.get("brewery_id"));
        beers.add(beer);
    }

    request.setAttribute("beers", beers);

    request.getRequestDispatcher("/WEB-INF/beers/index.jsp").forward(request, response);
}

From source file:com.grallandco.mongodb.beersample.BeerServlet.java

License:Open Source License

/**
 * Handle the /beers/search action.//  w  ww  .j a  va 2s  .co m
 *
 * @param request the HTTP request object.
 * @param response the HTTP response object.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
private void handleSearch(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String name = request.getParameter("value").toLowerCase();

    DBObject query = QueryBuilder.start().put("name")
            .regex(java.util.regex.Pattern.compile(name, Pattern.CASE_INSENSITIVE)).get();

    DBCursor cursor = db.getCollection("beer").find(query).sort(BasicDBObjectBuilder.start("name", 1).get())
            .limit(20);
    ArrayList<HashMap<String, String>> beers = new ArrayList<HashMap<String, String>>();
    while (cursor.hasNext()) {
        DBObject row = cursor.next();
        HashMap<String, String> beer = new HashMap<String, String>();
        beer.put("id", (String) row.get("_id"));
        beer.put("name", (String) row.get("name"));
        beer.put("brewery", (String) row.get("brewery_id"));
        beers.add(beer);
    }

    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    out.print(gson.toJson(beers));
    out.flush();
}

From source file:com.grallandco.mongodb.beersample.BreweryServlet.java

License:Open Source License

/**
 * Handle the /breweries action.//w  w w .  jav a  2  s  .c  o m
 *
 * @param request the HTTP request object.
 * @param response the HTTP response object.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
private void handleIndex(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    DBCursor cursor = db.getCollection("brewery").find().sort(BasicDBObjectBuilder.start("name", 1).get())
            .limit(20);
    ArrayList<HashMap<String, String>> breweries = new ArrayList<HashMap<String, String>>();
    while (cursor.hasNext()) {
        DBObject row = cursor.next();
        HashMap<String, String> brewery = new HashMap<String, String>();
        brewery.put("id", (String) row.get("_id"));
        brewery.put("name", (String) row.get("name"));
        breweries.add(brewery);
    }
    request.setAttribute("breweries", breweries);

    request.getRequestDispatcher("/WEB-INF/breweries/index.jsp").forward(request, response);
}

From source file:com.grallandco.mongodb.beersample.BreweryServlet.java

License:Open Source License

/**
 * Handle the /breweries/search action./*from  w  ww . ja v a2  s  .  c  om*/
 *
 * @param request the HTTP request object.
 * @param response the HTTP response object.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
private void handleSearch(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String name = request.getParameter("value").toLowerCase();

    DBObject query = QueryBuilder.start().put("name")
            .regex(java.util.regex.Pattern.compile(name, Pattern.CASE_INSENSITIVE)).get();

    DBCursor cursor = db.getCollection("brewery").find(query).sort(BasicDBObjectBuilder.start("name", 1).get())
            .limit(20);
    ArrayList<HashMap<String, String>> breweries = new ArrayList<HashMap<String, String>>();
    while (cursor.hasNext()) {
        DBObject row = cursor.next();
        HashMap<String, String> brewery = new HashMap<String, String>();
        brewery.put("id", (String) row.get("_id"));
        brewery.put("name", (String) row.get("name"));
        breweries.add(brewery);
    }

    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    out.print(gson.toJson(breweries));
    out.flush();
}