YQL - No definition found for Table

送分小仙女□ 提交于 2019-12-08 12:23:26

问题


My code:

import yql
y = yql.Public()
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
y.execute(query)

Result:

yql.YQLError: No definition found for Table yahoo.finance.option_contracts

I know that the table exists because I can test the query at http://developer.yahoo.com/yql/console/ and it works. What am I missing?

Update: I posted the url to the console but not the query I tried in the console. The query is now attached.

http://goo.gl/mNXwC

回答1:


Since the yahoo.finance.option_contracts table is a Community Open Data Table you will want to include it as part of the environment for the query. The easiest way to do that is to load up the environment file for all community tables; just like clicking "Show Community Tables" in the YQL console.

One would normally do that by specifying an env=... parameter in the YQL query URL, or (as you have done) with a use clause in the query itself.

The Python library that you are using lets you pass in the environment file as an argument to execute().

import yql
y = yql.Public()
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
y.execute(query, env="store://datatables.org/alltableswithkeys")

Here's an example of extending yql.Public to be able to define the default environment on instantiation.

class MyYql(yql.Public):

    def __init__(self, api_key=None, shared_secret=None, httplib2_inst=None, env=None):
        super(MyYql, self).__init__(api_key, shared_secret, httplib2_inst)
        self.env = env if env else None

    def execute(self, query, params=None, **kwargs):
        kwargs["env"] = kwargs.get("env", self.env)
        return super(MyYql, self).execute(query, params, **kwargs);

It can be used like:

y = MyYql(env="store://datatables.org/alltableswithkeys")
query = 'SELECT * FROM yahoo.finance.option_contracts WHERE symbol="SPY"'
r = y.execute(query)

You can still override the env in an individual call to y.execute() if you need to.




回答2:


Amending query to the following is what works.

query = 'use "http://www.datatables.org/yahoo/finance/yahoo.finance.option_contracts.xml" as foo; SELECT * FROM foo WHERE symbol="SPY"'

More elegant solutions might exist. Please share if such do. Thanks.



来源:https://stackoverflow.com/questions/16783175/yql-no-definition-found-for-table

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