Example #1
0
    public static void main(String[] args) throws UnknownHostException {
        MongoClient client = new MongoClient();
        DB db = client.getDB("hw31");
        DBCollection collection = db.getCollection("students");

        // create our pipeline operations, first with the $match
        DBObject match = new BasicDBObject("$match", new BasicDBObject("scores.type", "homework") );

        // $unwind op
        //DBObject unwind = new BasicDBObject("$unwind", "$scores");

        // Now the $group operation
        DBObject groupFields = new BasicDBObject( "_id", "$student_id");
        //groupFields.put("score", new BasicDBObject( "$min", "scores.$score"));
        DBObject group = new BasicDBObject("$group", groupFields);
//
//        // $sort operation
//        DBObject sortFields = new BasicDBObject("_id", 1).append("score", 1);
//        DBObject sort = new BasicDBObject("$sort", sortFields);

        // run aggregation
        AggregationOutput output = collection.aggregate(match, group);

        for(Iterator<DBObject> i = output.results().iterator(); i.hasNext();) {
            DBObject result = i.next();
            System.out.println(result);
//            collection.remove(QueryBuilder.start("student_id").is(result.get("_id"))
//                    .and("score").is(result.get("score")).and("type").is("homework").get());
        }

        System.out.println(collection.count());
    }
Example #2
0
  public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();
    DB db = client.getDB("course");
    DBCollection lines = db.getCollection("dotNotationTest");
    lines.drop();
    Random rand = new Random();

    for (int i = 0; i < 10; i++) {
      lines.insert(
          new BasicDBObject("_id", i)
              .append(
                  "start",
                  new BasicDBObject("x", rand.nextInt(90) + 10).append("y", rand.nextInt(90) + 10))
              .append(
                  "end",
                  new BasicDBObject("x", rand.nextInt(90) + 10)
                      .append("y", rand.nextInt(90) + 10)));
    }

    QueryBuilder builder = QueryBuilder.start("start.x").greaterThan(50);

    DBCursor cursor =
        lines.find(builder.get(), new BasicDBObject("start.y", true).append("_id", false));

    try {
      while (cursor.hasNext()) {
        DBObject cur = cursor.next();
        System.out.println(cur);
      }
    } finally {
      cursor.close();
    }
  }
Example #3
0
  public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("findCriteriaTest");
    collection.drop();

    for (int i = 0; i < 10; i++) {
      collection.insert(
          new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100)));
    }

    QueryBuilder builder = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(90);
    // DBObject query = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gt",
    // 10).append("$lt", 90));

    System.out.println("\nCount:");
    long count = collection.count(builder.get());
    System.out.println(count);

    System.out.println("\nFind all:");
    DBCursor cursor = collection.find(builder.get());
    try {
      while (cursor.hasNext()) {
        DBObject cur = cursor.next();
        System.out.println(cur);
      }
    } finally {
      cursor.close();
    }
  }
Example #4
0
  @Override
  public String ActualizarJson(String nombreDB, String json, int clienteId)
      throws UnknownHostException {
    // TODO Auto-generated method stub

    if (!ExisteCliente(nombreDB, clienteId)) {
      //        	System.out.println("********************\n");
      //        	System.out.println("No existe el cliente, no se puede actualizar");
      //        	System.out.println("********************\n");
      return "No existe el cliente, no se puede actualizar";
    }
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);
    DBCollection collection = base.getCollection("Json");

    BasicDBObject document = new BasicDBObject();
    DBObject object = (DBObject) JSON.parse(json);
    document.put("id", clienteId);
    document.put("json", object);

    BasicDBObject query = new BasicDBObject().append("id", clienteId);

    collection.findAndModify(query, document);
    return "Cliente actualizado";
  }
  public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
    final DB db = client.getDB("course");

    Spark.get(
        new Route("/") {
          @Override
          public Object handle(Request request, Response response) {
            DBCollection users = db.getCollection("user");
            DBObject user = users.findOne();
            StringWriter writer = new StringWriter();
            Configuration configuration = new Configuration();
            configuration.setClassForTemplateLoading(
                HelloWorldMongoDBSparkFreemarkerStyle.class, "/");

            try {
              Template template = configuration.getTemplate("hello.ftl");
              template.process(user, writer);
            } catch (Exception e) {
              halt(100);
              e.printStackTrace();
            }

            return writer;
          }
        });
  }
