
61
2
장
TFX - 텐서플로 익스텐디드
# 아파치 빔 파이프라인을 설정합니다.
with
beam.Pipeline(options=pipeline_options)
as
p:
# 텍스트 파일을 읽거나 파일 패턴을 PCollection으로 변환합니다.
# 텍스트 파일을 읽어서 데이터 콜렉션을 만듭니다.
lines = p | ReadFromText(input_file)
# 각 단어의 등장 횟수
# 콜렉션에서 변환을 수행합니다.
counts = (
lines
| ‘Split’ >> beam.FlatMap(
lambda
x: re.findall(r’[A-Za-z
\’
]+’, x))
| ‘PairWithOne’ >> beam.Map(
lambda
x: (x, 1))
| ‘GroupAndSum’ >> beam.CombinePerKey(sum))
# 각 단어의 등장 횟수를 문자열로 변환해 PCollection에 저장합니다.
def
format_result(word_count):
(word, count) = word_count
return
“{}: {}”.format(word, count)
output = counts | ‘Format’ >> beam.Map(format_result)
# “Write” 트랜스폼 명령으로 결과를 출력합니다.
output | WriteToText(output_file) ...