
com.ibm.util.merge.template.directive.enrich.provider.MongoProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of idmu Show documentation
Show all versions of idmu Show documentation
IBM Data Merge Utility - a template based transformation and enrichment engine
The newest version!
/*
*
* Copyright 2015-2017 IBM
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ibm.util.merge.template.directive.enrich.provider;
import java.util.ArrayList;
import java.util.HashMap;
import org.bson.Document;
import com.ibm.util.merge.Config;
import com.ibm.util.merge.data.DataElement;
import com.ibm.util.merge.data.DataList;
import com.ibm.util.merge.data.DataObject;
import com.ibm.util.merge.exception.Merge500;
import com.ibm.util.merge.exception.MergeException;
import com.ibm.util.merge.template.content.Content;
import com.ibm.util.merge.template.content.TagSegment;
import com.ibm.util.merge.template.directive.Enrich;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
/**
* A MongoDb provider that gets data from a Mongo database
* Provide Parameters usage
*
* - String command - A Mongo query JSON object, can contain replace tags
* - int parseAs - Not Applicable - Mongo data is always parsed as JSON
* - Wrapper wrapper - Wrapper for tags in command
* - HashMap<String,String> replace - The Replace HashMap used to process tags in command
* - Merger context - Merger managing the merge
*
* Configuration Environment Variables
*
* - {SourceName}.URI - The database connection URL
* - {SourceName}.USER - The database User ID, if empty Mongo Anonymous Auth is used, otherwise ScramSha1 authentication is used.
* - {SourceName}.PW - The database Password
* - {SourceName}.DB - The database name
*
* @author Mike Storey
*
*/
public class MongoProvider implements ProviderInterface {
private static final ProviderMeta meta = new ProviderMeta(
"Collection",
"The following environment variables are expected\n"
+ "{SourceName}.URI - The database connection URL\n"
+ "{SourceName}.USER - The database User ID, if empty Mongo Anonymous Auth is used, otherwise ScramSha1 authentication is used.\n"
+ "{SourceName}.PW - The database Password\n"
+ "{SourceName}.DB - The database name",
"Json Mongo Query Object",
"N/A",
"List of Document Objects");
class Query extends HashMap {
private static final long serialVersionUID = 1L;
}
private transient MongoClient client;
private transient MongoDatabase database;
private transient MongoCollection dbCollection;
private transient final String source;
private transient final String parameter;
/**
* Instantiate the provider and get the db connection
* @param source The mongo provider environment variable prefix
* @param parameter The mongo database name
*/
public MongoProvider(String source, String parameter) {
this.source = source;
this.parameter = parameter;
}
private void connect(Config config) throws Merge500 {
// Implements lazy connection
if (dbCollection != null) return;
// Get Credentials
String host = "";
String port = "";
String user = "";
String pw = "";
String dbName = "";
try {
host = config.getEnv(source + ".HOST");
port = config.getEnv(source + ".PORT");
user = config.getEnv(source + ".USER");
pw = config.getEnv(source + ".PW");
dbName = config.getEnv(source + ".DB");
} catch (MergeException e) {
throw new Merge500("Invalid Mongo Provider for:" + source);
}
ServerAddress addr = new ServerAddress(host, Integer.valueOf(port));
if (user.isEmpty()) {
this.client = new MongoClient(addr);
} else {
ArrayList creds = new ArrayList();
MongoCredential credential = MongoCredential.createScramSha1Credential(user, dbName, pw.toCharArray());
creds.add(credential);
this.client = new MongoClient(addr, creds);
}
this.database = this.client.getDatabase(dbName);
this.dbCollection = database.getCollection(parameter);
}
@Override
public DataElement provide(Enrich context) throws MergeException {
this.connect(context.getConfig());
DataList result = new DataList();
Content query = new Content(context.getTemplate().getWrapper(), context.getEnrichCommand(), TagSegment.ENCODE_JSON);
query.replace(context.getTemplate().getReplaceStack(), false, context.getConfig().getNestLimit());
DataObject queryObj = context.getConfig().parseString(Config.PARSE_JSON, query.getValue()).getAsObject();
BasicDBObject dbquery = new BasicDBObject();
for (String key : queryObj.keySet()) {
dbquery.put(key, queryObj.get(key).getAsPrimitive());
}
FindIterable find = this.dbCollection.find(dbquery);
// if (sort != null) {
// find.sort(sort);
// }
// if (limit != null) {
// find.limit(limit);
// }
if (find != null) {
for (Document aDoc : find) {
DataElement theDoc = context.getConfig().parseString(Config.PARSE_JSON, aDoc.toJson());
result.add(theDoc);
}
}
return result;
}
@Override
public void close() {
if (this.client != null) {
this.client.close();
}
return;
}
@Override
public ProviderMeta getMetaInfo() {
return MongoProvider.meta;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy