Example usage for com.mongodb DBCursor close

List of usage examples for com.mongodb DBCursor close

Introduction

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

Prototype

@Override
    public void close() 

Source Link

Usage

From source file:ConecteMongoDB.Consultas.java

public ArrayList<Hero> buscaSkill(String skill) {
    ArrayList<Hero> resultados = new ArrayList<>();

    DBCollection colecao = MongoConnection.getInstance().getDB().getCollection("heroesdata");

    DBCursor cursor;

    BasicDBObject query = new BasicDBObject("Abilities" + "." + skill, new BasicDBObject("$gt", 1));

    cursor = colecao.find(query).sort(new BasicDBObject("Abilities" + "." + "Title", -1));

    try {/*from  w w  w .  ja  v  a2 s.  c  o  m*/
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String nome = (String) obj.get("Title");
            String lore = (String) obj.get("Lore");
            String sider = (String) obj.get("Side");
            String url = (String) obj.get("Url");
            Hero novoHero = new Hero(nome, lore, sider, url);
            resultados.add(novoHero);
        }
    } finally {
        cursor.close();
    }

    return resultados;
}

From source file:course.BlogPostDAOImpl.java

License:Apache License

public List<DBObject> findByDateDescending(int limit) {
    List<DBObject> posts = null;
    DBCursor cursor = postsCollection.find().sort(new BasicDBObject().append("date", -1)).limit(limit);
    try {// w w  w.  j a  va  2s  . co m
        posts = cursor.toArray();
    } catch (Exception e) {
        System.out.print(e.getLocalizedMessage());
    } finally {
        cursor.close();
    }
    return posts;
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public HashSet getDBpediaHypernyms(String entityTitle, String lang) throws UnknownHostException {

    HashSet hypernymsList = new HashSet();
    try {//from   ww w  . ja  v a2s. c  o  m
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("types",
                new BasicDBObject().append("$elemMatch", new BasicDBObject().append("origin", "dbpedia")));

        BasicDBList dbTypesList;
        switch (lang) {
        case "en":
            String uriEn = "http://dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            queryObj.append("uri", uriEn);
            DBCursor cursorEN = MongoDBClient.getDBInstance().getCollection("en_entities_dbpedia")
                    .find(queryObj);

            while (cursorEN.hasNext()) {
                DBObject resObj = cursorEN.next();
                if (resObj != null) {

                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {
                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");
                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorEN.close();

            return hypernymsList;

        case "de":

            String uriDe = "http://de.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", uriDe);
            DBCursor cursorDE = MongoDBClient.getDBInstance().getCollection("de_entities_dbpedia")
                    .find(queryObj);

            while (cursorDE.hasNext()) {
                DBObject resObj = cursorDE.next();
                if (resObj != null) {
                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {

                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");
                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorDE.close();

            return hypernymsList;

        case "nl":

            String uriNl = "http://nl.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", uriNl);
            DBCursor cursorNL = MongoDBClient.getDBInstance().getCollection("nl_entities_dbpedia")
                    .find(queryObj);

            while (cursorNL.hasNext()) {
                DBObject resObj = cursorNL.next();
                if (resObj != null) {
                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {

                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");

                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorNL.close();

            return hypernymsList;

        default:
            return hypernymsList;
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(THDWorker.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hypernymsList;
}

From source file:DataBase.JavaMongoDB.java

public ArrayList<DBObject> Consultar(BasicDBObject Consulta, boolean lista) {
    long time_start, time_end;
    time_start = System.currentTimeMillis();
    DBCursor cur = col.find(Consulta);
    time_end = System.currentTimeMillis();
    time = (time_end - time_start);/*from   w w  w  . j a  va2 s.c  o  m*/

    if (lista) {
        ArrayList<DBObject> Lista = new ArrayList<>();
        // DBObject a;
        for (int i = 0; i < cur.count(); i++) {
            System.out.println("Mongo conulat" + time);
            DBObject a = cur.next();
            Lista.add(a);
        }
        cur.close();
        return Lista;
    } else {
        return null;
    }

}

From source file:datapreparation.MongoStatistics.java

public void usersToUrls() {
    // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
    MongoClient mongoClient;/*from  w  ww.  j  a v a2s . c  om*/

    try {
        mongoClient = new MongoClient("localhost");

        //use database
        DB db = mongoClient.getDB("users");

        //get collection
        DBCollection coll = db.getCollection("urls");

        //iterate with a cursor
        BasicDBObject query = new BasicDBObject("source", new BasicDBObject("$exists", true));

        DBCursor cursor = coll.find(query);

        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("Users_To_Urls.txt"));
            writer.write("url,user,time\n");

            while (cursor.hasNext()) {
                BasicDBObject tweet = (BasicDBObject) cursor.next();

                String time = tweet.get("created_at").toString();
                String user = tweet.get("from_user").toString();
                String urls[] = tweet.get("source").toString().replaceAll("&quot", "").split(";");

                for (String url : urls) {
                    if (url.matches("http.*")) {
                        //The user posted one link, write it in the file!
                        writer.write(url + "," + user + "," + time + "\n");
                    }
                }
            }
        } finally {
            cursor.close();
            mongoClient.close();
            writer.close();
        }
    } catch (IOException ex) {
        System.out.println("Something's Wrong! " + ex);
    }
}

From source file:de.otto.mongodb.profiler.op.OpProfileDataFetcher.java

License:Apache License

private DBCursor getCursor() {

    synchronized (cursorMutex) {

        // Close stale cursor
        if (cursor != null && cursor.getCursorId() == 0L) {
            cursor.close();
            cursor = null;/* w  w w .ja  v a 2  s  . c  o  m*/
        }

        // Create new cursor
        if (cursor == null && db.collectionExists(COLLECTION)) {

            if (lastTs == null) {
                lastTs = DateTime.now(DateTimeZone.UTC);
            }

            final DBCollection collection = db.getCollection(COLLECTION);
            final DBObject query = QueryBuilder.start()
                    .and(QueryBuilder.start("ns").notEquals(collection.getFullName()).get(),
                            QueryBuilder.start("ts").greaterThan(lastTs.toDate()).get())
                    .get();
            final DBObject sortBy = new BasicDBObject("$natural", 1);
            final DBCursor cursor = collection.find(query).sort(sortBy).batchSize(100)
                    .addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA);
            this.cursor = cursor;
        }
    }

    return cursor;
}

From source file:de.uni_koeln.spinfo.maalr.login.UserInfoDB.java

License:Apache License

boolean userExists(String login) {
    BasicDBObject obj = new BasicDBObject();
    obj.put(Constants.Users.LOGIN, login);
    DBCursor cursor = userCollection.find(obj);
    boolean hasNext = cursor.hasNext();
    cursor.close();
    return hasNext;
}

From source file:de.uni_koeln.spinfo.maalr.login.UserInfoDB.java

License:Apache License

private MaalrUserInfo findByDbObject(BasicDBObject obj) {
    DBCursor cursor = userCollection.find(obj);
    if (!cursor.hasNext())
        return null;
    MaalrUserInfo toReturn = new MaalrUserInfo(cursor.next());
    cursor.close();
    return toReturn;
}

From source file:de.uni_koeln.spinfo.maalr.login.UserInfoDB.java

License:Apache License

List<MaalrUserInfo> getAllUsers(Role role, String text, String sortColumn, boolean sortAscending, int from,
        int length) {
    BasicDBObject query = new BasicDBObject();
    Pattern pattern = Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE);
    if (role != null) {
        query.put(Constants.Users.ROLE, role.toString());
    }/*ww w.jav a 2 s .com*/
    if (text != null && text.trim().length() > 0) {
        BasicDBList attributes = new BasicDBList();
        DBObject firstName = new BasicDBObject();
        firstName.put(Constants.Users.FIRSTNAME, pattern);
        attributes.add(firstName);
        DBObject lastName = new BasicDBObject();
        lastName.put(Constants.Users.LASTNAME, pattern);
        attributes.add(lastName);
        DBObject login = new BasicDBObject();
        login.put(Constants.Users.LOGIN, pattern);
        attributes.add(login);
        query.append("$or", attributes);
    }
    DBCursor cursor = userCollection.find(query);
    if (sortColumn != null) {
        BasicDBObject sort = new BasicDBObject();
        sort.put(sortColumn, sortAscending ? 1 : -1);
        cursor.sort(sort);
    }
    cursor = cursor.skip(from).limit(length);
    List<MaalrUserInfo> all = new ArrayList<MaalrUserInfo>();
    while (cursor.hasNext()) {
        DBObject o = cursor.next();
        MaalrUserInfo user = new MaalrUserInfo(o);
        all.add(user);
    }
    cursor.close();
    return all;
}

From source file:de.uni_koeln.spinfo.maalr.mongo.core.Database.java

License:Apache License

public Iterator<LexEntry> getEntries() {
    final DBCursor cursor = entryCollection.find();
    Iterator<LexEntry> entryIterator = new Iterator<LexEntry>() {

        @Override/*from w w  w  .  ja v  a  2 s .co m*/
        public boolean hasNext() {
            if (!cursor.hasNext()) {
                cursor.close();
                return false;
            }
            return cursor.hasNext();
        }

        @Override
        public LexEntry next() {
            return Converter.convertToLexEntry(cursor.next());
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub

        }

    };
    return entryIterator;
}