List of usage examples for com.mongodb.client MongoDatabase getCollection
MongoCollection<Document> getCollection(String collectionName);
From source file:org.juanitodread.ecommercerest.model.dao.imp.MongoProductDaoImp.java
License:Apache License
public MongoProductDaoImp(final MongoDatabase productsDatabase) { productsCollection = productsDatabase.getCollection(COLLECTION); }
From source file:org.kaaproject.kaa.server.datamigration.CtlNotificationMigration.java
License:Apache License
@Override protected List<Schema> transform() throws SQLException { List<Schema> res = super.transform(); if (nosql.equals(Options.DEFAULT_NO_SQL)) { MongoDatabase database = client.getDatabase(dbName); MongoCollection<Document> notification = database.getCollection("notification"); MongoCollection<Document> enpNotification = database.getCollection("endpoint_notification"); FindIterable<Document> cursor = notification.find(); for (Document document : cursor) { Object id = document.get("_id"); Long schemaId = parseLong((String) document.get("notification_schema_id")); notification.updateMany(Filters.eq("_id", id), Filters.eq("$set", Filters.eq("notification_schema_id", schemaId + idShift))); }//w w w . jav a2 s . c o m cursor = enpNotification.find(); for (Document document : cursor) { Object id = document.get("_id"); Long schemaId = parseLong((String) document.get("notification.notification_schema_id")); notification.updateMany(Filters.eq("_id", id), Filters.eq("$set", Filters.eq("notification.notification_schema_id", schemaId + idShift))); } } else { Session session = cluster.connect(dbName); BatchStatement batchStatement = new BatchStatement(); //notification ResultSet results = session.execute(select().from("notification")); for (Row row : results) { String id = row.getString("nf_id"); Long schemaId = parseLong(row.getString("schema_id")); String[] ids = id.split("::"); batchStatement.add(update("notification").with(set("schema_id", String.valueOf(schemaId + idShift))) .where(eq("topic_id", ids[0])).and(eq("nf_type", ids[1])) .and(eq("nf_version", Integer.valueOf(ids[2]))) .and(eq("seq_num", Integer.valueOf(ids[3])))); } //endpoint notification results = session.execute(select().from("ep_nfs")); for (Row row : results) { String id = row.getString("nf_id"); Long schemaId = parseLong(row.getString("schema_id")); String[] ids = id.split("::"); ByteBuffer epKeyHash = Bytes.fromHexString(ids[0]); Date lastModTime = new Date(Long.valueOf(ids[1])); batchStatement.add(update("ep_nfs").with(set("schema_id", String.valueOf(schemaId + idShift))) .where(eq("ep_key_hash", epKeyHash)).and(eq("last_mod_time", lastModTime))); } session.execute(batchStatement); session.close(); cluster.close(); } return res; }
From source file:org.kaaproject.kaa.server.datamigration.EndpointProfileMigration.java
License:Apache License
/** * Add field use_raw_configuration_schema to endpointProfile that used to support devices using * SDK version 0.9.0/*from w w w. j a v a 2 s. c o m*/ */ public void transform() { //mongo MongoClient client = new MongoClient(host); MongoDatabase database = client.getDatabase(dbName); MongoCollection<Document> endpointProfile = database.getCollection("endpoint_profile"); endpointProfile.updateMany(new Document(), eq("$set", eq("use_raw_schema", false))); //cassandra Cluster cluster = Cluster.builder().addContactPoint(host).build(); Session session = cluster.connect(dbName); session.execute("ALTER TABLE ep_profile ADD use_raw_schema boolean"); session.close(); cluster.close(); }
From source file:org.kaaproject.kaa.server.datamigration.UpdateUuidsMigration.java
License:Apache License
/** * Change encoding of uuids from Latin1 to Base64 in relational and NoSQL databases. * *//*from ww w. j a v a 2 s .c o m*/ public void transform() throws IOException, SQLException { QueryRunner run = new QueryRunner(); ResultSetHandler<List<Configuration>> rsHandler = new BeanListHandler<>(Configuration.class); List<Configuration> configs = run.query(connection, "SELECT * FROM configuration", rsHandler); for (Configuration config : configs) { JsonNode json = new ObjectMapper().readTree(config.getConfigurationBody()); JsonNode jsonEncoded = encodeUuids(json); byte[] encodedConfigurationBody = jsonEncoded.toString().getBytes(); int updates = run.update(connection, "UPDATE configuration SET configuration_body=? WHERE id=?", encodedConfigurationBody, config.getId()); if (updates != 1) { System.err.println("Error: failed to update configuration: " + config); } } if (nosql.equals(Options.DEFAULT_NO_SQL)) { MongoDatabase database = client.getDatabase(dbName); MongoCollection<Document> userConfiguration = database.getCollection("user_configuration"); FindIterable<Document> documents = userConfiguration.find(); for (Document d : documents) { String body = (String) d.get("body"); JsonNode json = new ObjectMapper().readTree(body); JsonNode jsonEncoded = encodeUuids(json); userConfiguration.updateOne(Filters.eq("_id", d.get("_id")), Filters.eq("$set", Filters.eq("body", jsonEncoded))); } } else { Session session = cluster.connect(dbName); BatchStatement batchStatement = new BatchStatement(); String tableName = "user_conf"; ResultSet results = session.execute(select().from(tableName)); for (Row row : results) { String userId = row.getString("user_id"); String appToken = row.getString("app_token"); int schemaVersion = row.getInt("schema_version"); String body = row.getString("body"); String bodyEncoded = encodeUuids(new ObjectMapper().readTree(body)).toString(); batchStatement.add(update(tableName).with(set("body", bodyEncoded)).where(eq("user_id", userId)) .and(eq("app_token", appToken)).and(eq("schema_version", schemaVersion))); } session.execute(batchStatement); session.close(); cluster.close(); } }
From source file:org.kiaan.Main.java
/** public static class view_main extends JFrame { // ww w. ja va 2 s .c o m public view_main() throws HeadlessException, IOException { // addKeyListener(getInputKey()); // getContentPane(); setTitle(" ? "); setDefaultCloseOperation(EXIT_ON_CLOSE); setScreenSize(); // add BorderLayout BorderLayout Layout = new BorderLayout(); Layout.setHgap(3);Layout.setVgap(3); setLayout(Layout); // add Top JPanel pnlMainTop = new JPanel(); pnlMainTop.setBackground(Color.LIGHT_GRAY); add(pnlMainTop, BorderLayout.PAGE_START); JButton btn = new JButton("change"); pnlMainTop.add(btn); // add left JPanel pnlMainLeft = new JPanel(); pnlMainLeft.setBackground(Color.LIGHT_GRAY); add(pnlMainLeft, BorderLayout.LINE_START); // add right JPanel pnlMainRight = new JPanel(); pnlMainRight.setBackground(Color.LIGHT_GRAY); add(pnlMainRight, BorderLayout.LINE_END); // add Center JPanel pnlMainCenter = new JPanel(); pnlMainCenter.setBackground(Color.GRAY); add(pnlMainCenter, BorderLayout.CENTER); CardLayout cardLayoutMain = new CardLayout(); pnlMainCenter.setLayout(cardLayoutMain); JPanel subPanelMain = new JPanel(); subPanelMain.setBackground(Color.white); myToggleButton tb_main = new myToggleButton("",false); subPanelMain.add(tb_main); // JPanel subPanel2 = new JPanel(); subPanel2.setBackground(Color.blue); pnlMainCenter.add(subPanelMain, "1"); // pnlMainCenter.add(subPanel2, "2"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // cardLayoutMain.show(pnlMainCenter, "2"); cardLayoutMain.next(pnlMainCenter); } }); // add Bottom JPanel pnlMainBottom = new JPanel(); pnlMainBottom.setBackground(Color.LIGHT_GRAY); add(pnlMainBottom, BorderLayout.PAGE_END); // JTable tbl = new JTable(); // add(tbl, BorderLayout.CENTER); } private void setScreenSize() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setSize(screenSize.width, screenSize.height); } } **/ public static void db() { try { //connect to db MongoClient MongoClient = new MongoClient("localhost", 27017); MongoIterable<String> databaseNames = MongoClient.listDatabaseNames(); // MongoDatabase kiaanDB = MongoClient.getDatabase("kiaan"); //check exists kiaan db Boolean blnDBExists = false; for (String DBName : databaseNames) { if (DBName.equals("kiaan")) { blnDBExists = true; System.out.println("kiaan database is exists."); break; } } if (blnDBExists == false) System.out.println("create kiaan database..."); MongoDatabase mydb = MongoClient.getDatabase("kiaan"); System.out.println("Connect to database successfully"); //Check exist user collection Boolean blnCollectionName = false; for (String collectionName : mydb.listCollectionNames()) { if (collectionName.equals("user")) { blnCollectionName = true; break; } } if (!blnCollectionName) { //create users collection ObjectId user_id = new ObjectId(); Document doc = new Document().append("name", "") //.append("credit_limit", 0) // .append("status", true) .append("comment", " ? ( ?)") .append("creator_id", user_id).append("users", new Document("_id", user_id).append("id", 1) .append("first_name", " ") .append("last_name", "( ? )") .append("user_name", "admin").append("password", "1602") //.append("status", false) .append("creator_id", user_id)); mydb.getCollection("user").insertOne(doc); System.out.println("user collection created..."); doc.clear(); // BasicDBList dbl = new BasicDBList(); ObjectId branch_id = new ObjectId(); dbl.add(new Document("_id", branch_id).append("id", 8561).append("name", " ") .append("city", "").append("creator_id", user_id)); dbl.add(new Document("_id", new ObjectId()).append("id", 8576).append("name", "") .append("city", "").append("creator_id", user_id)); ObjectId bank_id = new ObjectId(); doc = new Document("_id", bank_id).append("name", "") // .append("logo", "") .append("creator_id", user_id).append("branches", dbl); mydb.getCollection("bank").insertOne(doc); dbl = new BasicDBList(); dbl.add(new Document("_id", new ObjectId()).append("id", 3238).append("name", " ") .append("city", "").append("creator_id", user_id)); doc = new Document().append("name", "") // .append("logo", "") .append("creator_id", user_id).append("branches", dbl); mydb.getCollection("bank").insertOne(doc); // doc = new Document().append("name", "").append("creator_id", user_id).append("branches", new BasicDBList()); mydb.getCollection("bank").insertOne(doc); //add doc to array // DBObject listItem = new BasicDBObject("branches", new BasicDBObject("_id", new ObjectId()) // .append("branch_id",8576) // .append("name","") // .append("city","") // ); // DBObject findQuery = new BasicDBObject("name", ""); // DBObject updateQuery = new BasicDBObject("$push", listItem); // mydb.getCollection("banks").update(findQuery, updateQuery); // System.out.println("bank collection created..."); //add person doc.clear(); dbl.clear(); ObjectId person_id1 = new ObjectId(); ObjectId person_id2 = new ObjectId(); dbl.add(new Document("_id", person_id1).append("id", 1).append("type", "") .append("first_name", "").append("last_name", "") .append("comment", " ") .append("creator_id", user_id)); BasicDBList dbl_tel = new BasicDBList(); BasicDBList dbl_bankAcc = new BasicDBList(); dbl_tel.add(new Document("_id", new ObjectId()).append("type", "mobile") .append("number", "09151213139").append("default", true)); dbl_tel.add(new Document("_id", new ObjectId()).append("type", "mobile").append("number", "09151112233")); dbl_tel.add(new Document("_id", new ObjectId()).append("type", "work") .append("number", "05133661313").append("default", true)); dbl_bankAcc.add(new Document("_id", new ObjectId()).append("type", "bank_acc") .append("bank_id", bank_id).append("number", "4218504285") .append("comment", " ").append("default", true)); dbl_bankAcc.add(new Document("_id", new ObjectId()).append("type", "sheba") .append("bank_id", bank_id).append("number", "600120020000004218504285")); dbl.add(new Document("_id", person_id2).append("id", 2).append("type", "") .append("first_name", "").append("last_name", "") .append("gender", true).append("credit_limit", 10000000) .append("address", " - ? - 2/716") .append("creator_id", user_id).append("tel", dbl_tel).append("bank_account", dbl_bankAcc)); doc = new Document("name", "").append("creator_id", user_id).append("persons", dbl); mydb.getCollection("person").insertOne(doc); mydb.getCollection("person").insertOne(new Document("name", "") .append("creator_id", user_id).append("persons", new BasicDBList())); System.out.println("person collection created..."); // doc = new Document("id", 1).append("account_no", "0205575259006") .append("account_holder", "? ").append("bank_id", bank_id) .append("branch_id", branch_id).append("type", 0).append("comment", " ") .append("creator_id", user_id); mydb.getCollection("bank_account").insertOne(doc); doc = new Document("id", 2).append("account_no", "0207723518008") .append("account_holder", "? ").append("bank_id", bank_id) .append("branch_id", branch_id).append("type", 1).append("creator_id", user_id); mydb.getCollection("bank_account").insertOne(doc); System.out.println("bank_account collection created..."); //add units ObjectId unit_id = new ObjectId(); doc = new Document("_id", unit_id).append("name", "").append("creator_id", user_id); mydb.getCollection("unit").insertOne(doc); System.out.println("Unit collection created..."); //add products dbl = new BasicDBList(); ObjectId product_id = new ObjectId(); dbl.add(new Document("_id", product_id).append("id", 1).append("name", "") .append("unit_id", unit_id).append("min_stock", 1000).append("default_price", 8300) .append("comment", " ").append("creator_id", user_id)); dbl.add(new Document("_id", new ObjectId()).append("id", 2).append("name", "") .append("unit_id", unit_id).append("min_stock", 10).append("default_price", 80000) .append("comment", " ").append("creator_id", user_id)); doc = new Document("name", "").append("comment", " ") .append("creator_id", user_id).append("products", dbl); mydb.getCollection("product").insertOne(doc); // dbl.clear(); dbl.add(new Document("_id", new ObjectId()).append("id", 3).append("name", "") .append("unit_id", unit_id).append("min_stock", 100).append("default_price", 2500) .append("comment", " ").append("creator_id", user_id)); doc = new Document("name", "").append("comment", " ") .append("creator_id", user_id).append("products", dbl); mydb.getCollection("product").insertOne(doc); // System.out.println("product Document created..."); //add buy doc.clear(); dbl.clear(); dbl.add(new Document("_id", new ObjectId()).append("product_id", product_id).append("value", 100) .append("price", 9250).append("comment", " ")); dbl.add(new Document("_id", new ObjectId()).append("product_id", product_id).append("value", 350) .append("price", 9350).append("comment", " ")); doc = new Document("_id", new ObjectId()).append("type", "buy").append("id", 1) .append("person_id", person_id1).append("by", "? ").append("discount", -1500) // .append("increase", 0) .append("tax", 4000) // .append("fare", 0) .append("comment", " ?").append("creator_id", user_id) .append("items", dbl); mydb.getCollection("buy").insertOne(doc); // doc.clear(); dbl.clear(); // dbl.add(new BasicDBObject("_id",new ObjectId()) // .append("product_id", 1) // .append("value", 1000) // .append("price", 9300) // ); // doc = new BasicDBObject("num", 2) // .append("date", new Date()) // .append("person_id", person_id2) // .append("by", "? ") // .append("discount", 0) // .append("increase", 0) // .append("user_id", user_id) // .append("items", dbl); // mydb.getCollection("buy").insert(doc); System.out.println("buy Document created..."); } // System.out.println("Collection created successfully"); // BasicDBObject doc = new BasicDBObject("name","mongoDB"); // coll.insert(doc); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }