繁体   English   中英

使用哪个 apache-beam 功能来读取管道中的第一个 function 并获取 output

[英]Which apache-beam feature to use to just read a function as first in the pipeline and take the output

我正在尝试创建一个数据流管道并将其部署在云环境中。 我有如下代码,它试图从 GCS 存储桶中的特定文件夹中读取文件:

def read_from_bucket():
    file_paths=[]
    client = storage.Client()
    bucket = client.bucket("bucket_name")
    blobs_specific = list(bucket.list_blobs(prefix="test_folder/"))
    for blob in blobs_specific: 
        file_paths.append(blob)       
    return file_paths   

上面的 function 返回该 GCS 文件夹中存在的文件路径列表。 现在这个列表被发送到下面的代码,它将根据扩展名过滤文件并将其存储在 GCS 存储桶中的相应文件夹中。

def write_to_bucket():
  
    client = storage.Client()  

    for blob in client.list_blobs("dataplus-temp"):
        source_bucket = client.bucket("source_bucket")
        source_blob = source_bucket.blob(blob.name) 
    
        file_extension = pathlib.Path(blob.name).suffix      
        
        if file_extension == ".json":       
               
                destination_bucket=client.bucket("destination_bucket")
                new_blob = source_bucket.copy_blob(source_blob,destination_bucket,'source_bucket/JSON/{}'.format(source_blob))                         
        
        elif file_extension == ".txt":
           
                destination_bucket=client.bucket("destination_bucket")
                new_blob = source_bucket.copy_blob(source_blob,destination_bucket,'Text/{}'.format(source_blob))


 

我必须使用数据流管道执行上述实现,这样文件路径有 go 到数据流管道,它应该存储在相应的文件夹中。 我创建了一个如下所示的数据流管道,但不确定我是否使用了正确的 Ptrasformations。

pipe= beam.Pipeline()( 
    pipe
    |"Read data from bucket" >> beam.ParDo(read_from_bucket)
    |"Write files to folders" >> beam.ParDo(write_to_bucket)
)

pipe.run()

  • 执行如下:
python .\\filename.py 
  --region asia-east1 
  --runner DataflowRunner 
  --project proejct_name 
  --temp_location gs://bucket_name/tmp 
  --job_name test

执行后出现以下错误:

return inputs[0].windowing
AttributeError: 'PBegin' object has no attribute 'windowing'

我已经检查了 apache-beam 文档,但不知何故无法理解。 我是 apache-beam 的新手,刚开始作为初学者,请考虑这个问题是否愚蠢。

请帮助我解决这个问题。

要解决您的问题,您必须使用现有的Beam IO来读取和写入Cloud Storage ,例如:

import apache_beam as beam
from apache_beam.io import ReadFromText, WriteToText

def run():
  pipeline_options = PipelineOptions()

  with beam.Pipeline(options=pipeline_options) as p:

    (
      p
      | "Read data from bucket" >> ReadFromText("gs://your_input_bucket/your_folder/*")
      | "Write files to folders" >> WriteToText("gs://your_dest_bucket")
    )

if __name__ == "__main__":
   logging.getLogger().setLevel(logging.INFO)
   run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM