Character encoding Apache Beam/GCP数据流编码问题
我正在datalab中“玩”ApacheBeam/dataflow。 我正在尝试从gcs读取csv文件。 使用以下命令创建pcollection时:Character encoding Apache Beam/GCP数据流编码问题,character-encoding,google-cloud-dataflow,apache-beam,Character Encoding,Google Cloud Dataflow,Apache Beam,我正在datalab中“玩”ApacheBeam/dataflow。 我正在尝试从gcs读取csv文件。 使用以下命令创建pcollection时: lines = p | 'ReadMyFile' >> beam.io.ReadFromText('gs://' + BUCKET_NAME + '/' + input_file, coder='StrUtf8Coder') 我得到以下错误: LookupError: unknown encoding: "THE","NAME","O
lines = p | 'ReadMyFile' >> beam.io.ReadFromText('gs://' + BUCKET_NAME + '/' + input_file, coder='StrUtf8Coder')
我得到以下错误:
LookupError: unknown encoding: "THE","NAME","OF","COLUMNS"
似乎列的名称被解释为编码
我不明白怎么了。
如果我没有指定我得到的“编码器”
在apache beam之外,我可以通过从gcs读取文件来处理此错误:
blob = storage.Blob(gs_path, bucket)
data = blob.download_as_string()
data.decode('utf-8', 'ignore')
我读到apachebeam只支持utf8,并且该文件不包含utf8
我应该下载并转换为pcollection吗
有什么建议吗?我建议更改实际文件的编码。如果使用“另存为”保存文件,则可以在excel CSV和regular.txt上选择UTF-8编码作为格式。一旦你这样做了,你需要确保你添加了一行代码,比如
class DoWork(beam.DoFn):
def process(self, text):
text = textfilePcollection.encode('utf-8')
Do other stuff
我不想这样做,因为它不是以代码为中心的,但它以前对我来说是有用的。不幸的是,我没有一个以代码为中心的解决方案。一个可能的攻击是创建一个继承自
编码器的类()
并将其作为参数传递给beam提供的ReadFromText IO transform()
像这样
from apache_beam.io import ReadFromText
with beam.Pipeline(options=pipeline_options) as p:
new_pcollection = ( p | 'Read From GCS' >>
beam.io.ReadFromText('input_file', coder=ISOCoder())
这背后的逻辑在此详述
from apache_beam.coders.coders import Coder
class ISOCoder(Coder):
"""A coder used for reading and writing strings as ISO-8859-1."""
def encode(self, value):
return value.encode('iso-8859-1')
def decode(self, value):
return value.decode('iso-8859-1')
def is_deterministic(self):
return True
from apache_beam.io import ReadFromText
with beam.Pipeline(options=pipeline_options) as p:
new_pcollection = ( p | 'Read From GCS' >>
beam.io.ReadFromText('input_file', coder=ISOCoder())