栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将CSV转换为Apache Beam数据流中的字典

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将CSV转换为Apache Beam数据流中的字典

编辑:从2.12.0版开始,Beam附带了新的

fileio
转换,使您可以从CSV读取数据而不必重新实现源代码。您可以这样做:

def get_csv_reader(readable_file):  # You can return whichever kind of reader you want here  # a DictReader, or a normal csv.reader.  if sys.version_info >= (3, 0):    return csv.reader(io.TextIOWrapper(readable_file.open()))  else:    return csv.reader(readable_file.open())with Pipeline(...) as p:  content_pc = (p     | beam.io.fileio.MatchFiles("/my/file/name")     | beam.io.fileio.ReadMatches()     | beam.Reshuffle()  # Useful if you expect many matches     | beam.FlatMap(get_csv_reader))

我最近为Apache
Beam编写了一个测试。您可以查看Github存储库。


旧的答案 依赖于重新实现源。这已不再是推荐的主要方法:)

想法是有一个返回已解析的CSV行的源。您可以通过对

FilebasedSource
类进行子类化以包括CSV解析来实现。特别是,该
read_records
函数将如下所示:

class MyCsvFileSource(apache_beam.io.filebasedsource.FilebasedSource):  def read_records(self, file_name, range_tracker):    self._file = self.open_file(file_name)    reader = csv.reader(self._file)    for rec in reader:      yield rec


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号