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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Thermometer Continuation in Scala
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech
  • DataWeave: Play With Dates (Part 1)

Trending

  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Scalability 101: How to Build, Measure, and Improve It
  1. DZone
  2. Coding
  3. Languages
  4. How to Use UDF in Spark Without Register Them

How to Use UDF in Spark Without Register Them

This article will show a code snippet on how to use UDF in Spark without registering them.

By 
sourabh mehta user avatar
sourabh mehta
·
Jun. 03, 20 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free

Here, we will demonstrate the use of UDF via a small example.

Use Case: We need to change the value of an existing column of DF/DS to add some prefix or suffix to the existing value in a new column.

// code snippet how to create UDF in Spark without registering them

Scala
 




x
15


 
1
import org.apache.spark.sql.types._
2
import org.apache.spark.sql.functions._
3

          
4
val rowKeyGenerator = udf((n: String) =>
5
{
6

          
7
  val r = scala.util.Random
8
  val randomNB =  r.nextInt( (100) ).toString()
9
  val deviceNew = randomNB.concat(n)
10
  deviceNew
11
}, StringType)
12

          
13
// "Name" is column name of type string at source DF.
14
val ds2=dfFromFile.withColumn("NameNewValue",rowKeyGenerator(col("Name")))
15
ds2.show()



Note: We can also change the type from String to any other supported type, as per individual requirements. Make sure while developing that we handle null cases, as this is a common cause of errors. UDFs are a black box for the Spark engine, whereas functions that take a Column argument and return a Column are not a black box for Spark. It is always recommended to use Spark's Native API/Expression over UDF's with contrast to performance parameters.

Black box Column (database) Snippet (programming) Requirement Strings Scala (programming language) SPARK (programming language) Engine Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Thermometer Continuation in Scala
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech
  • DataWeave: Play With Dates (Part 1)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!