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

  • Decoupling a Data Pipeline From the Tables It Was Never Supposed to Depend On
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Why Vulnerability Management Fails Without Reliable Asset Data
  • Engineering Audit-Ready Data Pipelines for Regulatory Reporting

Trending

  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • AI-Driven Integration in Large-Scale Agile Environments

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. Data Engineering
  3. Data
  4. How to Use Dynamic Data Transpose in Spark

How to Use Dynamic Data Transpose in Spark

We take a look at how to use in-memory operators and the Scala language to work with dynamically transposed data in Apache Spark.

By 
Subhasish Guha
·
Nov. 26, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.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

Dynamic Transpose is a critical transformation in Spark, as it requires a lot of iterations. This article will give you a clear idea of how to handle this complex scenario with in-memory operators.

First, let us see the source data that we have: 

idoc_number,orderid,idoc_qualifier_org,idoc_org
7738,2364,6,0
7738,2364,7,0
7738,2364,8,mystr1
7738,2364,12,mystr2
7739,2365,12,mystr3
7739,2365,7,mystr4

We also have a lookup table for the idoc_qualifier_org column in the Source data records. As the lookup table's size will be less, we can expect it to be in the cache and in the driver memory, as well.

qualifier,desc
6,Division
7,Distribution Channel
8,Sales Org
12,Order type

The expected output of the Dynamic Transpose operation is:

idoc_number,order_id,Division,Distribution Channel,Sales org,Order Type
7738,2364,0,0,mystr1,mystr2
7739,2365,null,mystr3,null,mystr4

The below code will actually transpose the data based on the present column in the data. This code is a different way to work with Transpose Data in Spark. This code rigorously uses the complex data types of Spark and also takes care of the performance of the iterations. 

object DynamicTranspose {
 def dataValidator(map_val: Seq[Map[String, String]], rule: String): String = {
  try {
   val rule_array = rule.split("#!").toList
   val src_map = map_val.toList.flatten.toMap
   var output_str = ""
   rule_array.foreach(f =>
    output_str = output_str + "!" + src_map.getOrElse(f, "#")
   )

   return output_str.drop(1)
  } catch {
   case t:
    Throwable => t.printStackTrace().toString()
    return "0".toString()
  }

 }

 def main(args: Array[String]): Unit = {

  val spark = SparkSession.builder().master("local[*]").config("spark.sql.warehouse.dir", "<src dir>").getOrCreate()
  val data_df = spark.read.option("header", "true").csv("<data path src>")
  val lkp_df = spark.read.option("header", "true").csv("lookup path source>")
  import spark.implicits._
  import org.apache.spark.sql.functions.broadcast

  val lkp_df_brdcast = broadcast(lkp_df)
  val result_df = data_df.join(broadcast(lkp_df_brdcast), $ "idoc_qualifier_org" === $ "qualifier", "inner")

  val df1 = result_df.groupBy(col("idoc_number"), col("orderid")).agg(collect_list(map($ "desc", $ "idoc_org")) as "map")
  import org.apache.spark.sql.functions.udf
  import org.apache.spark.sql.functions. {
   lit,
   max,
   row_number
  }
  import spark.implicits._
  import org.apache.spark.sql.Row
  val map_val = lkp_df.rdd.map(row => row.getString(1)).collect().mkString("#!")
  spark.sparkContext.broadcast(map_val)
  val recdValidator = udf(dataValidator _)
  var latest_df = df1.withColumn("explode_out", split(recdValidator(df1("map"), lit(map_val)), "!")).drop("map")
  val columns = map_val.split("#!").toList
  latest_df = columns.zipWithIndex.foldLeft(latest_df) {
   (memodDF, column) => {
    memodDF.withColumn(column._1, col("explode_out")(column._2))
   }
  }
  .drop("explode_out")
  latest_df.show()
 }

}

Hope this helps!

Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Decoupling a Data Pipeline From the Tables It Was Never Supposed to Depend On
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Why Vulnerability Management Fails Without Reliable Asset Data
  • Engineering Audit-Ready Data Pipelines for Regulatory Reporting

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