给定输入CSV (存储在本地或Google云存储中)为
a,b,c
1,2,3
4,5,6如何获取具有值的PCollection
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}而事先不知道CSV报头的名称?
发布于 2021-03-19 08:27:13
这里有两个选项。
(1)您可以使用beam.io.ReadFromText跳过头部,然后使用beam.Map(lambda line: zip(header_names, line.split(','))。这将不会处理引用等(虽然可以调整为这样做,可能使用csv模块,尽管处理多行行将不适用于此方法)。
(2)你可以使用Beam dataframes API来做这件事,例如
from apache_beam.dataframe.io import read_csv
with beam.Pipeline as p:
df = p | beam.dataframe.io.read_csv("/path/to/filepattern")
# Here you can use df as if it were a Pandas dataframe,
# or you can convert it into a PCollection of dicts with
# pcoll = beam.dataframe.convert.to_pcollection(df)https://stackoverflow.com/questions/66689622
复制相似问题