Example #6
0
  public EMSMongoClient() throws IOException {
    Properties config = new Properties();

    InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");

    config.load(in);

    MongoClient client;

    List<ServerAddress> serverAddresses =
        Arrays.asList(
            new ServerAddress(
                config.getProperty("mongo_host"),
                Integer.parseInt(config.getProperty("mongo_port"))));

    if (config.containsKey("mongo_user") && config.containsKey("mongo_password")) {
      MongoCredential credential =
          MongoCredential.createMongoCRCredential(
              config.getProperty("mongo_user"),
              config.getProperty("mongo_db"),
              config.getProperty("mongo_password").toCharArray());

      client = new MongoClient(serverAddresses, Arrays.asList(credential));
    } else {
      client = new MongoClient(serverAddresses);
    }

    DB db = client.getDB(config.getProperty("mongo_db"));

    this.collection = db.getCollection("sessions");
  }
Example #7
0
  @Override
  public String IngresarJson(String nombreDB, String json, int clienteId)
      throws UnknownHostException {
    // TODO Auto-generated method stub
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);

    // Ya existe
    if (ExisteCliente(nombreDB, clienteId)) {
      //        	System.out.println("********************\n");
      //        	System.out.println("Ya existe cliente, error no se igresa el Json");
      //        	System.out.println("********************\n");
      return "Ya existe cliente, error no se igresa el Json";
    }

    // no existe el cliente
    DBCollection collection = base.getCollection("Json");
    BasicDBObject document = new BasicDBObject();
    document.put("id", clienteId);

    DBObject object = (DBObject) JSON.parse(json);
    document.put("json", object);
    // document.put("json",json);
    collection.insert(document);
    return "Ok";
  }
Example #8
0
  @Override
  public String Json(String nombreDB, int clienteId) throws UnknownHostException, JSONException {
    // TODO Auto-generated method stub
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);

    DBCollection collection = base.getCollection("Json");
    BasicDBObject query = new BasicDBObject();
    query.put("id", clienteId);
    DBCursor cursor = collection.find(query);

    if (cursor.size() == 0) {
      //        	System.out.println("********************\n");
      //        	System.out.println("No existe el cliente, no se puede ingresar json");
      //        	System.out.println("********************\n");

      return "No existe el cliente, no se puede ingresar json";
    }
    // Existe el cliente

    DBObject objeto = (DBObject) cursor.next();

    DBObject json = (DBObject) objeto.get("json");
    //  DBObject j = JSON.parse(json.toString());
    JSONObject aj = new JSONObject(json.toString());

    return aj.toString();
  }
Example #9
0
  public static void main(String[] args) throws UnknownHostException {
    System.out.println("Start Testing");

    MongoClient client = new MongoClient(new ServerAddress("192.168.0.14", 27017));
    DB db = client.getDB("course");
    DBCollection collection = db.getCollection("findTestFromJava");
    collection.drop();

    for (int i = 0; i < 10; i++) {
      collection.insert(new BasicDBObject("x", new Random().nextInt(100)));
    }
    System.out.println("Find one:");
    DBObject one = collection.findOne();
    System.out.println("one = " + one);
    System.out.println("\nFind all:");
    DBCursor cursor = collection.find();
    try {
      while (cursor.hasNext()) {
        DBObject cur = cursor.next();
        System.out.println("cursor = " + cur);
      }
    } finally {
      cursor.close();
    }
    System.out.println("\nFind Count:");
    long count = collection.count();
    System.out.println("count = " + count);
  }
Example #10
0
 public static void main(String args[]) throws ParseException, IOException {
   final String mongoURI = "mongodb://223.3.75.101:27017";
   MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURI));
   DB db = mongoClient.getDB("huawei");
   CFDAO cfDAO = new CFDAO(db);
   List<DBObject> list = cfDAO.getConversionFunnel("2014-08-10");
   System.out.println(list);
 }
