使用 Python 获取 Rethinkdb 数据库的大小 [英] Get size of Rethinkdb database with Python

查看:55
本文介绍了使用 Python 获取 Rethinkdb 数据库的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Python 获取给定 rethinkdb 数据库的大小?我想要这个是因为我正在开发一个多用户图形前端来 rethinkdb,并希望能够为每个用户的数据库强制执行配额.

How do I get the size of a given rethinkdb database using Python? I want this because I'm developing a mutli-user graphical frontend to rethinkdb and want to be able to enforce a quota for each user's database.

像下面这样的东西会很棒:

Something like below would be awesome:

r.db('thedatabase').size().run()
50gb

推荐答案

我知道这是一个迟到的答案,并不像您要求的那么漂亮,但只是将其放在这里供将来寻找此功能的人使用:

I know this is a late answer and not quite as pretty as what you asked for, but just dropping this here for people looking for this functionality in the future:

这样做的一种方法是访问 RethinkDB 虚拟数据库 rethinkdb.里面有一个统计表,其中包含大量有关数据库使用情况的信息.

One way of doing this would be by accessing the RethinkDB virtual db rethinkdb. Inside there is a stats table which includes lots of information about database usage.

r.db('rethinkdb')
    .table('stats')
    .filter(
        r.row('id').contains('table_server')
    )('storage_engine')('disk')('space_usage')('data_bytes')
    .reduce((left, right) => left.add(right))

此查询将检索所有数据库中所有节点组合的所有表的大小.本质上,它只是从 stat 对象中读取每个单独表的使用情况并将其相加.

This query will retrieve the size of all tables in all databases over all nodes combined. In essence it just reads the usage for each individual individual table from the stat object and adds it up.

请注意,这会在分片设置中综合使用所有节点(我认为).

Note that this gets the combined usage of all nodes in a sharding setup (I think).

r.db('rethinkdb')
    .table('stats')
    .filter(r.and(
        r.row('id').contains('table_server'),
        r.row('server').eq('servername')
    )
    )('storage_engine')('disk')('space_usage')('data_bytes')
    .reduce((left, right) => left.add(right))

本质上与第一个相同,只是增加了一个 and

Essentially the same as the first one just with an additional filter condition in form of an and

r.db('rethinkdb')
    .table('stats')
    .filter(r.and(
        r.row('id').contains('table_server'),
        r.row('db').eq('dbname')
    )
    )('storage_engine')('disk')('space_usage')('data_bytes')
    .reduce((left, right) => left.add(right))

同上,只是带db.

r.db('rethinkdb')
    .table('stats')
    .filter(r.and(
        r.row('id').contains('table_server'),
        r.row('db').eq('dbname'),
        r.row('table').eq('tablename')
    )
    )('storage_engine')('disk')('space_usage')('data_bytes')

按表名过滤也差不多.请记住,如果您的目标是数据库中的特定表,则应确保还为数据库添加过滤器.否则两个同名的表将返回两个大小值.另请注意,我们只期望一个结果,因此不需要减少.

It's almost the same for filtering by table name. Just remember that if you're targeting a specific table in a database you should make sure to also add a filter for the database. Otherwise two tables with the same name will return two size values. Also note that we only expect one result, so no reduce needed.

最后请记住,它们只是 ReQL 查询.

In the end just remember that they're just ReQL queries.

另请注意,这使用了 JavaScript 箭头 (=>) 函数.在 Python 中,您可以将其替换为 .reduce(lambda left, right: left+right).

Also note that this uses a JavaScript arrow (=>) function. In Python you can just replace that with .reduce(lambda left, right: left+right).

如果您有任何改进,请发表评论:)

If you have any improvements, please do comment :)

这篇关于使用 Python 获取 Rethinkdb 数据库的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