首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用REST API进行Cassandra模型分页

使用REST API进行Cassandra模型分页
EN

Stack Overflow用户
提问于 2016-08-24 00:19:33
回答 1查看 1.1K关注 0票数 0

我有模型作为。

代码语言:javascript
运行
复制
# File: models.py
from uuid import uuid4

from cassandra.cqlengine.models import Model
from cassandra.cqlengine import columns


class StudentModel(Model):
    __table_name__ = 'students'
    id = columns.UUID(primary_key=True, default=uuid4)
    name = columns.Text(index=True, required=True)

    def __json__(self):
        return {'id': str(self.id),
                'name': self.name}

我写了瓶子应用程序,它提供来自这个模型的数据。

代码语言:javascript
运行
复制
# File: app.py
from bottle import run
from bottle import Bottle, request, HTTPResponse

from cassandra.cqlengine import connection
from cassandra.cqlengine.management import sync_table

from models import StudentModel

API = Bottle()

# Create Connection
connection.setup(hosts=['192.168.99.100'],
                 default_keyspace='test',
                 protocol_version=3)

# Sync database table to create table in keyspace
sync_table(StudentModel)

@API.get('/students')
def get_all_students():
    all_objs = StudentModel.all()
    return HTTPResponse(
            body={'data': [x.__json__() for x in all_objs]},
            headers={'content-type': 'application/json'},
            status_code=200)

run(host='localhost',
    port=8080,
    app=API,
    server='auto')

这段代码运行得很好,并且我让api像这样工作。

代码语言:javascript
运行
复制
curl http://localhost:8080/students -i
HTTP/1.1 200 OK
Content-Length: 74
Content-Type: application/json
Date: Tue, 23 Aug 2016 15:55:23 GMT
Server: waitress
Status-Code: 200

{"data": [{"id": "7f6d18ec-bf24-4583-a06b-b9f55a4dc6e8", "name": "test"}, {"id": "7f6d18ec-bf24-4583-a06b-b9f55a4dc6e9", "name": "test1"}]}

现在我想添加包装,并想创建具有limitoffset的应用编程接口。

我检查了Paging Large Queries,但它没有使用Model的示例。

然后我将我的api更改为:

代码语言:javascript
运行
复制
# File: app.py
...
...
@API.get('/students')
def get_all_students():
    limit = request.query.limit
    offset = request.query.offset

    all_objs = StudentModel.all()
    if limit and offset:
        all_objs = all_objs[int(offset): int(offset+limit)]

    return HTTPResponse(
            body={'data': [x.__json__() for x in all_objs]},
            headers={'content-type': 'application/json'},
            status_code=200)
...
...

并按如下方式调用api:

代码语言:javascript
运行
复制
curl "http://localhost:8080/students?limit=1&offset=0" -i
HTTP/1.1 200 OK
Content-Length: 74
Content-Type: application/json
Date: Tue, 23 Aug 2016 16:12:00 GMT
Server: waitress
Status-Code: 200

{"data": [{"id": "7f6d18ec-bf24-4583-a06b-b9f55a4dc6e8", "name": "test"}]}

代码语言:javascript
运行
复制
curl "http://localhost:8080/students?limit=1&offset=1" -i
HTTP/1.1 200 OK
Content-Length: 75
Content-Type: application/json
Date: Tue, 23 Aug 2016 16:12:06 GMT
Server: waitress
Status-Code: 200

{"data": [{"id": "7f6d18ec-bf24-4583-a06b-b9f55a4dc6e9", "name": "test1"}]}

我得到了另一个使用has_more_pagesstart_fetching_next_page()的解决方案

代码语言:javascript
运行
复制
from bottle import run
from bottle import Bottle, request, HTTPResponse

from cassandra.cqlengine import connection
from cassandra.query import SimpleStatement
from cassandra.cqlengine.management import sync_table

from models import StudentModel

API = Bottle()

# Create Connection
connection.setup(hosts=['192.168.99.100'],
                 default_keyspace='test',
                 protocol_version=3)

# Sync database table to create table in keyspace
sync_table(StudentModel)

@API.get('/students')
def get_all_students():
    limit = request.query.limit
    offset = request.query.offset

    page = int(request.query.page or 0)

    session = connection.get_session()
    session.default_fetch_size = 1

    objs = StudentModel.all()

    result = objs._execute(objs._select_query())

    data  = []
    count = 0
    while (not page or page > count) and result.has_more_pages:
        count += 1
        if page and page > count:
            result.fetch_next_page()
            continue

        data.extend(result.current_rows)
        result.fetch_next_page()
    all_objs = [StudentModel(**student) for student in data]

    return HTTPResponse(
            body={'data': [x.__json__() for x in all_objs]},
            headers={'content-type': 'application/json'},
            status_code=200)

run(host='localhost',
    port=8080,
    app=API,
    debug=True,
    server='auto')

从上面的两个解决方案中,哪一个是正确的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-24 04:53:00

目前,还没有使用CQLEngine进行分页的有效方法。使用QuerySet切片是可行的,但请注意,以前的页面仍将在结果缓存中内部实体化。因此,这可能会导致内存问题,还会影响请求性能。我已经创建了一个票据来分析一次填充一个页面的方法。您可以观看以下票证:

https://datastax-oss.atlassian.net/browse/PYTHON-627

如果您需要立即有效的分页支持,我建议使用核心驱动程序而不是cqlengine。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39106256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档