2

I have the below command in databricks notebook which is in python.

batdf = spark.sql(f"""select cast((from_unixtime((timestamp/1000), 'yyyy-MM-dd HH:mm:ss')) as date) as event_date,(from_unixtime((timestamp/1000), 'yyyy-MM-dd HH:mm:ss')) as event_datetime, * from testable  """)
    
srcRecCount = batdf.count()

I have one more cell in the same notebook which is in scala as below.

%scala
import java.time._
var srcRecCount: Long = 99999
val endPart = LocalDateTime.now()
val endPartDelta =  endPart.toString.substring(0,19)
dbutils.notebook.exit(s"""{'date':'$endPartDelta', 'srcRecCount':'$srcRecCount'}""")

I want to access the variable srcRecCount from python cell into scala cell in databricks notebook. Could you please let me know if this is possible.

2

2 Answers 2

3

For example, you can pass data via Spark configuration using spark.conf.set & spark.conf.get, like this:

# Python part
srcRecCount = batdf.count()
spark.conf.set("mydata.srcRecCount", str(srcRecCount))

and

// Scala part
val srcRecCount = spark.conf.get("mydata.srcRecCount")
dbutils.notebook.exit(
  s"""{'date':'$endPartDelta', 'srcRecCount':'$srcRecCount'}""")

P.S. But really, do you really need that Scala piece? Why not to do everything in Python?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply.Could you please let me know how we do dbutils.notebook.exit in python.
The same way as in Scala - most of dbutils functions are available in Python under the same names
0

I don't think this is possible . Way Databricks has been configured,When you invoke a language magic command in cell , the command is dispatched to the REPL in the execution context for the notebook.Variables defined in that cell are not available in the REPL of another language/ another cell. REPLs can share state only through external resources such as files in DBFS or objects in object storage. In your case , You are trying to use magic command in both cells . This is expected behavioral . Hope this helps you to understand. Ref : https://docs.databricks.com/notebooks/notebooks-use.html#mix-languages. But still this is possible as workaround , you can write value in temp DBFS location and read it from there.

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.