亿级别记录的mongodb分页查询java代码实现
1.准备环境
1.1 mongodb下载
1.2 mongodb启动
C:\mongodb\bin\mongod --dbpath D:\mongodb\data
1.3 可视化mongo工具Robo 3T下载
2.准备数据
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.1</version>
</dependency>
java代码执行
public static void main(String[] args) {
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
}
/**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
3.分页查询
传统的limit方式当数据量较大时查询缓慢,不太适用。考虑别的方式,参考了logstash-input-mongodb的思路:
public
def get_cursor_for_collection(mongodb, mongo_collection_name, last_id_object, batch_size)
collection = mongodb.collection(mongo_collection_name)
# Need to make this sort by date in object id then get the first of the series
# db.events_20150320.find().limit(1).sort({ts:1})
return collection.find({:_id => {:$gt => last_id_object}}).limit(batch_size)
end collection_name = collection[:name]
@logger.debug("collection_data is: #{@collection_data}")
last_id = @collection_data[index][:last_id]
#@logger.debug("last_id is #{last_id}", :index => index, :collection => collection_name)
# get batch of events starting at the last_place if it is set last_id_object = last_id
if since_type == 'id'
last_id_object = BSON::ObjectId(last_id)
elsif since_type == 'time'
if last_id != ''
last_id_object = Time.at(last_id)
end
end
cursor = get_cursor_for_collection(@mongodb, collection_name, last_id_object, batch_size)
使用java实现
import java.net.UnknownHostException;
import java.util.List; import org.bson.types.ObjectId; import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException; public class Test { public static void main(String[] args) {
int pageSize=50000; try { /**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017); /**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www"); /**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
DBCursor dbObjects;
Long cnt=table.count();
//System.out.println(table.getStats());
Long page=getPageSize(cnt,pageSize);
ObjectId lastIdObject=new ObjectId("5bda8f66ef2ed979bab041aa"); for(Long i=0L;i<page;i++) {
Long start=System.currentTimeMillis();
dbObjects=getCursorForCollection(table, lastIdObject, pageSize);
System.out.println("第"+(i+1)+"次查询,耗时:"+(System.currentTimeMillis()-start)/1000+"秒");
List<DBObject> objs=dbObjects.toArray();
lastIdObject=(ObjectId) objs.get(objs.size()-1).get("_id"); } } catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
} } public static DBCursor getCursorForCollection(DBCollection collection,ObjectId lastIdObject,int pageSize) {
DBCursor dbObjects=null;
if(lastIdObject==null) {
lastIdObject=(ObjectId) collection.findOne().get("_id"); //TODO 排序sort取第一个,否则可能丢失数据
}
BasicDBObject query=new BasicDBObject();
query.append("_id",new BasicDBObject("$gt",lastIdObject));
BasicDBObject sort=new BasicDBObject();
sort.append("_id",1);
dbObjects=collection.find(query).limit(pageSize).sort(sort);
return dbObjects;
} public static Long getPageSize(Long cnt,int pageSize) {
return cnt%pageSize==0?cnt/pageSize:cnt/pageSize+1;
} }
4.一些经验教训
1. 不小心漏打了一个$符号,导致查询不到数据,浪费了一些时间去查找原因
query.append("_id",new BasicDBObject("$gt",lastIdObject));
2.创建索引
创建普通的单列索引:db.collection.ensureIndex({field:1/-1}); 1是升续 -1是降续
实例:db.articles.ensureIndex({title:1}) //注意 field 不要加""双引号,否则创建不成功
查看当前索引状态: db.collection.getIndexes();
实例:
db.articles.getIndexes();
删除单个索引db.collection.dropIndex({filed:1/-1});
3.执行计划
db.student.find({"name":"dd1"}).explain()

参考文献:
【1】https://github.com/phutchins/logstash-input-mongodb/blob/master/lib/logstash/inputs/mongodb.rb
【2】https://www.cnblogs.com/yxlblogs/p/4930308.html
【3】https://docs.mongodb.com/manual/reference/method/db.collection.ensureIndex/
亿级别记录的mongodb分页查询java代码实现的更多相关文章
- 亿级别记录的mongodb批量导入Es的java代码完整实现
针对mongodb亿级别或者十亿级别的模糊查询,效率不高,解决方式是使用Es查询,这样就需要把数据导入的ES中 完整的代码实现如下所示:(仅供参考) import java.io.IOExceptio ...
- MongoDB分页的Java实现和分页需求的思考
前言 传统关系数据库中都提供了基于row number的分页功能,切换MongoDB后,想要实现分页,则需要修改一下思路. 传统分页思路 假设一页大小为10条.则 //page 1 1-10 //pa ...
- Mybatis包分页查询java公共类
Mybatis包分页查询java公共类 分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...
- 分页查询关键代码 多条件查询关键代码 删除选中商品关键代码 修改要先回显再修改 修改要先回显再修改 同一业务集中使用同一servlet的方法
分页查询关键代码: 通过servlet转发回来的各种信息进行分页的设计(转发回的信息有 分页查询的List集合 查询的页码 查询的条数 查询的数据库总条数 查询的总页码) 从开始时循环10次出现十个数 ...
- MongoDB 分页查询的方法及性能
最近有点忙,本来有好多东西可以总结,Redis系列其实还应该有四.五.六...不过<Redis in Action>还没读完,等读完再来总结,不然太水,对不起读者. 自从上次Redis之后 ...
- C#MongoDB 分页查询的方法及性能
传统的SQL分页 传统的sql分页,所有的方案几乎是绕不开row_number的,对于需要各种排序,复杂查询的场景,row_number就是杀手锏.另外,针对现在的web很流行的poll/push加载 ...
- Spring Data MongoDB 分页查询
在上篇文章 Spring Data MongoDB 环境搭建 基础上进行分页查询 定义公用分页参数类,实现 Pageable 接口 import java.io.Serializable; impor ...
- (记录)mysql分页查询,参数化过程的坑
在最近的工作中,由于历史遗留,一个分页查询没有参数化,被查出来有sql注入危险,所以对这个查询进行了参数化修改. 一看不知道,看了吓一跳,可能由于种种原因,分页查询sql是在存储过程中拼接出来的,wh ...
- 使用mybatis实现分页查询示例代码分析
*******************************************分页查询开始*************************************************** ...
随机推荐
- 在Visual Studio 2013中修改远程Git服务器的地址
在Visual Studio 2013中克隆了远程Git服务器的代码后,可以通过下图的方式修改Git服务器的地址:
- Tempdb--Allocation Bottleneck
Alloctaion bottleneck refers to contention in the system pages that store allocation structures. PFS ...
- Solr相似度算法二:BM25Similarity
BM25算法的全称是 Okapi BM25,是一种二元独立模型的扩展,也可以用来做搜索的相关度排序. Sphinx的默认相关性算法就是用的BM25.Lucene4.0之后也可以选择使用BM25算法(默 ...
- jenkins修改时区
查看jenkins目前的时区 访问http://your-jenkins/systemInfo,查看user.timezone变量的值 默认是纽约时间 修改时区 查https://wiki.jenki ...
- makefile文件。批处理文件。
makefile文件: NAME=XXX #要编译的文件名 OBJS=$(NAME).obj #指定输出的目标文件名 ML_FLAG=/C /COF ...
- 基于Quartz.net的远程任务管理系统 三
在上一篇中,已经把服务端都做好了.那接下来就是Web的管理端了,因为很多时候服务器是有专门的运维来管理的,我们没有权限去操作,所以有个可以管理Job的工具还是很有必要的. Web管理端,我选择现在很成 ...
- asp.net 添加错误日志
在开发程序中,错误日志很有必要.今天就把使用到的添加错误日志,记录下来,方便以后查看 利用的asp.net错误处理机制 Application_Error 贴出代码 protected void Ap ...
- 后台生产验证码code和byte[]图片
引用命名空间 using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System ...
- sharepoint 版本信息查看
#检查版本:# PowerShell script to display SharePoint products from the registry. Param( # decide on wheth ...
- @Configurable
spring的一个注解,用来自动注入bean的注解,不需要通过BeanFactory去获取