Python 3.x 将列表转换为PCollection
我目前有一个Python 3.x 将列表转换为PCollection,python-3.x,google-cloud-storage,apache-beam,dataflow,Python 3.x,Google Cloud Storage,Apache Beam,Dataflow,我目前有一个DoFn,它查看一个bucket,并查看该bucket和dir前缀中的所有文件。此DoFn返回一个列表,而不是PCollection。如何将此列表转换为可由DoFnConvertFileNames使用的PCollection # List all the files within a subdir class ListBlobs(beam.DoFn): def start_bundle(self): self.storage_client = stora
DoFn
,它查看一个bucket,并查看该bucket和dir前缀中的所有文件。此DoFn
返回一个列表,而不是PCollection
。如何将此列表转换为可由DoFn
ConvertFileNames
使用的PCollection
# List all the files within a subdir
class ListBlobs(beam.DoFn):
def start_bundle(self):
self.storage_client = storage.Client()
def process(self, prefix):
bucket = self.storage_client.bucket('xxx')
return list(self.bucket.list_blobs(prefix=prefix))
# Convert Blobs into filenames as patterns
class ConvertFileNames(beam.DoFn):
def process(self, blob):
return 'gs://' + blob.bucket.name + blob.name
如中所述,Beam DoFn的process方法返回要放置到下游PCollection中的元素的iterable。因此,在您的示例中,如果我有一个前缀的PCollection,称它为prefix\u pcoll
,那么我可以编写
blobs_pcoll = prefix_pcoll | beam.ParDo(ListBlobs())
而blobs\u pcoll
将包含带有此前缀的blob列表(即list(self.bucket.list\u blobs(prefix=prefix))在所有前缀上的组合)。然后你就可以写了
converted = blobs_pcoll | beam.ParDo(ConvertFileNames())
你也可以写
converted = blobs_pcoll | beam.Map(
lambda blob: 'gs://' + blob.bucket.name + blob.name)
你可能还想调查一下