我创建了一个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'