Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误:";缺少必需的参数";-在BigQuery中获取匿名表时_Python_Google Bigquery - Fatal编程技术网

Python 错误:";缺少必需的参数";-在BigQuery中获取匿名表时

Python 错误:";缺少必需的参数";-在BigQuery中获取匿名表时,python,google-bigquery,Python,Google Bigquery,我试图查询在BigQuery中查询表数据集后创建的匿名表。我试图使用中的示例使用Jobs.get()查找匿名表名,但遇到了一个错误 谷歌BigQuery分析示例(第209页): 查询1: class QueryHandler(webapp2.RequestHandler): credentials = GoogleCredentials.get_application_default() service = discovery.build('bigquery', 'v2', credentia

我试图查询在BigQuery中查询表数据集后创建的匿名表。我试图使用中的示例使用
Jobs.get()
查找匿名表名,但遇到了一个错误

谷歌BigQuery分析示例(第209页):

查询1:

class QueryHandler(webapp2.RequestHandler):
credentials = GoogleCredentials.get_application_default()
service = discovery.build('bigquery', 'v2', credentials=credentials)
def query1(self):
    myquery = {'configuration': {
        'query': {
            'query': 'SELECT DISTINCT user_id FROM `app.mydataset.mytable`',
            'destinationTable': {
            'projectId': projectId,
            'datasetId': datasetId,
            'tableId': 'tableId'},
            'useLegacySql': False
                 }
            }
    }

    response = service.jobs().query(projectId=projectId, body=myquery).execute()
    job = service.jobs().get(**response['jobReference']).execute()
    # both versions of this variable (destination_table) produce the same error message
    # destination_table = job['configuration']['query']['destinationTable']
    destination_table = job['destinationTable']

    table = service.jobs().get(projectId=destination_table['projectId'],
                              datasetId=destination_table['datasetId'],
                              tableId=destination_table['tableId']).execute()
    return table
def query2(self):
....
query: SELECT * FROM [anonymous table from query 1]
错误:

class QueryHandler(webapp2.RequestHandler):
credentials = GoogleCredentials.get_application_default()
service = discovery.build('bigquery', 'v2', credentials=credentials)
def query1(self):
    myquery = {'configuration': {
        'query': {
            'query': 'SELECT DISTINCT user_id FROM `app.mydataset.mytable`',
            'destinationTable': {
            'projectId': projectId,
            'datasetId': datasetId,
            'tableId': 'tableId'},
            'useLegacySql': False
                 }
            }
    }

    response = service.jobs().query(projectId=projectId, body=myquery).execute()
    job = service.jobs().get(**response['jobReference']).execute()
    # both versions of this variable (destination_table) produce the same error message
    # destination_table = job['configuration']['query']['destinationTable']
    destination_table = job['destinationTable']

    table = service.jobs().get(projectId=destination_table['projectId'],
                              datasetId=destination_table['datasetId'],
                              tableId=destination_table['tableId']).execute()
    return table
def query2(self):
....
query: SELECT * FROM [anonymous table from query 1]
内部服务器错误

服务器出现错误或无法执行请求的操作

HttpError:https://www.googleapis.com/bigquery/v2/projects/app_id/queries?alt=json 返回“缺少必需参数”>

我的问题:

class QueryHandler(webapp2.RequestHandler):
credentials = GoogleCredentials.get_application_default()
service = discovery.build('bigquery', 'v2', credentials=credentials)
def query1(self):
    myquery = {'configuration': {
        'query': {
            'query': 'SELECT DISTINCT user_id FROM `app.mydataset.mytable`',
            'destinationTable': {
            'projectId': projectId,
            'datasetId': datasetId,
            'tableId': 'tableId'},
            'useLegacySql': False
                 }
            }
    }

    response = service.jobs().query(projectId=projectId, body=myquery).execute()
    job = service.jobs().get(**response['jobReference']).execute()
    # both versions of this variable (destination_table) produce the same error message
    # destination_table = job['configuration']['query']['destinationTable']
    destination_table = job['destinationTable']

    table = service.jobs().get(projectId=destination_table['projectId'],
                              datasetId=destination_table['datasetId'],
                              tableId=destination_table['tableId']).execute()
    return table
def query2(self):
....
query: SELECT * FROM [anonymous table from query 1]
  • 为什么我会犯这个错误?(我遵循了这个例子,我看不出我遗漏了什么)
  • 如何使用Python从第二个查询中的第一个查询传递匿名表名?例如:
  • 查询2:

    class QueryHandler(webapp2.RequestHandler):
    credentials = GoogleCredentials.get_application_default()
    service = discovery.build('bigquery', 'v2', credentials=credentials)
    def query1(self):
        myquery = {'configuration': {
            'query': {
                'query': 'SELECT DISTINCT user_id FROM `app.mydataset.mytable`',
                'destinationTable': {
                'projectId': projectId,
                'datasetId': datasetId,
                'tableId': 'tableId'},
                'useLegacySql': False
                     }
                }
        }
    
        response = service.jobs().query(projectId=projectId, body=myquery).execute()
        job = service.jobs().get(**response['jobReference']).execute()
        # both versions of this variable (destination_table) produce the same error message
        # destination_table = job['configuration']['query']['destinationTable']
        destination_table = job['destinationTable']
    
        table = service.jobs().get(projectId=destination_table['projectId'],
                                  datasetId=destination_table['datasetId'],
                                  tableId=destination_table['tableId']).execute()
        return table
    
    def query2(self):
    ....
    query: SELECT * FROM [anonymous table from query 1]
    
  • 为什么我会犯这个错误?(我遵循了这个例子,我看不出我遗漏了什么)
  • 您的请求正文的格式不正确。您不需要“配置”或“查询”对象包装您所拥有的内容

    尝试:

    作为元注释,我们(BigQuery团队)知道“Required parameter is missing”错误消息过于模糊,无法调试,并导致类似这样的混乱情况。此外,无法识别的参数(如“configuration”对象)也会被忽略,因此如果您在请求中错误地命名了参数,则很容易出现“Required parameter is missing”错误。我们希望在将来的API更新中解决这个问题


  • 如何使用Python从第二个查询中的第一个查询传递匿名表名
  • 假定传入了预期的
    jobReference
    ,则应该能够从中检索目标表

    但是,请注意,在另一个查询中使用此匿名表是一种错误,不能保证:

    此方法的查询结果保存到一个临时表中,该临时表在查询运行约24小时后删除。您可以通过调用bigquery.tabledata.list(table_引用)或bigquery.jobs.getQueryResults(job_引用)来读取此结果表表和数据集名称是非标准的,不能用于任何其他API,因为其行为可能不可预测。

    相反,您最好传入一个显式的目标表,该表只能使用而不能使用
    作业。query
    。查找参数
    configuration.query.destinationTable


    如果您想让这些目标表保留一段时间,可以将它们放入数据集中,在一段时间(一小时、一天或…)后保存。

    谢谢您的帮助。我试图理解BigQueryAPI文档,但不幸的是它相当有限。我根据您对问题1的建议编辑了我的代码,它解决了错误。关于使用匿名表,我不会使用此方法,但是,我有3个查询背靠背进行,它们取决于保存到每个表的结果
    jobs.insert
    如果与
    jobs.query的功能相同,但等待它保存到一个永久表中,那么查询该表本身就有挑战性。如果遇到问题,我会重新考虑使用
    jobs.insert
    。我认为每24小时一次cron任务可以处理匿名表。不过我很快就会看到的。再次感谢。请注意,
    jobs.query
    基本上与
    jobs.insert
    执行相同的操作,但使用系统生成的名称保存到目标表。API之间的主要区别在于,查询是一个挂起请求,在作业完成和jobs.insert启动异步作业时返回。如果您想要类似的代码< Case>作业。查询< /代码>,您可以考虑使用<代码>作业。插入后面跟着一个<代码>作业。GETQueRebug结果< /C> >,GETQueRebug结果将等待类似查询的完成。更多细节。哦,那绝对是我想要实现的。我确实设法让匿名表在我的工作环境中工作,但是一个被重写的永久表将使用更少的代码,并且不必担心奇怪的行为。:-)太好了,我会尽快实施你的建议。再次感谢。