如何从Rethinkdb中的Python脚本执行转储

时间:2016-10-10 15:31:38

标签: python rethinkdb

https://www.rethinkdb.com/docs/backup/中所述,使用命令行时,可以使用命令

创建Rethinkdatabase的备份

rethinkdb dump

我有兴趣在Python脚本中做类似的事情,而不是从命令行做。如何实现这一目标?

例如,请考虑RethinkDB十分钟介绍中的以下代码:

import rethinkdb as r
r.connect("localhost", 28015).repl()
r.db("test").table_drop("authors").run()        # This line is included in case the script has already been run before
r.db("test").table_create("authors").run()

r.table("authors").insert([
    { "name": "William Adama", "tv_show": "Battlestar Galactica",
      "posts": [
        {"title": "Decommissioning speech", "content": "The Cylon War is long over..."},
        {"title": "We are at war", "content": "Moments ago, this ship received..."},
        {"title": "The new Earth", "content": "The discoveries of the past few days..."}
      ]
    },
    { "name": "Laura Roslin", "tv_show": "Battlestar Galactica",
      "posts": [
        {"title": "The oath of office", "content": "I, Laura Roslin, ..."},
        {"title": "They look like us", "content": "The Cylons have the ability..."}
      ]
    },
    { "name": "Jean-Luc Picard", "tv_show": "Star Trek TNG",
      "posts": [
        {"title": "Civil rights", "content": "There are some words I've known since..."}
      ]
    }
]).run()

cursor = r.table("authors").run()
for document in cursor:
    print(document)

运行此脚本会生成以下标准输出:

{u'tv_show': u'Battlestar Galactica', u'posts': [{u'content': u'The Cylon War is long over...', u'title': u'Decommissioning speech'}, {u'content': u'Moments ago, this ship received...', u'title': u'We are at war'}, {u'content': u'The discoveries of the past few days...', u'title': u'The new Earth'}], u'id': u'cc5f79c4-230b-4f6d-a642-db124c2ca9ad', u'name': u'William Adama'}
{u'tv_show': u'Star Trek TNG', u'posts': [{u'content': u"There are some words I've known since...", u'title': u'Civil rights'}], u'id': u'dd8b9963-34af-429d-8993-1c6be007367d', u'name': u'Jean-Luc Picard'}
{u'tv_show': u'Battlestar Galactica', u'posts': [{u'content': u'I, Laura Roslin, ...', u'title': u'The oath of office'}, {u'content': u'The Cylons have the ability...', u'title': u'They look like us'}], u'id': u'f3641355-282d-4fd6-ada6-1459246c054f', u'name': u'Laura Roslin'}

我想将这些数据写入Python脚本中的JSON文件。

0 个答案:

没有答案