- Timestamp:
- 2019-11-05T21:04:09+13:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/maori-lang-detection/src/org/greenstone/atea/MongoDBAccess.java
r33622 r33623 2 2 3 3 4 import com.mongodb.client.MongoCollection; 4 5 import com.mongodb.client.MongoDatabase; 5 6 import com.mongodb.MongoClient; 6 7 import com.mongodb.MongoCredential; 8 9 import org.bson.Document; 7 10 8 11 import java.io.BufferedReader; … … 23 26 * TO RUN: 24 27 * java -cp ".:../conf:../lib/*" org.greenstone.atea.MongoDBAccess 28 * 29 * Manually connecting to mongodb from client: 30 * mongo 'mongodb://mongodb.cms.waikato.ac.nz:27017' -u USERNAME -p 31 * Then after connecting with pwd, type: 32 * use DBNAME 33 * 34 * Or connect to mongodb and specify db in one statement: 35 * mongo 'mongodb://mongodb.cms.waikato.ac.nz:27017/DBNAME?authSource=admin' -u USERNAME -p 36 * 37 * Some links: 38 * - https://stackoverflow.com/questions/19938153/do-i-need-to-explicitly-close-connection 39 * - https://docs.mongodb.com/manual/reference/glossary/ (particularly "collection") 40 * - https://tecadmin.net/tutorial/mongodb/drop-collection/ 41 * IMPORTANT LINK: 42 * - https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1 43 * 25 44 */ 26 45 public class MongoDBAccess { … … 28 47 private static Logger logger = Logger.getLogger(org.greenstone.atea.MongoDBAccess.class.getName()); 29 48 30 final staticString HOST = "localhost";31 final staticint PORT = 27017; // mongodb port32 final staticString PROPS_FILENAME = "config.properties";33 final staticString DB_NAME = "ateacrawldata";49 String HOST = "localhost"; 50 int PORT = 27017; // mongodb port 51 String PROPS_FILENAME = "config.properties"; 52 String DB_NAME = "ateacrawldata"; 34 53 35 54 private String USERNAME; … … 37 56 38 57 58 private MongoClient mongo = null; 59 private MongoDatabase database = null; 60 39 61 public MongoDBAccess() throws Exception { 40 62 boolean success = false; … … 55 77 if(USERNAME.equals("")) { 56 78 USERNAME = "root"; 57 logger.warn("WARNING: No sensible value for mongodb.user specified in " + PROPS_FILENAME + " defaulting to: " + USERNAME);79 logger.warn("WARNING: No sensible value for mongodb.user specified in " + PROPS_FILENAME + ". Attempting to use: " + USERNAME); 58 80 } 59 81 PASSWORD = props.getProperty("mongodb.pwd"); … … 66 88 throw new Exception("************ FATAL ERROR: Change DB password in properties file " + PROPS_FILENAME); 67 89 } 90 91 HOST = props.getProperty("mongodb.host", HOST); 92 String port = props.getProperty("mongodb.port", Integer.toString(PORT)); 93 PORT = Integer.parseInt(port); 94 DB_NAME = props.getProperty("mongodb.dbname", DB_NAME); 95 96 logger.info("Connecting to mongodb with:"); 97 logger.info(" - host: " + HOST); 98 logger.info(" - port: " + PORT); 99 logger.info(" - user: " + USERNAME); 100 logger.info(" - db name: " + DB_NAME); 68 101 } 69 102 70 103 /** 104 * Since we have only a single MongoClient, don't need to call close/disconnect on it as per 105 * https://stackoverflow.com/questions/19938153/do-i-need-to-explicitly-close-connection 106 */ 71 107 public void connectToDB() throws Exception { 72 108 // Creating a Mongo client 73 MongoClientmongo = new MongoClient( HOST, PORT );109 mongo = new MongoClient( HOST, PORT ); 74 110 75 111 // Creating Credentials … … 79 115 80 116 // Accessing the database 81 MongoDatabasedatabase = mongo.getDatabase(DB_NAME);82 //System.out.println("Credentials: "+ credential);117 database = mongo.getDatabase(DB_NAME); 118 logger.info("Credentials: "+ credential); 83 119 } 84 120 85 121 122 /* 123 public void insertDocument() { 124 MongoCollection<Document> collection = this.database.getCollection("sampleCollection"); 125 } 126 */ 127 128 // create collection (table in RDBMS) websites, create collection webpages 129 // webpages collection will have sentences embedded 130 86 131 public static void main(String args[]) { 87 132 try { 88 133 MongoDBAccess mongodbCon = new MongoDBAccess(); 89 //mongodbCon.connectToDB(); 134 mongodbCon.connectToDB(); 135 //mongodbCon.insertDocument(); 90 136 }catch(Exception e) { 91 137 e.printStackTrace();
Note:
See TracChangeset
for help on using the changeset viewer.