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:clustering.ClusteringArtistsTW.java

public static void main(String[] args) throws UnknownHostException {

    new Integer(5).doubleValue();

    ArrayList<ArtistTW> artArr = new ArrayList<ArtistTW>();

    DBHelper dbHelper = DBHelper.getInstance();
    DBCursor artists = dbHelper.findAllArtistsWithTW();
    while (artists.hasNext()) {
        DBObject currentArtist = artists.next();
        //System.out.println(currentArtist);            
        String twf = currentArtist.get("twitter_followers").toString();
        StringTokenizer st = new StringTokenizer(twf, ".");
        int twfint = Integer.parseInt(st.nextToken());
        System.out.println(twfint);
        ArtistTW artist = new ArtistTW((ObjectId) currentArtist.get("_id"), twfint);
        artArr.add(artist);/*from w ww  . j av  a  2  s .  c  om*/
    }

    Collections.sort(artArr);

    parse(artArr, 1);
    merge_clusters(6);
    print_clusters();

}

From source file:clustering.KMeans.java

public static void main(String[] args) throws UnknownHostException {
    if (args.length != 1) {
        System.out.println("Usage : KMeans <nrClusters>");
        System.exit(-1);//from  w  w  w  . ja  va2 s .c o  m
    }

    int kClusters = Integer.parseInt(args[0]);

    ArrayList<Artist> artists = new ArrayList<Artist>();
    DBHelper dbHelper = DBHelper.getInstance();
    DBCursor result = dbHelper.findArtistsWithFBandTW();

    while (result.hasNext()) {
        DBObject currentArtist = result.next();
        artists.add(Artist.fromDBObject(currentArtist));
    }

    //System.out.println(artists.size());
    KMeansPlusPlusClusterer<Artist> clusterer = new KMeansPlusPlusClusterer<Artist>(kClusters);
    List<CentroidCluster<Artist>> clusters = clusterer.cluster(artists);
    //System.out.println(clusters.size());
    dbHelper.emptyClusterCenters();

    for (CentroidCluster<Artist> cluster : clusters) {
        double[] center = cluster.getCenter().getPoint();
        ObjectId centerId = dbHelper.insertClusterCenter(center[0], center[1], center[2]);

        List<Artist> artC = cluster.getPoints();
        for (Artist artist : artC) {
            dbHelper.updateMatrixRowCluster(artist.getDBObject(), centerId);
            //System.out.print("("+artist.fb_likes+","+artist.twitter_followers+","+artist.album_count+") ");
        }
    }
}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFS.java

License:Apache License

public List<MyGridFSDBFile> find(DBObject query, DBObject sort) {
    List<MyGridFSDBFile> files = new ArrayList<MyGridFSDBFile>();

    DBCursor c = null;
    try {//from ww w . ja v a  2s.  co m
        c = _filesCollection.find(query);
        if (sort != null) {
            c.sort(sort);
        }
        while (c.hasNext()) {
            files.add(_fix(c.next()));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return files;
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

private Set<Integer> queryChunkMap(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put("storageKey", new ObjectId(storageKey));
    Set<Integer> result = new HashSet<Integer>();
    DBCollection chunkTable = db.getCollection(tableName + ".chunks");
    DBObject ref = new BasicDBObject();
    ref.put("files_id", new ObjectId(storageKey));
    DBObject keys = new BasicDBObject();
    keys.put("n", 1);
    DBCursor cursor = chunkTable.find(ref, keys);
    while (cursor.hasNext()) {
        DBObject o = cursor.next();//www  . j  av  a 2s .  c om
        result.add(Integer.parseInt(o.get("n").toString()));
    }
    return result;
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

public String queryMd5(String storageKey, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    DBObject query = new BasicDBObject();
    query.put("storageKey", new ObjectId(storageKey));
    DBCollection fileTable = db.getCollection(tableName + ".files");
    DBCursor cursor = fileTable.find(query);
    if (cursor.hasNext()) {
        DBObject o = cursor.next();//from  w  w  w. j a va2  s. c om
        return o.get("md5").toString();
    }
    return "";
}

From source file:co.edu.uniandes.cloud.simuladorcredito.persistencia.SecuenciaDAO.java

public Secuencia leer(String llave) {
    BasicDBObject query = new BasicDBObject("tabla", llave);
    DBCursor cursor = col.find(query);
    try {//from   w w  w.  ja v a2 s  .co m
        if (cursor.hasNext()) {
            Secuencia s = new Secuencia();
            DBObject obj = cursor.next();
            s.setTabla((String) obj.get("tabla"));
            s.setSecuencia((Long) obj.get("secuencia"));
            return s;
        } else {
            return null;
        }
    } finally {
        cursor.close();
    }
}

From source file:co.edu.uniandes.cloud.simuladorcredito.persistencia.SuperDAO.java

protected DBObject leerBD(String campo, Object valor) {
    BasicDBObject query = new BasicDBObject(campo, valor);
    DBCursor cursor = col.find(query);
    try {//ww w  .ja va  2  s  . c  om
        if (cursor.hasNext()) {
            return cursor.next();
        } else {
            return null;
        }
    } finally {
        cursor.close();
    }
}

From source file:co.edu.uniandes.cloud.simuladorcredito.persistencia.SuperDAO.java

protected List<DBObject> leerVariosBD(String campo, Object valor) {
    List<DBObject> lista = new ArrayList();
    BasicDBObject query = new BasicDBObject(campo, valor);
    DBCursor cursor = col.find(query);
    try {/*from w  w w. jav  a2s.c  om*/
        while (cursor.hasNext()) {
            lista.add(cursor.next());
        }
    } finally {
        cursor.close();
    }
    return lista;
}

From source file:co.edu.uniandes.csw.Arquidalgos.usuario.persistence._UsuarioPersistence.java

License:MIT License

@SuppressWarnings("unchecked")
public List<UsuarioDTO> getUsuarios() {
    //      Query q = entityManager.createQuery("select u from UsuarioEntity u");
    //      return UsuarioConverter.entity2PersistenceDTOList(q.getResultList());

    List listaUsuarios = new ArrayList();
    DBCollection coll = db.getCollection("UsuarioDTO");
    BasicDBObject query = new BasicDBObject();
    DBCursor cursor = coll.find(query);
    while (cursor.hasNext()) {
        DBObject dBObject = cursor.next();
        UsuarioDTO user = new UsuarioDTO();
        user.setName((String) dBObject.get("name"));
        user.setContrasena((String) dBObject.get("contrasena"));
        user.setEmail((String) dBObject.get("email"));
        user.setFacebookId((String) dBObject.get("facebookId"));

        //TODO imprimir usuarios
        listaUsuarios.add(user);/*from  w ww . j a  va2  s.c o m*/
    }
    cursor.close();
    return listaUsuarios;

}

From source file:com.aankam.servlet.MongoCrudServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w ww . ja  va  2s  . c  o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    MongoClientURI uri = new MongoClientURI("mongodb://Username:Pasword@ds061721.mongolab.com:61721/cs612");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

    response.setContentType("text/html;charset=UTF-8");

    String serialNo = request.getParameter("serialNo");
    String name = request.getParameter("name");
    String emailId = request.getParameter("emailId");
    String operation = request.getParameter("operation");

    BasicDBObject customer;
    BasicDBObject updateQuery;
    BasicDBObject deleteQuery;
    BasicDBObject searchQuery;
    DBCollection customersCollection;
    customersCollection = db.getCollection("Customers");
    DBObject searchResult = null;
    boolean flag = false;
    DBCursor cursor;
    switch (operation) {
    case "create":
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        customersCollection.insert(customer);
        break;
    case "update":
        updateQuery = new BasicDBObject();
        updateQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(updateQuery);
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        if (cursor.hasNext()) {
            customersCollection.update(updateQuery, customer);
            flag = true;
        }

        break;
    case "delete":
        deleteQuery = new BasicDBObject();
        deleteQuery.append("serialNo", serialNo);
        customersCollection.remove(deleteQuery);
        break;
    case "search":
        searchQuery = new BasicDBObject();
        searchQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(searchQuery);
        while (cursor.hasNext()) {
            searchResult = cursor.next();
        }
        break;
    default:
        break;

    }
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MongoCrudServlet</title>");
        out.println("</head>");
        out.println("<body>");

        switch (operation) {
        case "create":
            out.println("<h3>Customer was successfully created.</h3>");
            break;
        case "update":
            if (flag == true) {
                out.println("<h3>Customer was successfully updated.</h3>");
            } else {
                out.println("<h3>Customer was not found to update.</h3>");
            }

            break;
        case "delete":
            out.println("<h3>Customer was successfully deleted.</h3>");
            break;
        case "search":
            if (searchResult != null) {
                out.println("<h3>The following customer was found:</h3>");
                out.println("<h4>Serial No :" + searchResult.get("serialNo") + "</h4>");
                out.println("<h4>Name :" + searchResult.get("name") + "</h4>");
                out.println("<h4>Email Id :" + searchResult.get("emailId") + "</h4>");
            } else {
                out.println("<h3>Customer not found.</h3>");
            }
            break;
        default:
            break;
        }
        out.println("<a href=\"index.html\">Back to Main Form</a>");
        out.println("</body>");
        out.println("</html>");
    }
}