执行多个查询到Freebase的单个调用

时间:2015-01-29 09:41:51

标签: python-2.7 semantic-web freebase mql

我希望将多个查询的结果转化为对freebase的单个调用,本章http://mql.freebaseapps.com/ch04.html就是这样。我正在使用python进行查询。我想像这样查询

{                                       # Start the outer envelope
  "q1": {                               # Query envelope for query named q1
    "query":{First MQL query here}      # Query property of query envelope
  },                                    # End of first query envelope
  "q2": {                               # Start query envelope for query q2
    "query":[{Second MQL query here}]   # Query property of q2
  }                                     # End of second query envelope
}   

并获得像

这样的答案
{
  "q1": {
    "result":{First MQL result here},
    "code": "/api/status/ok"
  },
  "q2": {
    "result":[{Second MQL result here}],
    "code": "/api/status/ok"
  },
  "status": "200 OK",
  "code": "/api/status/ok",
  "transaction_id":[opaque string value]
}

在该链接上指定。我也遇到了关于SO的一些问题,它们是 -

但他们似乎正在使用旧的API,即“api.freebase.com”。更新后的API为“www.googleapis.com/freebase”

我尝试了以下代码,但它不起作用。

import json
import urllib

api_key = "freebase_api_key"
service_url = 'https://www.googleapis.com/freebase/v1/mqlread'
query1 = [{'id': None, 'name': None, 'type': '/astronomy/planet'}]
query2 = [{'id': None, 'name': None, 'type': '/film/film'}]
envelope = {
    'q1':query1,
    'q2':query2

}

encoded = json.dumps(envelope)
params = urllib.urlencode({'query':encoded})
url = service_url + '?' + params
print url
response = json.loads(urllib.urlopen(url).read())
print response

我收到错误

{u'error': {u'code': 400, u'message': u'Type /type/object does not have property q1', u'errors': [{u'domain': u'global', u'message': u'Type /type/object does not have property q1', u'reason': u'invalid'}]}}

如何将多个查询嵌入到单个MQL查询中

1 个答案:

答案 0 :(得分:0)

我建议查看适用于Google API的Python客户端库的Batch功能。