DZone
Big Data Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Big Data Zone > Apache Beam Working With Files

Apache Beam Working With Files

The article explains how to read, write data to and from the file in Apache Beam with a pipeline where the ‘Employees.csv’ file be read/filtered/write to a new file.

Sameer Shukla user avatar by
Sameer Shukla
CORE ·
Feb. 16, 22 · Big Data Zone · Tutorial
Like (3)
Save
Tweet
4.38K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

The article explains how to read, write data to and from the file in Apache Beam with a proper pipeline example. The reading data from the file is done through the ‘ReadFromText’ transform and writing to a new file is done using the ‘WriteToText’ transform. The first article explains how to read data from the file and how to write to a file, in the latter part of the article a Pipeline will be created where ‘Employees.csv’ file be read, filtered based on age, extract employee’s first name, last name, and age and write to a new file. Overall, the pipeline looks like this:

Data pipeline

Reading From a File

In the article, we are using the file from the site we have downloaded a 100 records file and named it ‘Employees.csv’ through ‘ReadFromText’ will read file from the disk. The below code showcases the same:

Code for reading from a file


Output

Code output for reading from a file


Writing to File

The ‘WriteToText’ transform is used to write the data to the file, the program below reads the data from the file and writes to the ‘out.csv’ file. 

Code for writing to a file

Output

Code output for writing to a file


Pipeline

The pipeline code contains two functions one for filtering out the rows where the employee age is greater than 40 and the second one is used to map only firstname, lastname, and age of the employee. 

Pipeline code for filtering and mapping data

In both functions, we are accessing records based on the index. The full pipeline code is mentioned below:

 Full Pipeline code


Contents of Generated File

Generated file content

Summary

In the article we have explored how to read, write data to and from the file, the article also explained the full pipeline code where the filtering, mapping on the data is performed and written to the new file.  

Apache Beam

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Brief History of the DMCA
  • Refactor Switch to a One-Liner
  • Is Your Code DRY or WET?
  • Architecture as Code With C4 and Plantuml

Comments

Big Data Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo