Apache Beam Python ReadFromText正则表达式

2024-04-25 23:05:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一种情况,我想从地面军事系统读取两天的数据。我的文件夹结构是sensors/<date>/<hash>/x.csv.gz,我希望能够读取“20171104”和“20171105”的文件。使用regex sensors/[20171104,20171105]/<hash>/*不起作用。有人知道使用beam.io.ReadFromText功能?在


Tags: 文件csv数据文件夹date系统情况hash
1条回答
网友
1楼 · 发布于 2024-04-25 23:05:14

我已经知道了如何在不使用通配符的情况下读取预期日期的数据,而是编写了一个python函数。其思想是创建一个包含所有读取操作的数组,然后将该数组展平并将其用作管道的输入。在

    def read_files(pipeline, intended_day):

        collections = []
        previous_day = (datetime.strptime(intended_day, '%Y%m%d') - timedelta(days=1)).strftime('%Y%m%d')

        days = [intended_day, previous_day]
        path = "gs://sensors/{}/<hash>/*"
        for day in days:
            try:
                file_name = path.format(day)
                collection = pipeline | ('Read Past for %s' % day) >> beam.io.ReadFromText(file_name)
                collections.append(collection)
            except IOError:
                logging.error("Failed to read for day %s" % day)

        return collections

然后像这样调用管道中的函数:

^{pr2}$

相关问题 更多 >