Example #11
0
  public static void main(String[] args) throws UnknownHostException {

    MongoClient client = new MongoClient();
    DB database = client.getDB("school");
    DBCollection collection = database.getCollection("students");
    /*
     Hint/spoiler: With the new schema, this problem is a lot harder
     and that is sort of the point. One way is to find the lowest
     homework in code and then update the scores array with the low
     homework pruned. If you are struggling with the Node.js side of
     this, look at the .slice() operator, which can remove an element
     from an array in-place.
    */
    DBCursor cursor = collection.find();

    try {
      while (cursor.hasNext()) {
        BasicDBObject student = (BasicDBObject) cursor.next();

        int studentId = student.getInt("_id");
        String name = student.getString("name");
        BasicDBList scores = (BasicDBList) student.get("scores");
        System.out.printf("_id[%d], name[%s], scores%s %n", studentId, name, scores);

        DBObject scoreToRemove = null;
        double minScoreValue = 100.0;

        for (Object obj : scores) {
          BasicDBObject score = (BasicDBObject) obj;
          String type = score.getString("type");

          if (!"homework".equals(type)) {
            continue;
          }
          double curScoreValue = score.getDouble("score");
          System.out.printf("type[%s], current score value[%f] %n", type, curScoreValue);

          if (curScoreValue < minScoreValue) {
            scoreToRemove = score;
            minScoreValue = curScoreValue;
          }
        }
        System.out.printf("score to remove[%s] %n", scoreToRemove);

        if (scoreToRemove != null) {
          scores.remove(scoreToRemove);

          BasicDBObject query = new BasicDBObject("_id", studentId);
          BasicDBObject scoresUpdate =
              new BasicDBObject("$set", new BasicDBObject("scores", scores));
          WriteResult result = collection.update(query, scoresUpdate);
          System.out.printf("update count[%d] %n", result.getN());
        }
      }
    } finally {
      cursor.close();
    }
  }
Example #12
0
 @Override
 public void setupDatabase() {
   try {
     MongoClient mongoClient = new MongoClient(address, port);
     db = mongoClient.getDB(database);
   } catch (UnknownHostException e) {
     e.printStackTrace();
   }
 }
Example #13
0
  public boolean ExisteCliente(String nombreDB, int clienteId) throws UnknownHostException {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);

    DBCollection collection = base.getCollection("Json");
    BasicDBObject query = new BasicDBObject();
    query.put("id", clienteId);
    DBCursor cursor = collection.find(query);
    return cursor.size() > 0;
  }
  public static void main(String args[]) {

    // MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
    MongoClient mongoClient = new MongoClient(connectionString);
    MongoDatabase database = mongoClient.getDatabase("PROJ");

    MongoCollection<Document> collection = database.getCollection("proj");

    // *************************************************
    // permet de compter le nombre d'élément dans la base
    // **************************************************
    //        System.out.print(collection.count());

    // ************************************
    // permet d'afficher toutes les donnés
    // ************************************
    //        FindIterable<Document> iterable = collection.find();
    //        iterable.forEach(new Block<Document>() {
    //            @Override
    //            public void apply(final Document document) {
    //                System.out.println(document);
    //            }
    //        });

    // ************************************************************************
    // permet d'afficher les données ou le borrower est la republique d'albanie
    // ************************************************************************
    //        FindIterable<Document> iterable = collection.find(
    //                new Document("borrower", "REPUBLIC OF ALBANIA"));
    //        iterable.forEach(new Block<Document>() {
    //            @Override
    //            public void apply(final Document document) {
    //                System.out.println(document);
    //            }
    //        });

    // ****************************************************************
    // permet de trouver les marjor secteur energy and mining > 40%
    // ****************************************************************
    //        FindIterable<Document> iterable = collection.find(
    //                new Document("majorsector_percent.Name", "Energy and
    // mining").append("majorsector_percent.Percent",new Document("$lt", 40)));
    //        iterable.forEach(new Block<Document>() {
    //            @Override
    //            public void apply(final Document document) {
    //                System.out.println(document);
    //            }
    //        });
    // *****************************************************
    // permet d'ouvrir l'interface
    // ****************************************************/
    MainWindow page = new MainWindow();
  }
  @Override
  protected MongoClient createInstance() throws Exception {
    MongoClient mongo = initMongo();

    // 设定主从分离
    if (readSecondary) {
      mongo.setReadPreference(ReadPreference.secondaryPreferred());
    }
    // 设定写策略
    mongo.setWriteConcern(writeConcern);
    return mongo;
  }
