cassandra paging 分页方法应用
2015-09-15 16:13
302 查看
费了2天的时间,终于找到了如何实现分页的办法。 cassandra 只在nodejs driver 1.0里提到了如何分页,后面就再也没有提过了,坑爹啊。 网络上搜遍了,都只有java的driver example, 也没有说这个pageState这么用。
要实现分页,driver execute是做不到的,所以要用eachRow 的API实现分页, 要用到的参数主要是:autoPage: 是否要连续查询所有页面
pageState: 用来存放最后一次查询最后一条记录的token, 这样下次查询的时候就直接从下一条记录开始查,这个很重要,否则就是网站上看到的其他人贴出来的手动记录方法了
fetchSize: 每一页查询多少记录
对于我们的应用是: 查出100万条记录,然后分页到网站使用。 需要提供总得记录数字,和实时查询需要页数内的记录。
1. 总的记录数目:这个必须要做一次全部查询,目前只有这个办法。 这个API有个好处,就是有一个function(n,row), 当你不做任何操作的时候,他不保存任何记录信息,这样做就可以不需要buffer 来保存每一条具体的记录,而只是在最后的function(err, result) 拿出rowLength 即可;
2. 分页查询: 假设按webpage 每页25条记录,先给出webpage 1的数据, 那么就是page1 (注意,webpage 是网站上显示的页面, page是后端数据库的page);如果此时客户直接点了webpage3, 跳过了webpage2, 那么对后端其实没有影响,还是照样给page2的信息给前段即可,因为查出来的是根据token排序的,不是primary_key本身,所以对于客户来说,查出来的基本是无序的;
Fetching large result sets
The single-threaded nature of Node.js should be taken into consideration when dealing with retrieving large amount of rows. If an asynchronous method uses large buffers, it could cause large memory consumption and a poor response time.Because of this, the driver exposes the #eachRow() and #stream() methods. When using these methods,
the driver will parse the rows and yield them to the user as they come through the network. All methods use a default fetchSize of 5000 rows, retrieving only the first page of results
up to a maximum of 5000 rows.
Automatic paging¶
If you want to retrieve the next pages, you can do it by setting autoPage: true in the queryOptions toautomatically request the next page, once it finished parsing the previous page.
// Imagine a column family with millions of rows. var query = 'SELECT * FROM largetable'; client.eachRow(query, [], { autoPage : true }, function(n, row) { // This function will be called per each of the rows in all the table. }, callback);
Manual paging¶
If you want to retrieve the next page of results only when you ask for it (that is, a web pager), you use the pageState made available by the end callback in #eachRow().client.eachRow(query, [], { prepare : 1 , fetchSize : 1000 }, function (n, row) { // Row callback. }, function (err, result) { // End callback. // Store the paging state. pageState = result.meta.pageState; } );
In the following kinds of requests, use the page state.
// Use the pageState in the queryOptions to continue where you left it. client.eachRow(query, [], { pageState : pageState, prepare : 1 , fetchSize : 1000 }, function (n, row) { // Row callback. }, function (err, result) { // End callback. // Store the next paging state. pageState = result.meta.pageState; } );
http://docs.datastax.com/en/developer/nodejs-driver/1.0/nodejs-driver/reference/paging.html
相关文章推荐
- 黑马程序员Android 百分比布局库(percent-support-lib) 解析与扩展
- 干一行爱一行
- 用html中div元素画横竖直线的方法
- 更改Eclipse下Tomcat的部署目录
- 【Leet Code】148. Sort List---Medium
- C#获取管理员权限
- CoreGraphics QuartzCore CGContextTranslateCTM 用法
- HUD 5256 序列变换
- XMLHttpRequest cannot load"url"Cross origin requests are only supported. send @ jquery-2.1.4.js:8630
- linux命令之top
- cocos studio2.x 对象绑定
- 数据库连接字符串简析
- Quartz实现定时任务的配置方法
- ASP.NET MVC 缓存使用示例
- hibernate核心开发接口
- android+ADT+SDK开发环境及变量配置
- hdu5446(2015长春网络赛J题)
- MongoDB常用操作
- 简单工厂
- 黑马程序员学习(五)Final、接口、多态