1

I need to write SQL Query into DataFrame

SQL Query

A_join_Deals = sqlContext.sql("SELECT * FROM A_transactions LEFT 
JOIN Deals ON (Deals.device = A_transactions.device_id) WHERE 
A_transactions.device_id IS NOT NULL AND A_transactions.device_id != '' AND 
A_transactions.advertiser_app_object_id = '%s'"%(adv_id))

Code written up to now

val A_join_Deals = Deals.join(A_transactions,Deals("device") === A_transactions("device_id"),"left")

Now I am stuck how to write the where Clause.

Any Suggestion or Help is Highly appreciated.

2 Answers 2

4

you can try as below

val A_join_Deals = Deals.join(A_transactions,Deals("device") === A_transactions("device_id"),"left")
      .where(A_transactions("device_id").isNotNull && A_transactions("device_id") =!= "" && A_transactions("advertiser_app_object_id") === s"${adv_id}")
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help but how to pass a variable [%(adv_id))] ? in the condition in Data-frame ?
You can use like function for spark which is silmilar to % in sql.
@ShankarKoirala Thanks for your suggestion but we dont want to use %sql sparksql since we need to register the dataset as temp table which is taking a lot of Memory since we have data in TeraByte
@Bhavesh If adv_id is a scala variable in scope, you can do A_transactions("advertiser_app_object_id") === Deals(s"$adv_id"))
@DanieldePaula Thanks for your suggestion i will try to implement it and see once again Thank you for your help DanieldePaula, ShankarKoirala and Ramesh
|
2

Here is what you can do

where(A_transactions("device_id").isNotNull &&
   A_transactions("device_id") =!= "" && 
   A_transactions("advertiser_app_object_id") === s"${adv_id}%")

This works if adv_id is a variable and not Column

Hope this helps!

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.