import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;
import com.ai.paas.util.CiperTools;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.util.JSON;
public class MongoDBClient {
private static final Logger log = Logger.getLogger(MongoDBClient.class);
private static final String IP_KEY = “ip”;
private static final String PORT_KEY = “port”;
private static final String FILE_NAME = “filename”;
private MongoClient mongo = null;
public MongoDBClient(String addr) {
try {
JSONArray array = JSONArray.fromObject(addr);
if (array != null && array.size() > 0) {
int size = array.size();
JSONObject json = null;
ArrayList<ServerAddress> sa = new ArrayList<ServerAddress>();
for (int i = 0; i < size; i++) {
json = (JSONObject) array.get(i);
sa.add(new ServerAddress(json.getString(IP_KEY), json
.getInt(PORT_KEY)));
}
mongo = new MongoClient(sa);
}
} catch (UnknownHostException e) {
log.error(“”, e);
}
}
public MongoDBClient(String addr, String database, String userName,
String password) {
try {
JSONArray array = JSONArray.fromObject(addr);
if (array != null && array.size() > 0) {
int size = array.size();
JSONObject json = null;
ArrayList<ServerAddress> sa = new ArrayList<ServerAddress>();
for (int i = 0; i < size; i++) {
json = (JSONObject) array.get(i);
sa.add(new ServerAddress(json.getString(IP_KEY), json
.getInt(PORT_KEY)));
}
String orignPwd = CiperTools.decrypt(password);
MongoCredential credential = MongoCredential
.createMongoCRCredential(userName, database,
orignPwd.toCharArray());
mongo = new MongoClient(sa, Arrays.asList(credential));
}
} catch (UnknownHostException e) {
log.error(“”, e);
}
}
public void insert(String dbName, String collectionName, String content) {
BasicDBObject doc = new BasicDBObject();
doc.put(“content”, content);
mongo.getDB(dbName).getCollection(collectionName).insert(doc);
}
public void insert(String dbName, String collectionName, JSONObject doc) {
DBObject dbObj = (DBObject) JSON.parse(doc.toString());
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}
public void insertJSON(String dbName, String collectionName, String doc) {
DBObject dbObj = (DBObject) JSON.parse(doc);
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}
public void insertJSON(String dbName, String collectionName, JSONObject doc) {
DBObject dbObj = (DBObject) JSON.parse(doc.toString());
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}
@SuppressWarnings(“rawtypes”)
public void insert(String dbName, String collectionName, Map docMap) {
DBObject dbObj = new BasicDBObject(docMap);
mongo.getDB(dbName).getCollection(collectionName).insert(dbObj);
}
public String saveFile(String dbName, byte[] byteFile, String fileName,
String fileType) {
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSInputFile dbFile = null;
try {
dbFile = fs.createFile(byteFile);
} catch (Exception e) {
e.printStackTrace();
}
dbFile.setContentType(fileType);
dbFile.setFilename(fileName);
dbFile.put(“fileName”, fileName);
dbFile.save();
return dbFile.getId().toString();
}
public String saveFile(String dbName, String fileName, String fileType) {
if (fileName == null) {
return null;
}
String name = fileName.substring(fileName.lastIndexOf(“/”) + 1);
GridFS fs = new GridFS(mongo.getDB(dbName));
File file = new File(fileName);
GridFSInputFile dbFile = null;
try {
dbFile = fs.createFile(file);
} catch (IOException e) {
e.printStackTrace();
}
dbFile.setContentType(fileType);
dbFile.setFilename(name);
dbFile.save();
return dbFile.getId().toString();
}
public void deleteFile(String dbName, String fileId) {
if (fileId == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return;
}
fs.remove(dbFile);
}
public void deleteFileByName(String dbName, String fileName) {
if (fileName == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs
.findOne(new BasicDBObject(FILE_NAME, fileName));
if (dbFile == null) {
return;
}
fs.remove(dbFile);
}
public byte[] readFile(String dbName, String fileId) {
if (fileId == null) {
return null;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return null;
}
InputStream is = null;
try {
int len = (int) dbFile.getLength();
byte[] ret = new byte[len];
is = dbFile.getInputStream();
int tmp = 0;
int i = 0;
while ((tmp = is.read()) != -1 && i < len) {
ret[i] = (byte) tmp;
i++;
}
return ret;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public byte[] readFileByName(String dbName, String fileName) {
if (fileName == null) {
return null;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs
.findOne(new BasicDBObject(FILE_NAME, fileName));
if (dbFile == null) {
return null;
}
InputStream is = null;
try {
int len = (int) dbFile.getLength();
byte[] ret = new byte[len];
is = dbFile.getInputStream();
int tmp = 0;
int i = 0;
while ((tmp = is.read()) != -1 && i < len) {
ret[i] = (byte) tmp;
i++;
}
return ret;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void readFile(String dbName, String fileId, String localFileName) {
if (fileId == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return;
}
File file = new File(localFileName);
try {
dbFile.writeTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void readFileByName(String dbName, String fileName,
String localFileName) {
if (fileName == null) {
return;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs
.findOne(new BasicDBObject(FILE_NAME, fileName));
if (dbFile == null) {
return;
}
File file = new File(localFileName);
try {
dbFile.writeTo(file);
} catch (IOException e) {
log.error(“”, e);
}
}
public void destroyMongoDB() {
if (null != mongo) {
mongo.close();
mongo = null;
}
}
public String getFileName(String dbName,String fileId) {
if (fileId == null) {
return null;
}
GridFS fs = new GridFS(mongo.getDB(dbName));
GridFSDBFile dbFile = fs.findOne(new ObjectId(fileId));
if (dbFile == null) {
return null;
}
return dbFile.getFilename();
}
}今天的文章MongoDB客户端工具类分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7076.html