Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:MenuProb.java

public static void getProbability() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Probability", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {

        List<String> conv = (List<String>) doc.get("Probability");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            ProbTextArea.append(s + "\n");

    }/*  ww  w.  j  a va  2 s .  c o  m*/
}

From source file:MashUp.java

public MashUp() {
    try {/*from w  ww.j  a  va  2s.com*/
        mongo = new MongoClient("localhost", 27017);
    } catch (UnknownHostException ex) {
        Logger.getLogger(MashUp.class.getName()).log(Level.SEVERE, null, ex);
    }
    db1 = mongo.getDB("API");
    db2 = mongo.getDB("MashUp");
    table1 = db1.getCollection("APIS");
    table2 = db2.getCollection("MashUps");
}

From source file:admin_change_Emp.java

private void update_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_update_buttonActionPerformed
    // TODO add your handling code here:

    try {//  ww w.j  a v  a2  s .  com
        userid = userid_field.getText();
        int flag = 0;
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("employee_info");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("_id", userid);

        DBCursor cursor = collection.find(query, field);
        while (cursor.hasNext()) {
            DBObject obj = (DBObject) cursor.next();
            userid_db = obj.get("_id").toString();

            if (userid.equals(userid_db)) {

                flag = 1;
                setVisible(false);
                new admin_update_Emp(userid).setVisible(true);
                break;
            }
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "UserID invalid");
        }

    } catch (Exception e) {

    }

}

From source file:admin_change_Emp.java

private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_buttonActionPerformed
    // TODO add your handling code here:

    try {//  www . java2 s  . c  o m
        userid = userid_field.getText();
        int flag = 0;

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("employee_info");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("_id", userid);

        DBCursor cursor = collection.find(query, field);
        while (cursor.hasNext()) {
            DBObject obj = (DBObject) cursor.next();
            userid_db = obj.get("_id").toString();

            if (userid.equals(userid_db)) {

                flag = 1;

                BasicDBObject deleteObject = new BasicDBObject();
                deleteObject.put("_id", userid);
                DBCursor findQuery = collection.find(deleteObject);
                collection.remove(deleteObject);

                JOptionPane.showMessageDialog(null, "Employee Entry Deleted !!!");

                setVisible(false);
                new Admin_rights().setVisible(true);
                break;
            }
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "UserID invalid");
        }
    } catch (Exception e) {

    }
}

From source file:fList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// ww w .ja  v  a2s .co  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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        DBCursor cursor = User.find();

        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            String s = result.get("username").toString();
            if (!s.equals(username)) {
                response.getWriter().write(s);
                response.getWriter().write(" ");
            }

        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:tweetList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w w w . ja v  a2s.  co 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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");
        DBCollection Tweet = db.getCollection("Tweet");

        BasicDBObject query = new BasicDBObject("username", username);
        DBCursor curssc = User.find(query);
        // out.println(curssc.size());
        DBObject result = curssc.next();
        ArrayList<String> arr2 = new ArrayList();
        arr2 = (ArrayList<String>) result.get("tweet_id");

        ArrayList<String> e = new ArrayList();
        e = (ArrayList<String>) result.get("following_username");
        ArrayList<String> texts = new ArrayList();
        //out.println(e.size());
        //out.println(e.get(0));

        if (e.size() > 0 || arr2.size() > 0) {
            for (int i = e.size() - 1; i >= 0; i--) {
                BasicDBObject query2 = new BasicDBObject("username", e.get(i));
                DBCursor curssc2 = User.find(query2);
                if (curssc2.size() == 1) {
                    DBObject res = curssc2.next();
                    ArrayList<String> arr = new ArrayList();
                    arr = (ArrayList<String>) res.get("tweet_id");

                    texts.addAll(arr);

                }

            }
            texts.addAll(arr2);
            Collections.sort(texts);
            for (int j = texts.size() - 1; j >= 0; j--) {
                BasicDBObject query3 = new BasicDBObject("tweet_id", texts.get(j));
                DBCursor curssc3 = Tweet.find(query3);
                DBObject result1 = curssc3.next();
                response.getWriter().write(result1.get("username").toString());
                response.getWriter().write(":  ");
                response.getWriter().write(result1.get("tweet_text").toString());
                response.getWriter().write("\n");
            }

        } else {
            response.getWriter().write("No tweet");
        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:filterFriends.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w ww .j  av  a2 s.com
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    String username = request.getParameter("username");
    String filtered_name = request.getParameter("filtered");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        DBCursor cursor = User.find();

        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            String s = result.get("username").toString();
            if (s.equals(filtered_name)) {
                response.getWriter().write(s);
                response.getWriter().write(" ");
            }

        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:MenuDefAct.java

public static void getActions() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {

        List<String> conv = (List<String>) doc.get("Actions");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            ActTextArea.append(s + "\n");

    }/*from ww  w  .j  a v a  2 s. c  om*/
}

