org.elasticsearch.river.mongodb.MongoDBRiverDefinition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch-river-mongodb Show documentation
Show all versions of elasticsearch-river-mongodb Show documentation
MongoDB River for ElasticSearch
The newest version!
package org.elasticsearch.river.mongodb;
import java.net.UnknownHostException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.bson.BasicBSONObject;
import org.bson.types.BSONTimestamp;
import org.bson.types.Binary;
import org.elasticsearch.common.Preconditions;
import org.elasticsearch.common.collect.Maps;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.river.RiverSettings;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClientOptions;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.util.JSON;
public class MongoDBRiverDefinition {
private static final ESLogger logger = Loggers.getLogger(MongoDBRiverDefinition.class);
// defaults
public final static String DEFAULT_DB_HOST = "localhost";
public final static int DEFAULT_DB_PORT = 27017;
public final static int DEFAULT_CONCURRENT_REQUESTS = Runtime.getRuntime().availableProcessors();
public final static int DEFAULT_BULK_ACTIONS = 1000;
public final static TimeValue DEFAULT_FLUSH_INTERVAL = TimeValue.timeValueMillis(10);
public final static ByteSizeValue DEFAULT_BULK_SIZE = new ByteSizeValue(5, ByteSizeUnit.MB);
public final static int DEFAULT_CONNECT_TIMEOUT = 30000;
public final static int DEFAULT_SOCKET_TIMEOUT = 60000;
// fields
public final static String DB_FIELD = "db";
public final static String SERVERS_FIELD = "servers";
public final static String HOST_FIELD = "host";
public final static String PORT_FIELD = "port";
public final static String OPTIONS_FIELD = "options";
public final static String SECONDARY_READ_PREFERENCE_FIELD = "secondary_read_preference";
public final static String CONNECT_TIMEOUT = "connect_timeout";
public final static String SOCKET_TIMEOUT = "socket_timeout";
public final static String SSL_CONNECTION_FIELD = "ssl";
public final static String SSL_VERIFY_CERT_FIELD = "ssl_verify_certificate";
public final static String DROP_COLLECTION_FIELD = "drop_collection";
public final static String EXCLUDE_FIELDS_FIELD = "exclude_fields";
public final static String INCLUDE_FIELDS_FIELD = "include_fields";
public final static String INCLUDE_COLLECTION_FIELD = "include_collection";
public final static String INITIAL_TIMESTAMP_FIELD = "initial_timestamp";
public final static String INITIAL_TIMESTAMP_SCRIPT_TYPE_FIELD = "script_type";
public final static String INITIAL_TIMESTAMP_SCRIPT_FIELD = "script";
public final static String ADVANCED_TRANSFORMATION_FIELD = "advanced_transformation";
public final static String SKIP_INITIAL_IMPORT_FIELD = "skip_initial_import";
public final static String PARENT_TYPES_FIELD = "parent_types";
public final static String STORE_STATISTICS_FIELD = "store_statistics";
public final static String IMPORT_ALL_COLLECTIONS_FIELD = "import_all_collections";
public final static String DISABLE_INDEX_REFRESH_FIELD = "disable_index_refresh";
public final static String FILTER_FIELD = "filter";
public final static String CREDENTIALS_FIELD = "credentials";
public final static String USER_FIELD = "user";
public final static String PASSWORD_FIELD = "password";
public final static String SCRIPT_FIELD = "script";
public final static String SCRIPT_TYPE_FIELD = "script_type";
public final static String COLLECTION_FIELD = "collection";
public final static String GRIDFS_FIELD = "gridfs";
public final static String INDEX_OBJECT = "index";
public final static String NAME_FIELD = "name";
public final static String TYPE_FIELD = "type";
public final static String LOCAL_DB_FIELD = "local";
public final static String ADMIN_DB_FIELD = "admin";
public final static String THROTTLE_SIZE_FIELD = "throttle_size";
public final static String BULK_SIZE_FIELD = "bulk_size";
public final static String BULK_TIMEOUT_FIELD = "bulk_timeout";
public final static String CONCURRENT_BULK_REQUESTS_FIELD = "concurrent_bulk_requests";
public final static String BULK_FIELD = "bulk";
public final static String ACTIONS_FIELD = "actions";
public final static String SIZE_FIELD = "size";
public final static String CONCURRENT_REQUESTS_FIELD = "concurrent_requests";
public final static String FLUSH_INTERVAL_FIELD = "flush_interval";
// river
private final String riverName;
private final String riverIndexName;
// mongodb.servers
private final List mongoServers = new ArrayList();
// mongodb
private final String mongoDb;
private final String mongoCollection;
private final boolean mongoGridFS;
private final BasicDBObject mongoOplogFilter;
private final BasicDBObject mongoCollectionFilter;
// mongodb.credentials
private final String mongoAdminUser;
private final String mongoAdminPassword;
private final String mongoLocalUser;
private final String mongoLocalPassword;
// mongodb.options
private final MongoClientOptions mongoClientOptions;
private final int connectTimeout;
private final int socketTimeout;
private final boolean mongoSecondaryReadPreference;
private final boolean mongoUseSSL;
private final boolean mongoSSLVerifyCertificate;
private final boolean dropCollection;
private final Set excludeFields;
private final Set includeFields;
private final String includeCollection;
private final Timestamp> initialTimestamp;
private final String script;
private final String scriptType;
private final boolean advancedTransformation;
private final boolean skipInitialImport;
private final Set parentTypes;
private final boolean storeStatistics;
private final String statisticsIndexName;
private final String statisticsTypeName;
private final boolean importAllCollections;
private final boolean disableIndexRefresh;
// index
private final String indexName;
private final String typeName;
private final int throttleSize;
// bulk
private final Bulk bulk;
public static class Builder {
// river
private String riverName;
private String riverIndexName;
// mongodb.servers
private List mongoServers = new ArrayList();
// mongodb
private String mongoDb;
private String mongoCollection;
private boolean mongoGridFS;
private BasicDBObject mongoOplogFilter;// = new BasicDBObject();
private BasicDBObject mongoCollectionFilter = new BasicDBObject();
// mongodb.credentials
private String mongoAdminUser = "";
private String mongoAdminPassword = "";
private String mongoLocalUser = "";
private String mongoLocalPassword = "";
// mongodb.options
private MongoClientOptions mongoClientOptions = null;
private int connectTimeout = 0;
private int socketTimeout = 0;
private boolean mongoSecondaryReadPreference = false;
private boolean mongoUseSSL = false;
private boolean mongoSSLVerifyCertificate = false;
private boolean dropCollection = false;
private Set excludeFields = null;
private Set includeFields = null;
private String includeCollection = "";
private Timestamp> initialTimestamp = null;
private String script = null;
private String scriptType = null;
private boolean advancedTransformation = false;
private boolean skipInitialImport;
private Set parentTypes = null;
private boolean storeStatistics;
private String statisticsIndexName;
private String statisticsTypeName;
private boolean importAllCollections;
private boolean disableIndexRefresh;
// index
private String indexName;
private String typeName;
private int throttleSize;
private Bulk bulk;
public Builder mongoServers(List mongoServers) {
this.mongoServers = mongoServers;
return this;
}
public Builder riverName(String riverName) {
this.riverName = riverName;
return this;
}
public Builder riverIndexName(String riverIndexName) {
this.riverIndexName = riverIndexName;
return this;
}
public Builder mongoDb(String mongoDb) {
this.mongoDb = mongoDb;
return this;
}
public Builder mongoCollection(String mongoCollection) {
this.mongoCollection = mongoCollection;
return this;
}
public Builder mongoGridFS(boolean mongoGridFS) {
this.mongoGridFS = mongoGridFS;
return this;
}
public Builder mongoOplogFilter(BasicDBObject mongoOplogFilter) {
this.mongoOplogFilter = mongoOplogFilter;
return this;
}
public Builder mongoCollectionFilter(BasicDBObject mongoCollectionFilter) {
this.mongoCollectionFilter = mongoCollectionFilter;
return this;
}
public Builder mongoAdminUser(String mongoAdminUser) {
this.mongoAdminUser = mongoAdminUser;
return this;
}
public Builder mongoAdminPassword(String mongoAdminPassword) {
this.mongoAdminPassword = mongoAdminPassword;
return this;
}
public Builder mongoLocalUser(String mongoLocalUser) {
this.mongoLocalUser = mongoLocalUser;
return this;
}
public Builder mongoLocalPassword(String mongoLocalPassword) {
this.mongoLocalPassword = mongoLocalPassword;
return this;
}
public Builder mongoClientOptions(MongoClientOptions mongoClientOptions) {
this.mongoClientOptions = mongoClientOptions;
return this;
}
public Builder connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public Builder socketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}
public Builder mongoSecondaryReadPreference(boolean mongoSecondaryReadPreference) {
this.mongoSecondaryReadPreference = mongoSecondaryReadPreference;
return this;
}
public Builder mongoUseSSL(boolean mongoUseSSL) {
this.mongoUseSSL = mongoUseSSL;
return this;
}
public Builder mongoSSLVerifyCertificate(boolean mongoSSLVerifyCertificate) {
this.mongoSSLVerifyCertificate = mongoSSLVerifyCertificate;
return this;
}
public Builder dropCollection(boolean dropCollection) {
this.dropCollection = dropCollection;
return this;
}
public Builder excludeFields(Set excludeFields) {
this.excludeFields = excludeFields;
return this;
}
public Builder includeFields(Set includeFields) {
this.includeFields = includeFields;
return this;
}
public Builder includeCollection(String includeCollection) {
this.includeCollection = includeCollection;
return this;
}
public Builder disableIndexRefresh(boolean disableIndexRefresh) {
this.disableIndexRefresh = disableIndexRefresh;
return this;
}
public Builder initialTimestamp(Binary initialTimestamp) {
this.initialTimestamp = new Timestamp.GTID(initialTimestamp.getData(), null);
return this;
}
public Builder initialTimestamp(BSONTimestamp initialTimestamp) {
this.initialTimestamp = new Timestamp.BSON(initialTimestamp);
return this;
}
public Builder advancedTransformation(boolean advancedTransformation) {
this.advancedTransformation = advancedTransformation;
return this;
}
public Builder skipInitialImport(boolean skipInitialImport) {
this.skipInitialImport = skipInitialImport;
return this;
}
public Builder parentTypes(Set parentTypes) {
this.parentTypes = parentTypes;
return this;
}
public Builder storeStatistics(boolean storeStatistics) {
this.storeStatistics = storeStatistics;
return this;
}
public Builder statisticsIndexName(String statisticsIndexName) {
this.statisticsIndexName = statisticsIndexName;
return this;
}
public Builder statisticsTypeName(String statisticsTypeName) {
this.statisticsTypeName = statisticsTypeName;
return this;
}
public Builder importAllCollections(boolean importAllCollections) {
this.importAllCollections = importAllCollections;
return this;
}
public Builder script(String script) {
this.script = script;
return this;
}
public Builder scriptType(String scriptType) {
this.scriptType = scriptType;
return this;
}
public Builder indexName(String indexName) {
this.indexName = indexName;
return this;
}
public Builder typeName(String typeName) {
this.typeName = typeName;
return this;
}
public Builder throttleSize(int throttleSize) {
this.throttleSize = throttleSize;
return this;
}
public Builder bulk(Bulk bulk) {
this.bulk = bulk;
return this;
}
public MongoDBRiverDefinition build() {
return new MongoDBRiverDefinition(this);
}
}
static class Bulk {
private final int concurrentRequests;
private final int bulkActions;
private final ByteSizeValue bulkSize;
private final TimeValue flushInterval;
static class Builder {
private int concurrentRequests = DEFAULT_CONCURRENT_REQUESTS;
private int bulkActions = DEFAULT_BULK_ACTIONS;
private ByteSizeValue bulkSize = DEFAULT_BULK_SIZE;
private TimeValue flushInterval = DEFAULT_FLUSH_INTERVAL;
public Builder concurrentRequests(int concurrentRequests) {
this.concurrentRequests = concurrentRequests;
return this;
}
public Builder bulkActions(int bulkActions) {
this.bulkActions = bulkActions;
return this;
}
public Builder bulkSize(ByteSizeValue bulkSize) {
this.bulkSize = bulkSize;
return this;
}
public Builder flushInterval(TimeValue flushInterval) {
this.flushInterval = flushInterval;
return this;
}
/**
* Builds a new bulk processor.
*/
public Bulk build() {
return new Bulk(this);
}
}
public Bulk(final Builder builder) {
this.bulkActions = builder.bulkActions;
this.bulkSize = builder.bulkSize;
this.concurrentRequests = builder.concurrentRequests;
this.flushInterval = builder.flushInterval;
}
public int getConcurrentRequests() {
return concurrentRequests;
}
public int getBulkActions() {
return bulkActions;
}
public ByteSizeValue getBulkSize() {
return bulkSize;
}
public TimeValue getFlushInterval() {
return flushInterval;
}
}
@SuppressWarnings("unchecked")
public synchronized static MongoDBRiverDefinition parseSettings(String riverName, String riverIndexName, RiverSettings settings,
ScriptService scriptService) {
logger.info("Parse river settings for {}", riverName);
Preconditions.checkNotNull(riverName, "No riverName specified");
Preconditions.checkNotNull(riverIndexName, "No riverIndexName specified");
Preconditions.checkNotNull(settings, "No settings specified");
Builder builder = new Builder();
builder.riverName(riverName);
builder.riverIndexName(riverIndexName);
List mongoServers = new ArrayList();
String mongoHost;
int mongoPort;
if (settings.settings().containsKey(MongoDBRiver.TYPE)) {
Map mongoSettings = (Map) settings.settings().get(MongoDBRiver.TYPE);
if (mongoSettings.containsKey(SERVERS_FIELD)) {
Object mongoServersSettings = mongoSettings.get(SERVERS_FIELD);
logger.trace("mongoServersSettings: " + mongoServersSettings);
boolean array = XContentMapValues.isArray(mongoServersSettings);
if (array) {
ArrayList