[英]Error while running python dataflow job:
我正在嘗試使用GCP Dataflow Python使用第一個字符來處理輸入文本文件。 如果條目的第一個字符是“ A”,我想將文件存儲在A.txt中,依此類推。 同樣,我有一個與每個字符相關的數字。 我為此存儲了兩個哈希圖。 以下是我的代碼:
splitHashMap={'A':1,'F':4, 'J':4, 'Z':4, 'G':10, 'I':11};
fileHashMap= {'A':'A.txt','B':'B.txt','F':'F.txt','J':'J.txt','Z':'Z.txt','G':'G.txt','I':'I.txt'};
def to_table_row(x):
firstChar=x[0][0];
global splitHashMap
global fileHashMap
print splitHashMap[firstChar];
x | WriteToText(fileHashMap[firstChar]);
return {firstChar}
錯誤與WriteToText函數有關,如下所示:
PTransform Create: Refusing to treat string as an iterable. (string=u'AIGLM0012016-02-180000000112016-02-18-12.00.00.123456GB CARMB00132') [while running 'ToTableRows']
有人可以幫我解決這個問題嗎?
編輯:包含管道的代碼的其余部分如下:
arser = argparse.ArgumentParser()
parser.add_argument('--input',
dest='input',
default='gs://dataflow-samples/shakespeare/kinglear.txt',
help='Input file to process.')
parser.add_argument('--output',
dest='output',
help='Output file to write results to.')
known_args, pipeline_args = parser.parse_known_args(None)
pipeline_options = PipelineOptions(pipeline_args)
pipeline_options.view_as(SetupOptions).save_main_session = True
p = beam.Pipeline(options=pipeline_options)
lines = p | 'read' >> ReadFromText(known_args.input)
lines | 'ToTableRows' >> beam.Map(to_table_row);
result = p.run()
我要求您立即幫助我解決問題。 我用來調整python文件的命令是:
python File_parse.py ---input temp.txt
Temp.txt如下:
Aasadasd asdasd adsad af
Jdsad asdasd asd as
A asdd ad agfsfg sfg
Z afsdfrew320pjpoji
Idadfsd w8480ujfds
理想的輸出是所有以“ A”開頭的文件都轉到“ A.txt”,“ B”轉到“ B.txt”,依此類推。 如果您在響應中編寫了代碼,那就太好了。
您對WriteToText
使用不合適。 您不能將字符串傳遞給PTransform。 相反,您需要將PCollections傳遞到PTransforms中。 在下面的代碼中,您可以為第一個字符的每種情況創建單獨的PCollection,並將其傳遞給
在這種情況下,您可以執行以下操作:
file_hash_map= {'A':'A.txt','B':'B.txt','F':'F.txt',
'J':'J.txt','Z':'Z.txt','G':'G.txt','I':'I.txt'}
existing_chars = file_hash_map.keys()
class ToTableRowDoFn(beam.DoFn):
def process(self, element):
first_char = element[0][0]
if first_char in file_hash_map:
yield pvalue.TaggedOutput(first_char, element)
else:
# When the first char of the word is not from the allowed
# characters, we just send it to the main output.
yield element
lines = p | 'read' >> ReadFromText(known_args.input)
multiple_outputs = (
lines |
'ToTableRows' >> beam.ParDo(ToTableRowDoFn())
.with_outputs(*existing_chars, main='main'));
for pcollection_name in existing_chars:
char_pcollection = getattr(multiple_outputs, pcollection_name)
char_pcollection | WriteToFile(file_hash_map[pcollection_name])
此代碼的症結在於for
循環,在該循環中,我們遍歷每個輸出PCollections,並將它們的內容分別寫入另一個文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.