From source file:UnitTest3.java

License:Open Source License

public static int testgridfs() {

    long time1;/*from  w w  w. j  av a 2s  .c  o  m*/
    long time2;
    long time3;
    long time4;

    try {

        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("JFileDB");

        // TODO JFileMetaDataTable should be in MetaDB database
        DBCollection collection = db.getCollection("JFileMetaDataTable");

        String newFileName = "com.dilmus.scabi.testdata.in.App.class";

        File jFile = new File("/home/anees/workspace/testdata/in/App.class");

        // create a JFileTable namespace
        GridFS gfsj = new GridFS(db, "JFileTable");

        // get file from local drive
        GridFSInputFile gfsFile = gfsj.createFile(jFile);

        // set a new filename for identify purpose
        gfsFile.setFilename(newFileName);
        gfsFile.setContentType("class"); // jar, zip, war
        // save the image file into mongoDB
        gfsFile.save();

        // Let's create a new JSON document with some "metadata" information
        BasicDBObject info = new BasicDBObject();
        info.put("DBHost", "localhost");
        info.put("DBPort", "27017");
        info.put("JFileName", newFileName);
        info.put("JFileID", gfsFile.getId());
        info.put("JFileMD5", gfsFile.getMD5());
        collection.insert(info, WriteConcern.ACKNOWLEDGED);

        // print the result
        DBCursor cursor = gfsj.getFileList();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        DBCursor cursor2 = collection.find();

        while (cursor2.hasNext()) {
            System.out.println(cursor2.next());
        }

        // get file by it's filename
        GridFSDBFile jForOutput = gfsj.findOne(newFileName);

        // save it into a new image file
        jForOutput.writeTo("/home/anees/workspace/testdata/out/AppOut.class");

        // remove the file from mongoDB
        // gfsj.remove(gfsj.findOne(newFileName));

        System.out.println("Done");
        mongo.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return 0;
}

From source file:UnitTest3.java

License:Open Source License

public static void testgridfs2() throws FileNotFoundException, IOException {

    long time1;/*ww  w . ja  v a 2  s. c om*/
    long time2;
    long time3;
    long time4;
    String fileName = "makezip3.rar";

    MongoClient mongo = new MongoClient("localhost", 27017);
    //DB db = mongo.getDB("JFileDB");
    MongoDatabase db = mongo.getDatabase("JFileDB");
    GridFSBucket gridFSBucket = GridFSBuckets.create(db);

    // Get the input stream
    time1 = System.currentTimeMillis();
    InputStream streamToUploadFrom = new FileInputStream("/home/anees/workspace/testdata/in/" + fileName);

    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024 * 1024)
            .metadata(new Document("type", "class"));

    ObjectId fileId = gridFSBucket.uploadFromStream(fileName, streamToUploadFrom, options);
    streamToUploadFrom.close();
    time2 = System.currentTimeMillis();

    time3 = System.currentTimeMillis();
    Date date = new Date();

    FileOutputStream streamToDownloadTo = new FileOutputStream(
            "/home/anees/workspace/testdata/out/" + fileName + "_" + date.toString());
    // latest file with same name in DB
    GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(-1);
    //original file with same file name in DB GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0);

    //downloadOptions.
    gridFSBucket.downloadToStreamByName(fileName, streamToDownloadTo, downloadOptions);
    streamToDownloadTo.close();
    time4 = System.currentTimeMillis();

    System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());
    System.out.println("Upload time taken : time2 - time1 : " + (time2 - time1));
    System.out.println("Download time taken : time4 - time3 : " + (time4 - time3));

    /*
     Set the revision of the file to retrieve.
            
    Revision numbers are defined as follows:
            
    0 = the original stored file
    1 = the first revision
    2 = the second revision
    etc..
    -2 = the second most recent revision
    -1 = the most recent revision
            
     */

    mongo.close();

}