使用 python 查询 cloudant

发布于 2024-12-10 11:26:17 字数 773 浏览 0 评论 0原文

对此可能有一个明显的答案,但我似乎无法在任何地方找到它:查询存储在 cloudant 服务器上的 couchdb 数据库的最佳方法是什么?我尝试使用临时视图,按照 couchdb.py 说明:

>>> db['johndoe'] = dict(type='Person', name='John Doe')
>>> db['maryjane'] = dict(type='Person', name='Mary Jane')
>>> db['gotham'] = dict(type='City', name='Gotham City')
>>> map_fun = '''function(doc) {
...     if (doc.type == 'Person')
...         emit(doc.name, null);
... }'''
>>> for row in db.query(map_fun):
...     print row.key
John Doe
Mary Jane

虽然这适用于本地托管数据库,但使用 CloudAnt 时它会返回错误:

couchdb.http.ServerError: (403, ('forbidden', 'temp views are disabled on Cloudant'))

我已阅读有关查询的 cloudant 教程,但建议的查询语法似乎很笨拙,而且不清楚如何实现将其应用到Python中!有一个简单的方法可以解决这个问题吗?

There may be an obvious answer to this, but I can't seem to find it anywhere: what's the best way to query couchdb databases stored on cloudant servers? I try using temporary views, a la the couchdb.py instructions:

>>> db['johndoe'] = dict(type='Person', name='John Doe')
>>> db['maryjane'] = dict(type='Person', name='Mary Jane')
>>> db['gotham'] = dict(type='City', name='Gotham City')
>>> map_fun = '''function(doc) {
...     if (doc.type == 'Person')
...         emit(doc.name, null);
... }'''
>>> for row in db.query(map_fun):
...     print row.key
John Doe
Mary Jane

While this works on locally hosted databases, with CloudAnt it returns the error:

couchdb.http.ServerError: (403, ('forbidden', 'temp views are disabled on Cloudant'))

I've read the cloudant tutorial on querying but the querying syntax proposed seems clumsy and it's not obvious how to work it into python! Is there an easy way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖伴 2024-12-17 11:26:17

这就是我用 python 添加记录的方式。

import requests
import json

doc = {
  'username':'kerrie',
  'high_score':550,
  'level':3
}

auth = ('username', 'password')
headers = {'Content-type': 'application/json'}

post_url = "https://account.cloudant.com/database/kerrie".format(auth[0])

r = requests.put(post_url,  auth=auth,  headers=headers,  data=json.dumps(doc))
print json.dumps(r.json(), indent=1)

这就是我在Python中查询10条记录的方法。

import requests
import json
auth = ('username', 'password')
get_url = "https://account.cloudant.com/database/_all_docs?limit=10".format(auth[0])
r = requests.get(get_url, auth=auth)
print json.dumps(r.json(), indent=1)

This is how I am adding a record with python.

import requests
import json

doc = {
  'username':'kerrie',
  'high_score':550,
  'level':3
}

auth = ('username', 'password')
headers = {'Content-type': 'application/json'}

post_url = "https://account.cloudant.com/database/kerrie".format(auth[0])

r = requests.put(post_url,  auth=auth,  headers=headers,  data=json.dumps(doc))
print json.dumps(r.json(), indent=1)

This is how i query 10 records in Python.

import requests
import json
auth = ('username', 'password')
get_url = "https://account.cloudant.com/database/_all_docs?limit=10".format(auth[0])
r = requests.get(get_url, auth=auth)
print json.dumps(r.json(), indent=1)
星星的軌跡 2024-12-17 11:26:17

Cloudant 禁止临时视图的原因是它们无法扩展。您将需要创建一个包含已定义视图的设计文档。以下是设计文档及其上定义的视图的链接:

http://max.ic.ht/_utils/document.html?action/_design/action

我不确定如何在 couchdb.py 中执行此操作,但您可能想尝试不同的 python 库。 中创建视图的部分的链接

这是有关在 couchquery http://mikeal.github.com/ couchquery/#creating-views

The reason Cloudant forbids temp views is because they do not scale. You will need to create a design document with defined views in it. Here is a link to what a design document is like with views defined on it:

http://max.ic.ht/_utils/document.html?action/_design/action

I am not sure about how to do it in couchdb.py, but you might want to try a different python library. Here is a link to the section about creating views in couchquery

http://mikeal.github.com/couchquery/#creating-views

画▽骨i 2024-12-17 11:26:17

只是注意到 Cloudant 现在有一个官方的 python 库, https://github.com/cloudant/python-cloudant

Just noting that Cloudant now has an official python library, https://github.com/cloudant/python-cloudant.

任谁 2024-12-17 11:26:17

您可能应该使用 couchdbkit。它使设置视图变得容易。我认为您不能再在 Cloudant 中使用临时视图。

You should probably use couchdbkit. It makes setting up views easy. I don't think you can use temporary views in Cloudant anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文