mirror of
https://github.com/alibaba/DataX.git
synced 2025-05-02 17:40:28 +08:00
修复写入MongoDB时的权限认证bug
This commit is contained in:
parent
ef84593760
commit
d569a93ad6
@ -25,6 +25,10 @@ public class KeyConstant {
|
|||||||
* mongodb 数据库名
|
* mongodb 数据库名
|
||||||
*/
|
*/
|
||||||
public static final String MONGO_DB_NAME = "dbName";
|
public static final String MONGO_DB_NAME = "dbName";
|
||||||
|
/**
|
||||||
|
* mongodb 验证数据库
|
||||||
|
*/
|
||||||
|
public static final String MONGO_AUTHDB = "authDb";
|
||||||
/**
|
/**
|
||||||
* mongodb 集合名
|
* mongodb 集合名
|
||||||
*/
|
*/
|
||||||
|
@ -29,6 +29,9 @@ public class MongoDBWriter extends Writer{
|
|||||||
public static class Job extends Writer.Job {
|
public static class Job extends Writer.Job {
|
||||||
|
|
||||||
private Configuration originalConfig = null;
|
private Configuration originalConfig = null;
|
||||||
|
private MongoClient mongoClient;
|
||||||
|
private String userName = null;
|
||||||
|
private String password = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Configuration> split(int mandatoryNumber) {
|
public List<Configuration> split(int mandatoryNumber) {
|
||||||
@ -42,6 +45,15 @@ public class MongoDBWriter extends Writer{
|
|||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
this.originalConfig = super.getPluginJobConf();
|
this.originalConfig = super.getPluginJobConf();
|
||||||
|
this.userName = originalConfig.getString(KeyConstant.MONGO_USER_NAME);
|
||||||
|
this.password = originalConfig.getString(KeyConstant.MONGO_USER_PASSWORD);
|
||||||
|
String database = originalConfig.getString(KeyConstant.MONGO_DB_NAME);
|
||||||
|
String authDb = originalConfig.getString(KeyConstant.MONGO_AUTHDB, database);
|
||||||
|
if(!Strings.isNullOrEmpty(userName) && !Strings.isNullOrEmpty(password)) {
|
||||||
|
this.mongoClient = MongoUtil.initCredentialMongoClient(this.originalConfig,userName,password,authDb);
|
||||||
|
} else {
|
||||||
|
this.mongoClient = MongoUtil.initMongoClient(this.originalConfig);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -64,6 +76,7 @@ public class MongoDBWriter extends Writer{
|
|||||||
|
|
||||||
private String userName = null;
|
private String userName = null;
|
||||||
private String password = null;
|
private String password = null;
|
||||||
|
private String authDb = null;
|
||||||
|
|
||||||
private String database = null;
|
private String database = null;
|
||||||
private String collection = null;
|
private String collection = null;
|
||||||
@ -126,7 +139,7 @@ public class MongoDBWriter extends Writer{
|
|||||||
if(Strings.isNullOrEmpty(database) || Strings.isNullOrEmpty(collection)
|
if(Strings.isNullOrEmpty(database) || Strings.isNullOrEmpty(collection)
|
||||||
|| mongoClient == null || mongodbColumnMeta == null || batchSize == null) {
|
|| mongoClient == null || mongodbColumnMeta == null || batchSize == null) {
|
||||||
throw DataXException.asDataXException(MongoDBWriterErrorCode.ILLEGAL_VALUE,
|
throw DataXException.asDataXException(MongoDBWriterErrorCode.ILLEGAL_VALUE,
|
||||||
MongoDBWriterErrorCode.ILLEGAL_VALUE.getDescription());
|
MongoDBWriterErrorCode.ILLEGAL_VALUE.getDescription());
|
||||||
}
|
}
|
||||||
MongoDatabase db = mongoClient.getDatabase(database);
|
MongoDatabase db = mongoClient.getDatabase(database);
|
||||||
MongoCollection<BasicDBObject> col = db.getCollection(this.collection, BasicDBObject.class);
|
MongoCollection<BasicDBObject> col = db.getCollection(this.collection, BasicDBObject.class);
|
||||||
@ -179,7 +192,7 @@ public class MongoDBWriter extends Writer{
|
|||||||
try {
|
try {
|
||||||
if (KeyConstant.isObjectIdType(type.toLowerCase())) {
|
if (KeyConstant.isObjectIdType(type.toLowerCase())) {
|
||||||
data.put(columnMeta.getJSONObject(i).getString(KeyConstant.COLUMN_NAME),
|
data.put(columnMeta.getJSONObject(i).getString(KeyConstant.COLUMN_NAME),
|
||||||
new ObjectId(record.getColumn(i).asString()));
|
new ObjectId(record.getColumn(i).asString()));
|
||||||
} else if (KeyConstant.isArrayType(type.toLowerCase())) {
|
} else if (KeyConstant.isArrayType(type.toLowerCase())) {
|
||||||
String splitter = columnMeta.getJSONObject(i).getString(KeyConstant.COLUMN_SPLITTER);
|
String splitter = columnMeta.getJSONObject(i).getString(KeyConstant.COLUMN_SPLITTER);
|
||||||
if (Strings.isNullOrEmpty(splitter)) {
|
if (Strings.isNullOrEmpty(splitter)) {
|
||||||
@ -320,8 +333,9 @@ public class MongoDBWriter extends Writer{
|
|||||||
this.userName = writerSliceConfig.getString(KeyConstant.MONGO_USER_NAME);
|
this.userName = writerSliceConfig.getString(KeyConstant.MONGO_USER_NAME);
|
||||||
this.password = writerSliceConfig.getString(KeyConstant.MONGO_USER_PASSWORD);
|
this.password = writerSliceConfig.getString(KeyConstant.MONGO_USER_PASSWORD);
|
||||||
this.database = writerSliceConfig.getString(KeyConstant.MONGO_DB_NAME);
|
this.database = writerSliceConfig.getString(KeyConstant.MONGO_DB_NAME);
|
||||||
|
this.authDb = writerSliceConfig.getString(KeyConstant.MONGO_AUTHDB, database);
|
||||||
if(!Strings.isNullOrEmpty(userName) && !Strings.isNullOrEmpty(password)) {
|
if(!Strings.isNullOrEmpty(userName) && !Strings.isNullOrEmpty(password)) {
|
||||||
this.mongoClient = MongoUtil.initCredentialMongoClient(this.writerSliceConfig,userName,password,database);
|
this.mongoClient = MongoUtil.initCredentialMongoClient(this.writerSliceConfig,userName,password,authDb);
|
||||||
} else {
|
} else {
|
||||||
this.mongoClient = MongoUtil.initMongoClient(this.writerSliceConfig);
|
this.mongoClient = MongoUtil.initMongoClient(this.writerSliceConfig);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user