DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Create a Spark DataFrame: Read and Parse Multiple (Small) Files

We take a look at how to work with data sets without using UTF -16 encoded files in Apache Spark using the Scala language.

sourabh mehta user avatar by
sourabh mehta
·
Apr. 08, 19 · Tutorial
Like (8)
Save
Tweet
Share
14.77K Views

Join the DZone community and get the full member experience.

Join For Free

This is a very specific use case we faced while loading small CSV files in bulk with UTF -16 encoded files. After reading data from a source location, one might see junk/special characters while using DF.show() which may lead to unexpected results.

So, below, I've shown how to read a file in Spark without using the deafault UTF-8 encoded file.

Soln: We have used the below code to solve this; here folderPath can be any HDFS location

val rdd_file = sparkSession.sparkContext.binaryFiles(folderPath, 2).
mapValues(content => new String(content.toArray(), StandardCharsets.UTF_16))

// rdd_file is created as paired RDD like below

//val rdd_file: RDD[(String, String)] à first String represents filename

//second String represents: String content of file

// From rdd_file we can perform map transformation on each file to get RDD as RDD[List[Employee]]

val rddOfListPojo= rdd_file.map{x =>

val fileData = x._2

// parse fileData to get list of Pojo w.r.t business logic

listOfEmployee

}

val rddPojo = rddOfListPojo.flatMap(x => x)

val dsCombined = rddPojo.toDS()

Note: We have created a combined data set called dsCombined (as mentioned above) from the small UTF-16 encoded files.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Easy Smart Contract Debugging With Truffle’s Console.log
  • An Introduction to Data Mesh
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • Understanding gRPC Concepts, Use Cases, and Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: