Example usage for com.mongodb MongoClient getDatabase

List of usage examples for com.mongodb MongoClient getDatabase

Introduction

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

Prototype

public MongoDatabase getDatabase(final String databaseName) 

Source Link

Usage

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public void connect(ServerConfiguration configuration) {
    MongoClient mongo = null;
    try {//w w  w. j  a v  a  2s .c  o  m
        String userDatabase = configuration.getUserDatabase();
        mongo = createMongoClient(configuration);

        MongoIterable<String> collectionNames;
        if (StringUtils.isNotEmpty(userDatabase)) {
            collectionNames = mongo.getDatabase(userDatabase).listCollectionNames();
        } else {
            collectionNames = mongo.getDatabase("test").listCollectionNames();
        }
        collectionNames.first();

    } catch (IOException ex) {
        throw new MongoConnectionException(ex);
    } catch (MongoException ex) {
        LOG.error("Error when accessing Mongo server", ex);
        throw new MongoConnectionException(ex.getMessage());
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

List<Database> loadDatabaseCollections(ServerConfiguration configuration) {
    MongoClient mongo = null;
    List<Database> mongoDatabases = new LinkedList<>();
    try {//from w  w  w  .ja  v a2s  .  c  om
        String userDatabase = configuration.getUserDatabase();

        mongo = createMongoClient(configuration);

        if (StringUtils.isNotEmpty(userDatabase)) {
            MongoDatabase database = mongo.getDatabase(userDatabase);
            mongoDatabases.add(createMongoDatabaseAndItsCollections(database));
        } else {
            MongoIterable<String> databaseNames = mongo.listDatabaseNames();
            for (String databaseName : databaseNames) {
                MongoDatabase database = mongo.getDatabase(databaseName);
                mongoDatabases.add(createMongoDatabaseAndItsCollections(database));
            }

            Collections.sort(mongoDatabases, new Comparator<Database>() {
                @Override
                public int compare(Database o1, Database o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });
        }

        return mongoDatabases;
    } catch (MongoException | UnknownHostException mongoEx) {
        throw new ConfigurationException(mongoEx);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public void update(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection,
        Document mongoDocument) {
    MongoClient mongo = null;
    try {/*  w w w . ja v a2  s.c om*/
        String databaseName = singleMongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        MongoDatabase database = mongo.getDatabase(databaseName);
        MongoCollection<Document> collection = database.getCollection(singleMongoCollection.getName());

        final Object id = mongoDocument.get("_id");
        if (id == null) {
            collection.insertOne(mongoDocument);
        } else {
            collection.replaceOne(Filters.eq("_id", id), mongoDocument);
        }
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public void delete(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection, Object _id) {
    MongoClient mongo = null;
    try {/*from   w  w w .ja va2  s  .  co m*/
        String databaseName = singleMongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        MongoDatabase database = mongo.getDatabase(databaseName);
        MongoCollection<Document> collection = database.getCollection(singleMongoCollection.getName());
        collection.deleteOne(Filters.eq("_id", _id));
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public void dropCollection(ServerConfiguration configuration, SingleMongoCollection singleMongoCollection) {
    MongoClient mongo = null;
    try {//  w w w.  j  a v  a  2  s.c  o  m
        String databaseName = singleMongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        mongo.getDatabase(databaseName).getCollection(singleMongoCollection.getName()).drop();

    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public MongoResult loadCollectionValues(ServerConfiguration configuration,
        SingleMongoCollection singleMongoCollection, MongoQueryOptions mongoQueryOptions) {
    MongoClient mongo = null;
    try {//from  w  ww  .j  ava  2  s.  c  om
        String databaseName = singleMongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        MongoDatabase database = mongo.getDatabase(databaseName);

        MongoCollection<Document> collection = database.getCollection(singleMongoCollection.getName());

        MongoResult mongoResult = new MongoResult(singleMongoCollection.getName());
        if (mongoQueryOptions.isAggregate()) {
            return aggregate(mongoQueryOptions, mongoResult, collection);
        }

        return find(mongoQueryOptions, mongoResult, collection);

    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java

License:Apache License

public Document findMongoDocument(ServerConfiguration configuration,
        SingleMongoCollection singleMongoCollection, Object _id) {
    MongoClient mongo = null;
    try {/* ww w .j a  v a 2 s. co  m*/
        String databaseName = singleMongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        MongoDatabase database = mongo.getDatabase(databaseName);
        com.mongodb.client.MongoCollection<Document> collection = database
                .getCollection(singleMongoCollection.getName());
        return collection.find(Filters.eq("_id", _id)).first();
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.eclipse.leshan.server.californium.impl.LeshanServer.java

License:Open Source License

private void observeResource(final Client client) {
    // ObserveRequest request = new ObserveRequest("2050/0/0");
    String contentFormatParam = "TLV";
    ContentFormat contentFormat = contentFormatParam != null
            ? ContentFormat.fromName(contentFormatParam.toUpperCase())
            : null;//from   w  ww . j a  v  a2 s .c  o  m
    ObserveRequest request = new ObserveRequest(contentFormat, "/3/0/13");
    // ObserveRequest request = new ObserveRequest(contentFormat, "/2050/0/0");
    ObserveResponse cResponse = null;
    try {

        long i = 50000L;
        cResponse = this.send(client, request, i);

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    LOG.debug("cResponse : " + cResponse);

    Observation observation = cResponse.getObservation();

    observationRegistry.addListener(new ObservationRegistryListener() {

        @Override
        public void newObservation(Observation observation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void cancelled(Observation observation) {
            LOG.debug("Observation Cancelled ....");

        }

        @Override
        public void newValue(Observation observation, ObserveResponse response) {
            // writeToFile(observation, mostRecentValue,timestampedValues );
            // TODO Auto-generated method stub
            if (client.getRegistrationId().equals(observation.getRegistrationId())) {

                // initialize("document");

                /*
                 * try { publishMesssage(); } catch (Exception e1) { // TODO Auto-generated catch block
                 * e1.printStackTrace(); }
                 */

                // ********Saving into database**************************
                Gson gson = new Gson();
                // List<TimestampedLwM2mNode> obresp = response.getTimestampedLwM2mNode();
                JsonObject jsonObject = new JsonParser().parse(gson.toJson(response.getContent()))
                        .getAsJsonObject();

                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                java.util.Date date = new java.util.Date();
                String timestamp = dateFormat.format(date);

                try {

                    MongoClient mongoClient = new MongoClient(mongoDBAdd, mongoDBPort);
                    MongoDatabase database = mongoClient.getDatabase("qolsys");
                    MongoCollection<Document> collection = database.getCollection("events");
                    String event = jsonObject.get("value").getAsString().trim();
                    Document document = new Document();
                    document.put("client_ep", client.getEndpoint());
                    document.put("event", event);
                    document.put("timestamp", timestamp);
                    collection.insertOne(document);
                    json = document.toJson();
                    sendToBroker(topic, json);
                    mongoClient.close();
                    producer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println(e.getClass().getName() + ": " + e.getMessage());
                    System.exit(0);
                }

                // ******************************************************

                LOG.debug("recent observation ...." + observation);
            }
        }

        // *************************************************************

        /*
         * private static Producer<Integer, String> producer; private static final String topic = "mytopic";
         */

        @SuppressWarnings("deprecation")
        public void sendToBroker(String topic, String msg) {

            Properties producerProps = new Properties();
            producerProps.put("metadata.broker.list", kafkaBroker1Add + ":" + kafkaBroker1Port);
            producerProps.put("serializer.class", "kafka.serializer.StringEncoder");
            producerProps.put("request.required.acks", "1");
            ProducerConfig producerConfig = new ProducerConfig(producerProps);
            producer = new Producer<Integer, String>(producerConfig);
            KeyedMessage<Integer, String> keyedMsg = new KeyedMessage<Integer, String>(topic, msg);
            producer.send(keyedMsg);

        }

    });

}

From source file:org.eclipse.leshan.server.demo.servlet.FetchServlet.java

License:Open Source License

/**
 * {@inheritDoc}/*ww  w. j a v  a 2s . c om*/
 */

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Properties props = new Properties();
    props.put("zookeeper.connect", "localhost:2181");
    props.put("group.id", "testgroup");
    props.put("zookeeper.session.timeout.ms", "400");
    props.put("zookeeper.sync.time.ms", "300");
    props.put("auto.commit.interval.ms", "1000");
    ConsumerConfig conConfig = new ConsumerConfig(props);
    consumerConnector = Consumer.createJavaConsumerConnector(conConfig);
    resp.setContentType("application/json");
    MongoClient client = new MongoClient(mongoDBAdd, mongoDBPort);
    // MongoClient client = new MongoClient("54.161.178.113", 27017);
    MongoDatabase database = client.getDatabase(mongoDBName);
    MongoCollection<Document> collection = database.getCollection("events");
    Gson gson = new Gson();

    ArrayList<ClientDao> clientDaoList = new ArrayList<ClientDao>();
    if (req.getPathInfo() == null) {
        try {
            MongoCursor<String> mongoCursor = database.getCollection("events")
                    .distinct("client_ep", String.class).iterator();
            while (mongoCursor.hasNext()) {
                String clientEp = mongoCursor.next();
                ClientDao clientDao = new ClientDao();
                clientDao.setClientEP(clientEp);
                clientDao.setTimestamp(null);
                clientDaoList.add(clientDao);
            }

            String json = gson.toJson(clientDaoList);
            resp.getWriter().write(json.toString());
            resp.setStatus(HttpServletResponse.SC_OK);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
    }

    if (req.getPathInfo() != null) {
        String[] path = StringUtils.split(req.getPathInfo(), '/');
        if (path.length == 1) {
            try {

                BasicDBObject query1 = new BasicDBObject();
                BasicDBObject sort = new BasicDBObject();
                sort.put("timestamp", -1);

                query1.put("client_ep", path[0].toString());
                Iterable<Document> cur = collection.find(query1).sort(sort);
                Iterator<Document> itr = cur.iterator();
                while (itr.hasNext()) {

                    Document document = itr.next();
                    ClientDao clientDao = new ClientDao();
                    clientDao.setClientEP(document.getString("client_ep"));
                    clientDao.setEvent(document.getString("event"));
                    clientDao.setTimestamp(document.getString("timestamp"));
                    clientDaoList.add(clientDao);
                }

                //                    String json = gson.toJson(clientDaoList);
                //                    resp.getWriter().write(json.toString());
                //                    resp.setStatus(HttpServletResponse.SC_OK);

                Map<String, Integer> topicCount = new HashMap<String, Integer>();
                topicCount.put(topic, new Integer(1));

                //ConsumerConnector creates the message stream for each topic
                Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams = consumerConnector
                        .createMessageStreams(topicCount);

                // Get Kafka stream for topic 'mytopic'
                List<KafkaStream<byte[], byte[]>> kStreamList = consumerStreams.get(topic);
                // Iterate stream using ConsumerIterator
                for (final KafkaStream<byte[], byte[]> kStreams : kStreamList) {
                    ConsumerIterator<byte[], byte[]> consumerIte = kStreams.iterator();
                    int count = 0;
                    while (consumerIte.hasNext()) {
                        count++;

                        // Shutdown the consumer connector
                        if (consumerConnector != null && count == 10)
                            consumerConnector.shutdown();
                        Message msg = new Message(consumerIte.next().message());
                        ByteBuffer buffer = msg.payload();
                        byte[] bytes = new byte[buffer.remaining()];
                        buffer.get(bytes);
                        String result = new String(bytes);
                        JSONObject jsonObj = new JSONObject(result);

                        ClientDao clientDao = new ClientDao();
                        clientDao.setClientEP(jsonObj.getString("client_ep"));
                        clientDao.setEvent(jsonObj.getString("event"));
                        clientDao.setTimestamp(jsonObj.getString("timestamp"));
                        clientDaoList.add(clientDao);

                    }
                }

                String json = gson.toJson(clientDaoList);
                resp.getWriter().write(json.toString());
                resp.setStatus(HttpServletResponse.SC_OK);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                client.close();
            }

        }

    }

}

From source file:org.flywaydb.core.internal.dbsupport.mongo.MongoDatabaseUtil.java

License:Apache License

/**
 * Checks whether a Mongo database is empty.
 *
 * @param client a MongoClient for connecting to the database.
 * @param dbName the database name.//from   ww w  .java  2s.c o  m
 * @return {@code true} if it is, {@code false} if isn't. Returns {@code false} if the database does not exist.
 * @throws FlywayException when the check fails.
 */
public static boolean empty(MongoClient client, String dbName) throws FlywayException {
    return empty(client.getDatabase(dbName));
}