【问题标题】:importing and using google cloud packages导入和使用谷歌云包
【发布时间】:2019-11-25 09:33:13
【问题描述】:

我创建了一个管道,它会输出一个数字列表。 这些数字流入 ParDo,在 ParDo 中我使用数字查询 Bigquery 表,然后返回查询结果。

这在本地有效。 Linux、Python 3.7、google-cloud-bigquery 1.22.0

当我将作业上传到数据流时,事情变得有趣起来。 我在顶层所做的一切在下面的功能中都无效。所以我必须在每个函数中导​​入所有我用过的包才能可用。

这太丑了,我怀疑我做错了什么。但是什么?

所以我得到一个这样的函数:

def flatten(elements):
    import datetime
    for element in elements['transactionids']:
        print('flatten: ' + str(element) + ' ' + datetime.datetime.now().isoformat())
        yield element

我得到一个像这样的“DoFn 类”:

class TransformTransaction(beam.DoFn):
    def setup(self):
        print("This will never run. Why?")

    def start_bundle(self):
        print("Bundle Start")
        from google.cloud import bigquery
        self.client = bigquery.Client()
        self.dataset_id = 'mydataset'
        self.table_id = 'testhijs'
        self.table_ref = self.client.dataset(self.dataset_id).table(self.table_id)
        self.table = self.client.get_table(self.table_ref)

   def retrieveTransactionData(self, transactionID):
        query = f"select transactionID, someNr from `thijs-dev.thijsset.thijstable` " \
                f"where transactionID = {transactionID}"

    query_job = self.client.query(
        query,
        location="EU",
    )  
    print(query_job)

    transactions = []

    for row in query_job:
        transactions.append(row)

    return transactions

【问题讨论】:

    标签: python python-3.x google-cloud-dataflow apache-beam


    【解决方案1】:

    使用管道配置--save_main_session。这将导致全局命名空间的状态被腌制并加载到 Cloud Dataflow 工作器上。

    Python 中的完整示例:

    import apache_beam as beam
    from apache_beam.options.pipeline_options import PipelineOptions
    from apache_beam.options.pipeline_options import SetupOptions
    import argparse
    
    def run(argv=None):
        parser = argparse.ArgumentParser()
        known_args, pipeline_args = parser.parse_known_args(argv)
        pipeline_args.extend([
              '--runner=DataflowRunner',
              '--project=proj',
              '--region=region',
              '--staging_location=gs://bucket/staging/',
              '--temp_location=gs://bucket/temp/',
              '--job_name=name',
              '--setup_file=./setup.py'
              ]) 
    
        pipeline_options = PipelineOptions(pipeline_args)
        pipeline_options.view_as(SetupOptions).save_main_session = True #this is what you need to include 
    

    编辑:link to doc

    【讨论】:

    • 很好的东西,谢谢。要在 shell 脚本中使用它,我使用选项 --save_main_session 没有 =true
    • 糟糕,我将进行编辑。如果这解决了您的问题,请考虑支持并接受它:) 干杯
    • 更短的选项是:pipeline_options = PipelineOptions(pipeline_args, save_main_session=True)
    猜你喜欢
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多