08-02-2017 12:26 AM
When running a notebook using dbutils.notebook.run from a master-notebook, an url to that running notebook is printed, i.e.:
Notebook job #223150
Notebook job #223151Are there any ways to capture that Job Run ID (#223150 or #223151)? We have 50 or so notebooks that runs in parallell, and if one of them fails it would be nice to see the actual run of the notebook without clicking every url to find the correct one.
Thanks 🙂
03-19-2020 03:36 AM
Hi! It's been a long time since I have looked at this problem, but I revisited it this week and found a solution. 🙂
This returns a json containing information about the notebook:
dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson()
If the notebook has been triggered by dbutils.notebook.run, we can find the tag "jobId" here. So we can return the jobid using dbutils.notebook.exit(job_id):
import json
notebook_info = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
try:
#The tag jobId does not exists when the notebook is not triggered by dbutils.notebook.run(...)
jobId = notebook_info["tags"]["jobId"]
except:
jobId = -1
dbutils.notebook.exit(jobId)
When executing the notebook we get:
jobid = dbutils.notebook.run(...)
print(jobid)
This outputs:
Notebook job #1522478
1522478
Well, this only works when a notebook successfully completes. What we have done is that we are inserting the jobid and notebook path together with the current timestamp to a db-table and query the table when an exception occurs to get the jobid.
12-11-2017 05:44 AM
@hanspetter you can get the info via the command context:
dbutils.notebook.getContext.currentRunId
Besides of that, the following methods (vals) are available in the context:
04-09-2018 04:52 AM
Is there any development here, as currentRunId doesn't seem to work(i'm working in python though)
04-09-2018 07:14 AM
@naman1994 check which Databricks Runtime version you're using. I know that this works with 3.5, but didn't test with 4.0 yet.
04-09-2018 12:05 PM
mine's 3.5 LTS, and it says, "NotebookHandler instance has no attribute 'getContext' "
05-27-2019 08:21 AM
In Python you should write smth like:
dbutils.notebook.entry_point.getDbutils().notebook().getContext().currentRunId().toString()04-09-2018 12:05 PM
It 'll be helpful if you could tell where you foundcurrentrunid, jobGroup, rootrunid etc, cz i cant seem to find these...
Anyways Thanks04-10-2018 05:48 AM
This code works in Scala only, there is no implementation for it in the Python runtime. And the information I got from a Databricks employee via email, those are internal APIs and they may change without notice.
04-10-2018 05:50 AM
Okay, thank you 🙂
04-10-2018 06:32 AM
In scala, have you ever returned this job run id to another notebook, since im calling many notebooks and i want the job run id returned from the respective notebooks to master. I think I could do it by returning an array, But im not sure if that is possible, cz If scala is like C++, then I wont be able to at all..
Anyways, i would appreciate your thoughts over this.Thanks
01-08-2019 07:49 AM
FYI - for a Python notebook, you can get the run id in Scala and pass it in via the temp table:
Cell N:%scala val runId = dbutils.notebook.getContext .currentRunId .getOrElse(System.currentTimeMillis() / 1000L) .toStringSeq(runId).toDF("run_id").createOrReplaceTempView("run_id")Cell N + 1:
runId = spark.table("run_id").head()["run_id"]
07-03-2019 09:10 AM
Definitely needs to be a Feature request. The Run ID is good, we know it's a notebook and therefore it should be the Notebook name not just the word "Notebook"
Also this dbutils.notebook.getContext.currentRunId
Is yielding a different number, when print out on success!
dbutils.notebook.exit(s"${notebook} #${dbutils.notebook.getContext.currentRunId} : success")
03-19-2020 03:36 AM
Hi! It's been a long time since I have looked at this problem, but I revisited it this week and found a solution. 🙂
This returns a json containing information about the notebook:
dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson()
If the notebook has been triggered by dbutils.notebook.run, we can find the tag "jobId" here. So we can return the jobid using dbutils.notebook.exit(job_id):
import json
notebook_info = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
try:
#The tag jobId does not exists when the notebook is not triggered by dbutils.notebook.run(...)
jobId = notebook_info["tags"]["jobId"]
except:
jobId = -1
dbutils.notebook.exit(jobId)
When executing the notebook we get:
jobid = dbutils.notebook.run(...)
print(jobid)
This outputs:
Notebook job #1522478
1522478
Well, this only works when a notebook successfully completes. What we have done is that we are inserting the jobid and notebook path together with the current timestamp to a db-table and query the table when an exception occurs to get the jobid.
05-11-2021 03:29 AM
Well this has helped me a lot but i was unable to retrive browserhostname when i was running the notebook. while is was present documentation.. can you please through some light on this
08-10-2021 08:53 AM
this is a more simple version:
dbutils.notebook.entry_point.getDbutils().notebook().getContext().jobId().get()Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group