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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Building a Deterministic Event Correlation Engine in Go for High-Volume Alert Systems
  • Renaming Columns in PySpark: withColumnRenamed vs toDF
  • Mastering Advanced Aggregations in Spark SQL

Trending

  • Why We Chose Iceberg Over Delta After Evaluating Both at Scale
  • LLM Integration in Enterprise Applications: A Practical Guide
  • Self-Hosted Inference Doesn’t Have to Be a Nightmare: How to Use GPUStack
  • What Nobody Tells You About Multimodal Data Pipelines for AI Training
  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.8K 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
  • Building a Deterministic Event Correlation Engine in Go for High-Volume Alert Systems
  • Renaming Columns in PySpark: withColumnRenamed vs toDF
  • Mastering Advanced Aggregations in Spark SQL

Partner Resources

×

Comments

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

  • 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