Example #16
0
  public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();
    MongoDatabase mongoDatabase = client.getDatabase("course");
    MongoCollection<Document> collection = mongoDatabase.getCollection("findTest");

    collection.drop();

    // insert 10 documents with a random integer as the value of field "x"
    for (int i = 0; i < 10; i++) {
      collection.insertOne(new Document("x", new Random().nextInt(100)));
    }

    System.out.println("Find one:");
    Document one = collection.find().first();
    System.out.println(one);

    System.out.println("\nFind all: ");
    List<Document> all = collection.find().into(new ArrayList<Document>());
    System.out.println(all);

    System.out.println("\nFind all cursor: ");
    MongoCursor<Document> cursor = collection.find().iterator();
    try {

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

    } finally {
      cursor.close();
    }

    //        System.out.println("\nFind all: ");
    //        DBCursor cursor = collection.find();
    //        try {
    //          while (cursor.hasNext()) {
    //              DBObject cur = cursor.next();
    //              System.out.println(cur);
    //          }
    //        } finally {
    //            cursor.close();
    //        }
    //
    System.out.println("\nCount:");
    long count = collection.count();
    System.out.println(count);
  }
Example #17
0
 public void start() throws Exception {
   Configuration conf = ConfigLoader.load("ingestor-cfg.xml");
   this.dbName = conf.getMongoDBName();
   List<ServerAddress> servers = new ArrayList<ServerAddress>(conf.getServers().size());
   for (ServerInfo si : conf.getServers()) {
     InetAddress inetAddress = InetAddress.getByName(si.getHost());
     servers.add(new ServerAddress(inetAddress, si.getPort()));
   }
   mongoClient = new MongoClient(servers);
   mongoClient.setWriteConcern(WriteConcern.SAFE);
 }
Example #18
0
  @Override
  public String EliminarJson(String nombreDB, int clienteId) throws UnknownHostException {
    // TODO Auto-generated method stub
    if (!ExisteCliente(nombreDB, clienteId)) {
      //			System.out.println("********************\n");
      //        	System.out.println("No existe el cliente, no se puede eliminar");
      //        	System.out.println("********************\n");
      return "No existe el cliente, no se puede eliminar";
    }

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);
    DBCollection collection = base.getCollection("Json");

    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("id", clienteId);

    collection.remove(searchQuery);
    return "Cliente eliminado";
  }
