全局名称'bigquery'未定义。

3
我创建了一个Google Dataflow工作,但是我一直收到“全局名称'bigquery'未定义”的提示,即使我已经导入了所需的变量。

这是我的导入列表:

from __future__ import absolute_import

import argparse
import logging
import ast
import json

import apache_beam as beam
from apache_beam.io import ReadFromText, WriteToText 
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import SetupOptions
from apache_beam.options.pipeline_options import StandardOptions
from google.cloud import bigquery

这就是返回错误的类:

class CheckExistance(beam.DoFn):

    def __init__(self, table):
        self.table = table.replace(":", ".")

    def process(self, element):

        client = bigquery.Client()
        date = element['date'].split(" ")[0]

        query_job = client.query("""
        QUERY """ % (self.table, date))

        yield element

你们知道是什么导致了这个错误吗?顺便说一下,只有在部署到Google的Dataflow作业时才会出现这个错误,在本地运行正常。

编辑:

我通过将我的导入位置改为函数内部来解决了最初的问题,像这样:

class CheckExistance(beam.DoFn):

    def __init__(self, table):
        self.table = table.replace(":", ".")

    def process(self, element):
        from google.cloud import bigquery
        client = bigquery.Client()
        date = element['date'].split(" ")[0]

        query_job = client.query("""
        QUERY""" % (self.table, date))

        yield element

但是现在我遇到了一个错误,错误信息为“'client' object has no attribute 'query'”,尽管我的数据流作业的软件包已经更新并且在本地运行时没有任何问题。

错误信息:

AttributeError: 'Client' object has no attribute 'query'

1个回答

0

我猜你需要启用BigQuery

资源 / 高级Google服务 / 启用BigQuery

编辑:查看评论以查看故障排除、发现和有效方法。


它已经启用了,我能够修复最初的错误,但现在我遇到了另一个错误。 - Pedro Daumas
1
@PedroDaumas 比较本地的bigquery版本与部署的版本。 - Hassan Voyeau
@PedroDaumas 你已经用你的项目 ID 替换了 PROJECT_ID 吗? - Hassan Voyeau
1
@PedroDaumas client.run_async_query或run_sync_query可用吗? - Hassan Voyeau
运行同步查询() 已经成功!非常感谢。 - Pedro Daumas
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接