Пример #1
0
  @Override
  public List<Album> findAlbumByProperty(AlbumSearchType searchType, Object value) {

    List<Album> albums = new ArrayList<>();
    // Generic methods
    String whereClause = "";
    String valueClause = "";

    switch (searchType) {
      case ARTIST:
        whereClause = "artist.name";
        valueClause = value.toString();
        break;
      case SCORE:
        whereClause = "score";
        valueClause = value.toString();
        break;
      case TITLE:
        whereClause = "title";
        valueClause = value.toString();
        break;
      case GENRE:
        whereClause = "genre";
        valueClause = value.toString();
        break;
      default:
        System.out.println("Unknow search type");
        break;
    }

    try {

      FindIterable<Document> iterable =
          db.getCollection("album").find(new Document(whereClause, valueClause));
      iterable.forEach(
          new Block<Document>() {

            @Override
            public void apply(final Document document) {

              String title = document.getString("title");
              System.out.println(title);
              String genre = document.getString("genre");
              System.out.println(genre);
              String score = document.getString("score");
              System.out.println(score);
              String released = document.getString("released");
              System.out.println(released);
              String artists = document.get("artist").toString();
              System.out.println(artists);

              albums.add(new Album(title, score, genre, released, artists));
            }
          });

    } catch (Exception e) {
      e.printStackTrace();
    }
    return albums;
  }
Пример #2
0
  @Override
  public List<Album> findAll() {
    List<Album> albums = new ArrayList<Album>();
    try {

      iterable = db.getCollection("album").find();
      iterable.forEach(
          new Block<Document>() {

            @Override
            public void apply(final Document document) {

              String title = document.getString("title");
              System.out.println(title);
              String genre = document.getString("genre");
              System.out.println(genre);
              String score = document.getString("score");
              System.out.println(score);
              String released = document.getString("released");
              System.out.println(released);
              String artists = document.get("artist").toString();
              System.out.println(artists);

              albums.add(new Album(title, score, genre, released, artists));
            }
          });

    } catch (Exception e1) {
      e1.printStackTrace();
    }
    return albums;
  }
  public static void updateSQLDB(String mongoHost, int mongoPort, String mongoDb) {

    MongoClient mongoClient = new MongoClient(mongoHost, mongoPort);
    MongoDatabase db = mongoClient.getDatabase(mongoDb);
    FindIterable<Document> iterable = db.getCollection("flights").find();

    iterable.forEach(
        new Block<Document>() {
          public void apply(final Document document) {
            List<FlightLeg> legs = FlightToLegs.getInstance().getLegsFromFlightRecord(document);
            for (FlightLeg leg : legs) {
              FlightLegDAO legDAO = new FlightLegDAOJDBCImpl();
              int result = legDAO.create(leg);
              if (result == 0) {
                System.out.println(
                    "["
                        + result
                        + "]: ERROR Leg Creation:"
                        + leg.getDepartureAirportCode()
                        + "->"
                        + leg.getArrivalAirportCode()
                        + " DepartureUTC: "
                        + leg.getDepartureTimeUTC()
                        + " ArrivalUTC: "
                        + leg.getArrivalTimeUTC());
                System.exit(0);
              }
            }
          }
        });
    System.out.println("Done");
    mongoClient.close();
  }
  public static void updateMongoDB(
      String mongoHost, int mongoPort, String mongoDb, String flightsCol, String legsCollection) {

    MongoClient mongoClient = new MongoClient(mongoHost, mongoPort);
    MongoDatabase db = mongoClient.getDatabase(mongoDb);
    FindIterable<Document> iterable = db.getCollection(flightsCol).find();

    db.getCollection(legsCollection).createIndex(new Document("flightID", 1));
    db.getCollection(legsCollection).createIndex(new Document("departureAirport._id", 1));
    db.getCollection(legsCollection).createIndex(new Document("arrivalAirport._id", 1));
    db.getCollection(legsCollection).createIndex(new Document("effectiveDate", 1));
    db.getCollection(legsCollection).createIndex(new Document("discontinuedDate", 1));

    iterable.forEach(
        new Block<Document>() {
          public void apply(final Document document) {
            try {
              List<FlightLeg> legs = FlightToLegs.getInstance().getLegsFromFlightRecord(document);
              for (FlightLeg leg : legs) {
                FlightLegDAO legDAO = new FlightLegDAOMongoImpl();
                legDAO.setHost(mongoHost);
                legDAO.setPort(mongoPort);
                legDAO.setDB(mongoDb);
                legDAO.create(leg);
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
    System.out.println("Done");
    mongoClient.close();
  }
Пример #5
0
 public static void main(String[] args) {
   MongoClient mongoClient = new MongoClient("liangyanbo.com");
   MongoDatabase db = mongoClient.getDatabase("test");
   FindIterable<Document> iterable =
       db.getCollection("restaurants").find(new Document("borough", "Manhattan"));
   iterable.forEach(
       new Block<Document>() {
         public void apply(final Document document) {
           System.out.println(document);
         }
       });
 }
  public void find() throws UnknownHostException {

    // Connect to Mongo DB
    MongoClientURI mongoURI = new MongoClientURI("mongodb://localhost:27017");
    MongoClient mongo = new MongoClient(mongoURI);
    try {

      // Get "logs" collection from the "websites" DB.
      MongoDatabase db = mongo.getDatabase("websites");
      MongoCollection<Document> coll = db.getCollection("logs");

      // Find all DB object from the DB collection
      //			Document log = null;
      System.out.println(coll.count());
      FindIterable<Document> cursor = coll.find();

      // Loop for each db object of the cursor.
      //			while (cursor.iterator().hasNext()) {
      //				log = cursor.iterator().next();
      //				System.out.println(log.toString());
      //			}

      // JAVA 8 : these codes become
      //			cursor.forEach(new Block<Document>() {
      //
      //				@Override
      //				public void apply(Document t) {
      //					System.out.println(t.toString());
      //
      //				}
      //			});

      cursor.forEach((Block<Document>) t -> System.out.println(t.toString()));
    } finally {
      // close mongo connection
      mongo.close();
    }
  }
Пример #7
0
 public Map<String, List<String>> getBoxes() {
   final Map<String, List<String>> boxesMap = new LinkedHashMap<String, List<String>>();
   final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection();
   final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOXES);
   final Document sortCr = new Document();
   sortCr.put(DBConstants.OWNER, 1);
   final FindIterable<Document> cursor = coll.find().sort(sortCr);
   cursor.forEach(
       new Block<Document>() {
         @Override
         public void apply(final Document document) {
           List<String> boxes;
           if (!boxesMap.containsKey(document.getString(DBConstants.OWNER))) {
             boxes = new ArrayList<String>();
             boxes.add(document.getString(DBConstants.BOX_NAME));
           } else {
             boxes = boxesMap.get(document.get(DBConstants.OWNER).toString());
             boxes.add(document.getString(DBConstants.BOX_NAME));
           }
           boxesMap.put(document.getString(DBConstants.OWNER), boxes);
         }
       });
   return boxesMap;
 }