How to get cursor from mqlread() when using Freebase API?

十年热恋 提交于 2019-12-01 12:39:30

问题


I'm using the Python-Freebase module's mqlread(). Using the following query:

query = [{
  "cursor": True,
  "id":     None,
  "type":   "/games/game",
  "mid":    None,
}]

That returns 100 entries, but the result does not include "cursor". If you run the query manually you get something like:

{ "code": "/api/status/ok", "cursor": "eNqFj8FqwzAQRD-mF4sisrtaaaWlhP6H8MHYNTUEW8hpKP36KjgU2kvnMoeZ4THjR923qhKS07wpAvR5VZbYBIL9sE76FDGAs-yCA_FISprL_nWUt5tC383L59ukh9llH_TvpL7Y7rYUdZ4CN9iRITB6vTwnAhriCAwAgXGO8Etkz48dicnvy3ptTGO6OwgsBwIfwJNeHpDWowgh_URO63-M0Z7vM0neGZNL3RrihBKFU_uZS2l3sT-9cpIBaRrnOE8JhaAhQL4B9ZFRmg==", "result": [

{
  "id":   "/en/cities_and_knights_of_catan",
  "mid":  "/m/02hq3",
  "type": "/games/game"
},
....

Which contains the "cursor". But the result from:

freebase.mqlread(query, extended=True)

Will return:

[{u'type': u'/games/game', u'id': u'/en/cities_and_knights_of_catan'}, ...

Which strips out "code" and "cursor". How can I get the "cursor"?


回答1:


I found that the Freebase-Python module actually includes a method that takes care of cursoring for you. Just call mqlreaditer(query, extended=true), and that will return a Python generator that you can iterate through. Behind the scenes the Freebase module will query and keep track of the cursor, return results on demand until a "false" cursor is reached.

Very simple!




回答2:


Support for MQL envelope parameters was dropped, presumably accidentally, in the 1.0.8 release of the Freebase Python library.

If not don't need MQL extensions, you can just drop the parameter extended=True parameter and use mqlreaditer(query).

If you need MQL extensions (or other envelope parameters), you'll have to fall back to 1.0.6 until someone fixes the problem and does a new release.

 sudo easy_install freebase==1.0.6


来源:https://stackoverflow.com/questions/4835141/how-to-get-cursor-from-mqlread-when-using-freebase-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!