@Override protected void doInitialize(UimaContext context) throws ResourceInitializationException { coll = mongo.getDB().getCollection(collection); getNewIds(); }
private void connectToMongo( String host, int port, String database, String username, String password) throws BaleenException { try { // Connect to Mongo ServerAddress sa = new ServerAddress(host, port); Optional<MongoCredential> cred = createCredentials(username, password, database); m = createMongoClient(sa, cred); getMonitor().debug("Getting Mongo Database '{}'", db); db = m.getDatabase(database); } catch (Exception e) { throw new BaleenException("Unable to connect to Mongo", e); } }
@Override protected boolean doInitialize( ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams) throws ResourceInitializationException { mongoPort = ConfigUtils.stringToInteger(mongoPortString, 27017); try { connectToMongo(mongoHost, mongoPort, mongoDb, mongoUser, mongoPass); } catch (BaleenException be) { throw new ResourceInitializationException(be); } getMonitor().info("Initialised shared Mongo resource"); return true; }
@Test public void testCredentials() { Optional<MongoCredential> credentials = SharedMongoResource.createCredentials(TEST_USER, TEST_PASS, TEST_DB); assertTrue(credentials.isPresent()); assertEquals(TEST_USER, credentials.get().getUserName()); assertEquals(TEST_PASS, new String(credentials.get().getPassword())); credentials = SharedMongoResource.createCredentials(null, TEST_PASS, TEST_DB); assertFalse(credentials.isPresent()); credentials = SharedMongoResource.createCredentials(TEST_USER, null, TEST_DB); assertFalse(credentials.isPresent()); credentials = SharedMongoResource.createCredentials(TEST_USER, TEST_PASS, null); assertFalse(credentials.isPresent()); } }
protected MongoClient createMongoClient(ServerAddress sa, Optional<MongoCredential> credentials) { if (!credentials.isPresent()) { getMonitor().debug("Connecting to Mongo without authentication"); return new MongoClient(sa); } else { getMonitor() .debug( "Connecting to Mongo with authentication as user '{}'", credentials.get().getUserName()); return new MongoClient(sa, Arrays.asList(credentials.get())); } }
@Override protected void doGetNext(JCas jCas) throws IOException, CollectionException { ObjectId id = queue.remove(0); Document docIdField = new Document(idField, id); Document document = coll.find(docIdField).first(); if (document == null) { getMonitor().error("No document returned from Mongo"); throw new CollectionException(); } String content = (String) document.get(contentField); InputStream is = IOUtils.toInputStream(content, Charset.defaultCharset()); extractor.processStream(is, mongo.getMongoURI() + "." + collection + "#" + id, jCas); for (Entry<String, Object> entry : document.entrySet()) { String key = entry.getKey(); if (contentField.equals(key) || idField.equals(key)) { continue; } else { processMongoMetadataField(jCas, key, entry.getValue()); } } if (deleteSource) { coll.deleteOne(docIdField); } }
protected MongoClient createMongoClient(ServerAddress sa, Optional<MongoCredential> credentials) { if (!credentials.isPresent()) { getMonitor().debug("Connecting to Mongo without authentication"); return new MongoClient(sa); } else { getMonitor() .debug( "Connecting to Mongo with authentication as user '{}'", credentials.get().getUserName()); return new MongoClient(sa, Arrays.asList(credentials.get())); } }
@Override protected void doGetNext(JCas jCas) throws IOException, CollectionException { ObjectId id = queue.remove(0); Document docIdField = new Document(idField, id); Document document = coll.find(docIdField).first(); if (document == null) { getMonitor().error("No document returned from Mongo"); throw new CollectionException(); } String content = (String) document.get(contentField); InputStream is = IOUtils.toInputStream(content, Charset.defaultCharset()); extractContent(is, mongo.getMongoURI() + "." + collection + "#" + id, jCas); for (Entry<String, Object> entry : document.entrySet()) { String key = entry.getKey(); if (contentField.equals(key) || idField.equals(key)) { continue; } else { processMongoMetadataField(jCas, key, entry.getValue()); } } if (deleteSource) { coll.deleteOne(docIdField); } }
@Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); MongoDatabase db = mongoResource.getDB(); recordsCollection = db.getCollection(recordsCollectionName); objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(Include.NON_NULL); }
private void connectToMongo( String host, int port, String database, String username, String password) throws BaleenException { try { // Connect to Mongo ServerAddress sa = new ServerAddress(host, port); Optional<MongoCredential> cred = createCredentials(username, password, database); m = createMongoClient(sa, cred); getMonitor().debug("Getting Mongo Database '{}'", db); db = m.getDatabase(database); } catch (Exception e) { throw new BaleenException("Unable to connect to Mongo", e); } }
@Override protected boolean doInitialize( ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams) throws ResourceInitializationException { mongoPort = ConfigUtils.stringToInteger(mongoPortString, 27017); try { connectToMongo(mongoHost, mongoPort, mongoDb, mongoUser, mongoPass); } catch (BaleenException be) { throw new ResourceInitializationException(be); } getMonitor().info("Initialised shared Mongo resource"); return true; }
@Override protected void doDestroy() { getMonitor().debug("Disconnecting from Mongo"); m.close(); db = null; m = null; }
@Override public void afterResourcesInitialized() throws ResourceInitializationException { // Our initialisation needs to wait for Mongo to be initialised and then injected collection = mongo.getDB().getCollection(collectionName); collection.createIndex(new Document("docId", 1)); }
@Override protected void doDestroy() { getMonitor().debug("Disconnecting from Mongo"); m.close(); db = null; m = null; }
@Override protected void initialiseForDocuments() throws ResourceInitializationException { final MongoDatabase db = mongoResource.getDB(); documentCollection = db.getCollection(documentCollectionName); documentCollection.createIndex(new Document(EXTERNAL_ID, 1)); documentCollection.createIndex(new Document("content", TEXT)); }
@Override public void doInitialize(final UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); dbCollection = mongo.getDB().getCollection(collection); // Delete the whole database if (clear) { dbCollection.deleteMany(new Document()); } }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); MongoDatabase db = mongo.getDB(); documentsCollection = db.getCollection(documentCollectionName); stopwords = stopwordResource.getStopwords(stoplist); }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); MongoDatabase db = mongo.getDB(); documentsCollection = db.getCollection(documentCollectionName); stopwords = stopwordResource.getStopwords(stoplist); }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); MongoDatabase db = mongo.getDB(); documentsCollection = db.getCollection(documentCollectionName); stopwords = stopwordResource.getStopwords(stoplist); }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); MongoDatabase db = mongo.getDB(); documentsCollection = db.getCollection(documentCollectionName); stopwords = stopwordResource.getStopwords(stoplist); }