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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Building a Deterministic Event Correlation Engine in Go for High-Volume Alert Systems
  • Renaming Columns in PySpark: withColumnRenamed vs toDF

Trending

  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • AI Agents and Frontier Models: Insights From The Neuron
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see 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
·
Jun. 03, 20 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Building a Deterministic Event Correlation Engine in Go for High-Volume Alert Systems
  • Renaming Columns in PySpark: withColumnRenamed vs toDF

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook