经过几天的实际开发之后,对此问题又有了新看法,修改如下:(先看原文)
0、使用MongoClient中的insertMany方法,虽然能够快速的插入数据,但是,在使用完MongoClient之后,一定要关闭MongoClient。否则,将会在连接数叠加至MongoDB最大连接数之后导致宕机。
1、使用MongoTemplate中的save方法,速度的确太慢,但是此方法可以在保存数据时做重复判断,如果有_id相同的数据则做更新操作。有利有弊,存在即合理,不是一无是处。
2、一般情况下采用MongoTemplate中的insert方法,此方法有insertMany的效果,同时又不需要去手动关闭MongoDB链接客户端。(底层源码中显示,MongoTemplate已经做了关闭客户机的处理操作。)
====================以下为原文====================================
需求背景:某定时任务产生千条或更多JSON数据,本次数据还未完全写入数据库中,下一次定时任务的数据已经产生,由此而产生的数据拥堵怎么解决?
最初使用SpringBoot对MongoDB数据库做数据插入操作时,使用的是MongoTemplate中的save方法完成数据存储操作。
具体代码实现如下:
JSONArray为我从定时任务中获取到的数据。
for (int i = 0;i < jsonArray.size();i++){
mongoTemplate.save(jsonArray.get(i),"存储的库名");
}
此种方式存数据太慢,因为是遍历后一个一个存储,效率太低。
可以采用mongoCollection.insertMany()方法,此方法可以批量插入数据,效率很高
具体实现代码如下:
IDEA、SPringBoot、MongoDB
//首先,创建一个MongoDB连接数据库的工具类。
//此类使用的是无密码连接数据库的方法,如果你的数据库有密码,请参照文章末尾的代码。那个代码是使用用户名、密码连接数据库的方法,代码摘自菜鸟教程,亲测有效。
public class MongoDBJDBC {
static String MONGO_IP = "数据库IP地址";
static Integer MONGO_PORT = 数据库端口号,默认为27017;
public static MongoCollection getMongoDatabase(String databaseName,String collectionName){
MongoCollection mongoCollection = null;
try {
MongoClient mongoClient = new MongoClient(MONGO_IP,MONGO_PORT);
MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName);
mongoCollection = mongoDatabase.getCollection(collectionName);
}catch (Exception e){
System.out.println(e.getClass().getName()+":"+e.getMessage());
}
return mongoCollection;
}
}
获取到MongoCollection对象之后,使用其insertMany方法,插入一个集合。
public void insertData(){
//先使用remove方法,对某数据做删除,再做插入操作,以此实现批量数据的更新功能。
Query query = new Query(Criteria.where("busi_type").
is(TransformTime.getStringDate()));
mongoTemplate.remove(query,"数据库中的集合名");
List<Document> list = //获取到需要插入到数据库中的方法。
//使用工具类,获取到指定数据库的MongoCollection对象
MongoCollection mongoCollection = MongoDBJDBC.getMongoDatabase("数据库名","数据库集合名");
mongoCollection.insertMany(list);
}
======================我是华丽的分割线==========================
MongoDB数据库有用户名、密码时,连接数据库的方法示例
import java.util.ArrayList;
import java.util.List;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
public class MongoDBJDBC {
public static void main(String[] args){
try {
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("databaseName");
System.out.println("Connect to database successfully");
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
更多Java操作MongoDB数据库的方法,参照我的其他博客,怎么其他文章的传送门,我也不会弄,自己找咯。