Example #19
0
  public void Crear(String nombreDB, int clienteId) throws IOException {
    try {
      //			Properties propMongo = new Properties();
      //			FileInputStream in = new FileInputStream("datosMongoDB.properties");
      //			propMongo.load(in);
      //			in.close();

      MongoClient mongoClient = new MongoClient("localhost", 27017);
      DB base = mongoClient.getDB(nombreDB);
      DBCollection collection = base.getCollection("Json");
      // Ingreso algo en la base porque si no no me la ingresa.
      if (!ExisteCliente(nombreDB, clienteId)) {
        BasicDBObject document = new BasicDBObject();
        document.put("id", clienteId);
        collection.insert(document);
      }

    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
Example #20
0
 DB(final Mongo mongo, final String name, final OperationExecutor executor) {
   if (!isValidName(name)) {
     throw new IllegalArgumentException(
         "Invalid database name format. Database name is either empty or it contains spaces.");
   }
   this.mongo = mongo;
   this.name = name;
   this.executor = executor;
   this.collectionCache = new ConcurrentHashMap<String, DBCollection>();
   this.optionHolder = new Bytes.OptionHolder(mongo.getOptionHolder());
   this.commandCodec = MongoClient.getCommandCodec();
 }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String deleteProductValue = "false";

    try {

      // Get the form data
      int productID = Integer.parseInt(request.getParameter("productID"));
      PrintWriter out = response.getWriter();
      DB db = mongo.getDB("FashionFactoryProd");
      DBCollection myProducts = db.getCollection("productInfo");
      DBCollection myTrending = db.getCollection("myTrending");
      BasicDBObject searchQuery = new BasicDBObject();
      searchQuery.put("productID", productID);
      DBCursor cursor = myProducts.find(searchQuery);

      if (cursor.count() == 0) {
        deleteProductValue = "false";
        request.setAttribute("deleteProductValue", deleteProductValue);
        RequestDispatcher rd = request.getRequestDispatcher("deleteProduct.jsp");
        rd.forward(request, response);
      } else {
        int product = 0;
        while (cursor.hasNext()) {

          BasicDBObject obj = (BasicDBObject) cursor.next();
          product = obj.getInt("productID");
          if (product == productID) {
            myProducts.remove(obj);
            myTrending.remove(searchQuery);
            deleteProductValue = "true";

            request.setAttribute("deleteProductValue", deleteProductValue);

            RequestDispatcher rd = request.getRequestDispatcher("deleteProduct.jsp");
            rd.forward(request, response);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public VariantHbaseDBAdaptor(String species, MonbaseCredentials credentials)
      throws MasterNotRunningException, ZooKeeperConnectionException, UnknownHostException {
    this.monbaseCredentials = credentials;
    this.tableName = species;
    this.effectTableName = species + "effect";

    // HBase configuration
    Configuration config = HBaseConfiguration.create();
    config.set(
        "hbase.master", credentials.getHbaseMasterHost() + ":" + credentials.getHbaseMasterPort());
    config.set("hbase.zookeeper.quorum", credentials.getHbaseZookeeperQuorum());
    config.set(
        "hbase.zookeeper.property.clientPort",
        String.valueOf(credentials.getHbaseZookeeperClientPort()));
    admin = new HBaseAdmin(config);

    // Mongo configuration
    mongoClient = new MongoClient(credentials.getMongoHost());
    db = mongoClient.getDB(credentials.getMongoDbName());
  }
  public void close() throws IOException {

    client.close();
  }
Example #24
0
  public void deleteAllRecords() {
    DB db = mongoClient.getDB(this.dbName);

    DBCollection records = db.getCollection("records");
    records.remove(new BasicDBObject());
  }
Example #25
0
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
      MongoClient mc = new MongoClient("localhost");
      DB db = mc.getDB("mydb");
      DBCollection dbc = db.getCollection("books");
      /*BasicDBObject book=
      		new BasicDBObject();
      book.put("name", "자바의 정석");
      book.put("pages", 600);
      dbc.insert(book);

      book=
      		new BasicDBObject();
      book.put("name", "스프링4");
      book.put("pages", 250);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "JQuery");
      book.put("pages", 150);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "오라클 프로그램");
      book.put("pages", 800);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "AngularJS");
      book.put("pages", 130);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "MongoDB");
      book.put("pages", 650);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "Hadoop");
      book.put("pages", 300);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "HIVE");
      book.put("pages", 350);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "PIG");
      book.put("pages", 650);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "HTML5");
      book.put("pages", 360);
      dbc.insert(book);*/
      String map =
          "function(){"
              + "var category;"
              + "if(this.pages>=300){"
              + "category='Big Books';"
              + "}else{"
              + "category='Small Books';}"
              + "emit(category,{name:this.name});}";
      String reduce =
          "function(key,values){"
              + "var sum=0;"
              + "values.forEach(function(doc){"
              + "sum+=1;"
              + "});"
              + "return {books:sum};}";
      MapReduceCommand cmd =
          new MapReduceCommand(dbc, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
      MapReduceOutput out = dbc.mapReduce(cmd);

      for (DBObject o : out.results()) {
        System.out.println(o.toString());
      }
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }
Example #26
0
 @Override
 public void Eliminar(String nombreDB) throws UnknownHostException {
   // TODO Auto-generated method stub
   MongoClient mongoClient = new MongoClient("localhost", 27017);
   mongoClient.dropDatabase(nombreDB);
 }
Example #27
0
  public static void main(String[] args) {

    try {

      meet myobj = new meet();

      // load jsiconfig.txt
      ArrayList<String> myconfiglist = new ArrayList<String>();
      myconfiglist = myobj.loadArray("jsiconfig.txt");

      // The text uri
      // "mongodb://*****:*****@ds023288.mongolab.com:23288/sample";
      String textUri = myconfiglist.get(0);

      // Create MongoClientURI object from which you get MongoClient obj
      MongoClientURI uri = new MongoClientURI(textUri);

      // Connect to that uri
      MongoClient m = new MongoClient(uri);

      // get the database named sample
      String DBname = myconfiglist.get(1);
      DB d = m.getDB(DBname);

      // get the collection mycollection in sample
      String collectionName = myconfiglist.get(2);
      DBCollection collection = d.getCollection(collectionName);

      // System.out.println("Config: "+textUri+":"+DBname+":"+collectionName);

      // twitter4j
      // Twitter twitter = new TwitterFactory().getInstance();
      Twitter twitter = new TwitterFactory().getSingleton();
      User user = twitter.verifyCredentials();

      // Twitter collection of latest tweets into the home account - defaulted to latest 20 tweets//
      //////////////////////////////////////////////////////////////

      ArrayList<String> mylatesttweetslist = new ArrayList<String>();
      // get list of tweets from a user or the next tweet listed
      try {
        long actid = 0;
        // twitter.createFriendship(actid);

        // The factory instance is re-useable and thread safe.
        // Twitter twitter = TwitterFactory.getSingleton();
        List<Status> statuses = twitter.getHomeTimeline();
        System.out.println("Showing home timeline.");

        for (Status status : statuses) {

          // System.out.println(status.getUser().getName() + ":" +status.getText());
          // Addes timeline to an array
          String mytweets = status.getUser().getName() + ":" + status.getText();
          mylatesttweetslist.add(mytweets);
        }
        ;

      } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
      }

      // MongoDB Insert Below //
      //////////////////////////

      // System Date
      Date sd = new Date();
      String sysdate = sd.toString();

      // Toggle the below to display and insert the transactions as required
      boolean showtrans = true;
      boolean inserttrans = true;

      // checkArray - loads args to a text string to allow the contains function
      String checkString = "";
      for (int ck = 0; ck < args.length; ck++) {
        checkString = checkString + args[ck];
      }
      ;

      // display transactions flag on runnning jsimongo eg: java jsimongo -d  will NOT display
      // transactions
      // insert transactions flag on runnning jsimongo eg: java jsimongo -i  will NOT insert
      // transactions

      if (args.length > 0) {
        if (checkString.contains("-d")) showtrans = false;
        if (checkString.contains("-i")) inserttrans = false;
      }
      ;

      int x = 0;
      for (String atweet : mylatesttweetslist) {
        x++;
        // Display tweets to console
        if (showtrans == true) {
          System.out.println("tweet : " + atweet);
          System.out.println("Created_DateTime : " + sysdate); // was sysdate
        }
        ;

        // Insert JSON into MongoDB
        if (inserttrans == true) {
          BasicDBObject b = new BasicDBObject();
          System.out.println("tweet : " + atweet);
          System.out.println("Created_DateTime : " + sysdate); // was sysdate

          // Insert the JSON object into the chosen collection
          collection.insert(b);
        }
        ;
        Thread.sleep(1);
      }
      ;

      System.out.println("End, the number of tweets inserted at this time was: " + x);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #28
0
 public void insertDoc(JSONObject doc) {
   DB db = mongoClient.getDB(this.dbName);
   DBCollection records = db.getCollection(this.collectionName);
   DBObject dbObject = JSONUtils.encode(doc);
   records.insert(dbObject);
 }
 public DB getConnection() {
   return client.getDB(dbName);
 }
  @Override
  public boolean close() {

    mongoClient.close();
    return true;
  }