1

I need to write some regex for condition check in spark while doing some join,

My regex should match below string

n3_testindia1 = test-india-1
n2_stagamerica2 = stag-america-2
n1_prodeurope2 = prod-europe-2

df1.select("location1").distinct.show()

+----------------+
|    location1   |
+----------------+
|n3_testindia1   |
|n2_stagamerica2 |
|n1_prodeurope2  |

df2.select("loc1").distinct.show()

+--------------+
|      loc1    |
+--------------+
|test-india-1  |   
|stag-america-2|
|prod-europe-2 |
+--------------+

I want to join based on location columns like below

val joindf = df1.join(df2, df1("location1") == regex(df2("loc1")))

2 Answers 2

1

Based on the information above you can do that in Spark 2.4.0 using

val joindf = df1.join(df2, 
  regexp_extract(df1("location1"), """[^_]+_(.*)""", 1) 
    === translate(df2("loc1"), "-", ""))

Or in prior versions something like

val joindf = df1.join(df2, 
  df1("location1").substr(lit(4), length(df1("location1")))
    === translate(df2("loc1"), "-", ""))
Sign up to request clarification or add additional context in comments.

Comments

0

You can split by "_" in location1 and take the 2 element, then match with the entire string of "-" removed string in loc1. Check this out:

scala> val df1 = Seq(("n3_testindia1"),("n2_stagamerica2"),("n1_prodeurope2")).toDF("location1")
df1: org.apache.spark.sql.DataFrame = [location1: string]

scala> val df2 = Seq(("test-india-1"),("stag-america-2"),("prod-europe-2")).toDF("loc1")
df2: org.apache.spark.sql.DataFrame = [loc1: string]

scala> df1.join(df2,split('location1,"_")(1) === regexp_replace('loc1,"-",""),"inner").show
+---------------+--------------+
|      location1|          loc1|
+---------------+--------------+
|  n3_testindia1|  test-india-1|
|n2_stagamerica2|stag-america-2|
| n1_prodeurope2| prod-europe-2|
+---------------+--------------+


scala>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.