/
databricks_submitjob.py
691 lines (612 loc) · 31.4 KB
/
databricks_submitjob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
import six
import time
from typing import List, Dict
from prefect import Task
from prefect.utilities.tasks import defaults_from_attrs
from prefect.exceptions import PrefectException
from prefect.tasks.databricks.databricks_hook import DatabricksHook
def _deep_string_coerce(content, json_path="json"):
"""
Coerces content or all values of content if it is a dict to a string. The
function will throw if content contains non-string or non-numeric types.
The reason why we have this function is because the `self.json` field must be a
dict with only string values. This is because `render_template` will fail
for numerical values.
"""
c = _deep_string_coerce
if isinstance(content, six.string_types):
return content
elif isinstance(content, six.integer_types + (float,)):
# Databricks can tolerate either numeric or string types in the API backend.
return str(content)
elif isinstance(content, (list, tuple)):
return [c(e, "{0}[{1}]".format(json_path, i)) for i, e in enumerate(content)]
elif isinstance(content, dict):
return {
k: c(v, "{0}[{1}]".format(json_path, k)) for k, v in list(content.items())
}
else:
param_type = type(content)
msg = "Type {0} used for parameter {1} is not a number or a string".format(
param_type, json_path
)
raise ValueError(msg)
def _handle_databricks_task_execution(task, hook, log):
"""
Handles the Databricks + Prefect lifecycle logic for a Databricks task
Args:
- task (prefect.Task) : Prefect task being handled
- hook (prefect.tasks.databricks.databricks_hook.DatabricksHook): Databricks Hook
- log (logger): Prefect logging instance
"""
log.info("Run submitted with run_id: %s", task.run_id)
run_page_url = hook.get_run_page_url(task.run_id)
log.info("Run submitted with config : %s", task.json)
log.info("View run status, Spark UI, and logs at %s", run_page_url)
while True:
run_state = hook.get_run_state(task.run_id)
if run_state.is_terminal:
if run_state.is_successful:
log.info("%s completed successfully.", task.name)
log.info("View run status, Spark UI, and logs at %s", run_page_url)
return
else:
error_message = "{t} failed with terminal state: {s}".format(
t=task.name, s=run_state
)
raise PrefectException(error_message)
else:
log.info("%s in run state: %s", task.name, run_state)
log.info("View run status, Spark UI, and logs at %s", run_page_url)
log.info("Sleeping for %s seconds.", task.polling_period_seconds)
time.sleep(task.polling_period_seconds)
class DatabricksSubmitRun(Task):
"""
Submits a Spark job run to Databricks using the
`api/2.0/jobs/runs/submit
<https://docs.databricks.com/api/latest/jobs.html#runs-submit>`_
API endpoint.
There are two ways to instantiate this task.
In the first way, you can take the JSON payload that you typically use
to call the `api/2.0/jobs/runs/submit` endpoint and pass it directly
to our `DatabricksSubmitRun` task through the `json` parameter.
For example:
```
json = {
'new_cluster': {
'spark_version': '2.1.0-db3-scala2.11',
'num_workers': 2
},
'notebook_task': {
'notebook_path': '/Users/prefect@example.com/PrepareData',
},
}
conn = PrefectSecret('DATABRICKS_CONNECTION_STRING')
notebook_run = DatabricksSubmitRun(json=json)
notebook_run(databricks_conn_secret=conn)
```
Another way to accomplish the same thing is to use the named parameters
of the `DatabricksSubmitRun` directly. Note that there is exactly
one named parameter for each top level parameter in the `runs/submit`
endpoint. In this method, your code would look like this:
```
new_cluster = {
'spark_version': '2.1.0-db3-scala2.11',
'num_workers': 2
}
notebook_task = {
'notebook_path': '/Users/prefect@example.com/PrepareData',
}
conn = PrefectSecret('DATABRICKS_CONNECTION_STRING')
notebook_run = DatabricksSubmitRun(
new_cluster=new_cluster,
notebook_task=notebook_task)
notebook_run(databricks_conn_secret=conn)
```
In the case where both the json parameter **AND** the named parameters
are provided, they will be merged together. If there are conflicts during the merge,
the named parameters will take precedence and override the top level `json` keys.
This task requires a Databricks connection to be specified as a Prefect secret and can
be passed to the task like so:
```
from prefect.tasks.secrets import PrefectSecret
from prefect.tasks.databricks import DatabricksSubmitRun
with Flow('my flow') as flow:
conn = PrefectSecret('DATABRICKS_CONNECTION_STRING')
notebook_run = DatabricksSubmitRun(json=...)
notebook_run(databricks_conn_secret=conn)
```
Currently the named parameters that `DatabricksSubmitRun` task supports are
- `spark_jar_task`
- `notebook_task`
- `new_cluster`
- `existing_cluster_id`
- `libraries`
- `run_name`
- `timeout_seconds`
Args:
- databricks_conn_secret (dict, optional): Dictionary representation of the Databricks Connection
String. Structure must be a string of valid JSON. To use token based authentication, provide
the key `token` in the string for the connection and create the key `host`.
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "login": "ghijklmn", "password": "opqrst"}'`
OR
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "token": "ghijklmn"}'`
See documentation of the `DatabricksSubmitRun` Task to see how to pass in the connection
string using `PrefectSecret`.
- json (dict, optional): A JSON object containing API parameters which will be passed
directly to the `api/2.0/jobs/runs/submit` endpoint. The other named parameters
(i.e. `spark_jar_task`, `notebook_task`..) to this task will
be merged with this json dictionary if they are provided.
If there are conflicts during the merge, the named parameters will
take precedence and override the top level json keys. (templated)
For more information about templating see :ref:`jinja-templating`.
https://docs.databricks.com/api/latest/jobs.html#runs-submit
- spark_jar_task (dict, optional): The main class and parameters for the JAR task. Note that
the actual JAR is specified in the `libraries`.
*EITHER* `spark_jar_task` *OR* `notebook_task` should be specified.
This field will be templated.
https://docs.databricks.com/api/latest/jobs.html#jobssparkjartask
- notebook_task (dict, optional): The notebook path and parameters for the notebook task.
*EITHER* `spark_jar_task` *OR* `notebook_task` should be specified.
This field will be templated.
https://docs.databricks.com/api/latest/jobs.html#jobsnotebooktask
- new_cluster (dict, optional): Specs for a new cluster on which this task will be run.
*EITHER* `new_cluster` *OR* `existing_cluster_id` should be specified.
This field will be templated.
https://docs.databricks.com/api/latest/jobs.html#jobsclusterspecnewcluster
- existing_cluster_id (str, optional): ID for existing cluster on which to run this task.
*EITHER* `new_cluster` *OR* `existing_cluster_id` should be specified.
This field will be templated.
- libraries (list of dicts, optional): Libraries which this run will use.
This field will be templated.
https://docs.databricks.com/api/latest/libraries.html#managedlibrarieslibrary
- run_name (str, optional): The run name used for this task.
By default this will be set to the Prefect `task_id`. This `task_id` is a
required parameter of the superclass `Task`.
This field will be templated.
- timeout_seconds (int, optional): The timeout for this run. By default a value of 0 is used
which means to have no timeout.
This field will be templated.
- polling_period_seconds (int, optional): Controls the rate which we poll for the result of
this run. By default the task will poll every 30 seconds.
- databricks_retry_limit (int, optional): Amount of times retry if the Databricks backend is
unreachable. Its value must be greater than or equal to 1.
- databricks_retry_delay (float, optional): Number of seconds to wait between retries (it
might be a floating point number).
- **kwargs (dict, optional): additional keyword arguments to pass to the
Task constructor
"""
def __init__(
self,
databricks_conn_secret: dict = None,
json: dict = None,
spark_jar_task: dict = None,
notebook_task: dict = None,
new_cluster: dict = None,
existing_cluster_id: str = None,
libraries: List[Dict] = None,
run_name: str = None,
timeout_seconds: int = None,
polling_period_seconds: int = 30,
databricks_retry_limit: int = 3,
databricks_retry_delay: float = 1,
**kwargs
) -> None:
self.databricks_conn_secret = databricks_conn_secret
self.json = json or {}
self.spark_jar_task = spark_jar_task
self.notebook_task = notebook_task
self.new_cluster = new_cluster
self.existing_cluster_id = existing_cluster_id
self.libraries = libraries
self.run_name = run_name
self.timeout_seconds = timeout_seconds
self.polling_period_seconds = polling_period_seconds
self.databricks_retry_limit = databricks_retry_limit
self.databricks_retry_delay = databricks_retry_delay
self.run_id = None
super().__init__(**kwargs)
def get_hook(self):
return DatabricksHook(
self.databricks_conn_secret,
retry_limit=self.databricks_retry_limit,
retry_delay=self.databricks_retry_delay,
)
@defaults_from_attrs(
"databricks_conn_secret",
"json",
"spark_jar_task",
"notebook_task",
"new_cluster",
"existing_cluster_id",
"libraries",
"run_name",
"timeout_seconds",
"polling_period_seconds",
"databricks_retry_limit",
"databricks_retry_delay",
)
def run(
self,
databricks_conn_secret: dict = None,
json: dict = None,
spark_jar_task: dict = None,
notebook_task: dict = None,
new_cluster: dict = None,
existing_cluster_id: str = None,
libraries: List[Dict] = None,
run_name: str = None,
timeout_seconds: int = None,
polling_period_seconds: int = 30,
databricks_retry_limit: int = 3,
databricks_retry_delay: float = 1,
) -> str:
"""
Task run method.
Args:
- databricks_conn_secret (dict, optional): Dictionary representation of the Databricks Connection
String. Structure must be a string of valid JSON. To use token based authentication, provide
the key `token` in the string for the connection and create the key `host`.
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "login": "ghijklmn", "password": "opqrst"}'`
OR
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "token": "ghijklmn"}'`
See documentation of the `DatabricksSubmitRun` Task to see how to pass in the connection
string using `PrefectSecret`.
- json (dict, optional): A JSON object containing API parameters which will be passed
directly to the `api/2.0/jobs/runs/submit` endpoint. The other named parameters
(i.e. `spark_jar_task`, `notebook_task`..) to this task will
be merged with this json dictionary if they are provided.
If there are conflicts during the merge, the named parameters will
take precedence and override the top level json keys. (templated)
For more information about templating see :ref:`jinja-templating`.
https://docs.databricks.com/api/latest/jobs.html#runs-submit
- spark_jar_task (dict, optional): The main class and parameters for the JAR task. Note that
the actual JAR is specified in the `libraries`.
*EITHER* `spark_jar_task` *OR* `notebook_task` should be specified.
This field will be templated.
https://docs.databricks.com/api/latest/jobs.html#jobssparkjartask
- notebook_task (dict, optional): The notebook path and parameters for the notebook task.
*EITHER* `spark_jar_task` *OR* `notebook_task` should be specified.
This field will be templated.
https://docs.databricks.com/api/latest/jobs.html#jobsnotebooktask
- new_cluster (dict, optional): Specs for a new cluster on which this task will be run.
*EITHER* `new_cluster` *OR* `existing_cluster_id` should be specified.
This field will be templated.
https://docs.databricks.com/api/latest/jobs.html#jobsclusterspecnewcluster
- existing_cluster_id (str, optional): ID for existing cluster on which to run this task.
*EITHER* `new_cluster` *OR* `existing_cluster_id` should be specified.
This field will be templated.
- libraries (list of dicts, optional): Libraries which this run will use.
This field will be templated.
https://docs.databricks.com/api/latest/libraries.html#managedlibrarieslibrary
- run_name (str, optional): The run name used for this task.
By default this will be set to the Prefect `task_id`. This `task_id` is a
required parameter of the superclass `Task`.
This field will be templated.
- timeout_seconds (int, optional): The timeout for this run. By default a value of 0 is used
which means to have no timeout.
This field will be templated.
- polling_period_seconds (int, optional): Controls the rate which we poll for the result of
this run. By default the task will poll every 30 seconds.
- databricks_retry_limit (int, optional): Amount of times retry if the Databricks backend is
unreachable. Its value must be greater than or equal to 1.
- databricks_retry_delay (float, optional): Number of seconds to wait between retries (it
might be a floating point number).
Returns:
- run_id (str): Run id of the submitted run
"""
assert (
databricks_conn_secret
), "A databricks connection string must be supplied as a dictionary or through Prefect Secrets"
assert isinstance(
databricks_conn_secret, dict
), "`databricks_conn_secret` must be supplied as a valid dictionary."
self.databricks_conn_secret = databricks_conn_secret
if json:
self.json = json
# Initialize Databricks Connections
hook = self.get_hook()
if spark_jar_task is not None:
self.json["spark_jar_task"] = spark_jar_task
if notebook_task is not None:
self.json["notebook_task"] = notebook_task
if new_cluster is not None:
self.json["new_cluster"] = new_cluster
if existing_cluster_id is not None:
self.json["existing_cluster_id"] = existing_cluster_id
if libraries is not None:
self.json["libraries"] = libraries
if run_name is not None:
self.json["run_name"] = run_name
if timeout_seconds is not None:
self.json["timeout_seconds"] = timeout_seconds
if "run_name" not in self.json:
self.json["run_name"] = run_name or "Run Submitted by Prefect"
# Validate the dictionary to a valid JSON object
self.json = _deep_string_coerce(self.json)
# Submit the job
self.run_id = hook.submit_run(self.json)
_handle_databricks_task_execution(self, hook, self.logger)
return self.run_id
class DatabricksRunNow(Task):
"""
Runs an existing Spark job run to Databricks using the
`api/2.0/jobs/run-now
<https://docs.databricks.com/api/latest/jobs.html#run-now>`_
API endpoint.
There are two ways to instantiate this task.
In the first way, you can take the JSON payload that you typically use
to call the `api/2.0/jobs/run-now` endpoint and pass it directly
to our `DatabricksRunNow` task through the `json` parameter.
For example:
```
json = {
"job_id": 42,
"notebook_params": {
"dry-run": "true",
"oldest-time-to-consider": "1457570074236"
}
}
conn = PrefectSecret('DATABRICKS_CONNECTION_STRING')
notebook_run = DatabricksRunNow(json=json)
notebook_run(databricks_conn_secret=conn)
```
Another way to accomplish the same thing is to use the named parameters
of the `DatabricksRunNow` task directly. Note that there is exactly
one named parameter for each top level parameter in the `run-now`
endpoint. In this method, your code would look like this:
```
job_id=42
notebook_params = {
"dry-run": "true",
"oldest-time-to-consider": "1457570074236"
}
python_params = ["douglas adams", "42"]
spark_submit_params = ["--class", "org.apache.spark.examples.SparkPi"]
jar_params = ["john doe","35"]
conn = PrefectSecret('DATABRICKS_CONNECTION_STRING')
notebook_run = DatabricksRunNow(
notebook_params=notebook_params,
python_params=python_params,
spark_submit_params=spark_submit_params,
jar_params=jar_params
)
notebook_run(databricks_conn_secret=conn)
```
In the case where both the json parameter **AND** the named parameters
are provided, they will be merged together. If there are conflicts during the merge,
the named parameters will take precedence and override the top level `json` keys.
This task requires a Databricks connection to be specified as a Prefect secret and can
be passed to the task like so:
```
from prefect.tasks.secrets import PrefectSecret
from prefect.tasks.databricks import DatabricksRunNow
with Flow('my flow') as flow:
conn = PrefectSecret('DATABRICKS_CONNECTION_STRING')
notebook_run = DatabricksRunNow(json=...)
notebook_run(databricks_conn_secret=conn)
```
Currently the named parameters that `DatabricksRunNow` task supports are
- `job_id`
- `json`
- `notebook_params`
- `python_params`
- `spark_submit_params`
- `jar_params`
Args:
- databricks_conn_secret (dict, optional): Dictionary representation of the Databricks Connection
String. Structure must be a string of valid JSON. To use token based authentication, provide
the key `token` in the string for the connection and create the key `host`.
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "login": "ghijklmn", "password": "opqrst"}'`
OR
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "token": "ghijklmn"}'`
See documentation of the `DatabricksSubmitRun` Task to see how to pass in the connection
string using `PrefectSecret`.
- job_id (str, optional): The job_id of the existing Databricks job.
https://docs.databricks.com/api/latest/jobs.html#run-now
- json (dict, optional): A JSON object containing API parameters which will be passed
directly to the `api/2.0/jobs/run-now` endpoint. The other named parameters
(i.e. `notebook_params`, `spark_submit_params`..) to this operator will
be merged with this json dictionary if they are provided.
If there are conflicts during the merge, the named parameters will
take precedence and override the top level json keys. (templated)
https://docs.databricks.com/api/latest/jobs.html#run-now
- notebook_params (dict, optional): A dict from keys to values for jobs with notebook task,
e.g. "notebook_params": {"name": "john doe", "age": "35"}.
The map is passed to the notebook and will be accessible through the
dbutils.widgets.get function. See Widgets for more information.
If not specified upon run-now, the triggered run will use the
job’s base parameters. notebook_params cannot be
specified in conjunction with jar_params. The json representation
of this field (i.e. {"notebook_params":{"name":"john doe","age":"35"}})
cannot exceed 10,000 bytes.
https://docs.databricks.com/user-guide/notebooks/widgets.html
- python_params (list[str], optional): A list of parameters for jobs with python tasks,
e.g. "python_params": ["john doe", "35"].
The parameters will be passed to python file as command line parameters.
If specified upon run-now, it would overwrite the parameters specified in
job setting.
The json representation of this field (i.e. {"python_params":["john doe","35"]})
cannot exceed 10,000 bytes.
https://docs.databricks.com/api/latest/jobs.html#run-now
- spark_submit_params (list[str], optional): A list of parameters for jobs with spark submit
task, e.g. "spark_submit_params": ["--class", "org.apache.spark.examples.SparkPi"].
The parameters will be passed to spark-submit script as command line parameters.
If specified upon run-now, it would overwrite the parameters specified
in job setting.
The json representation of this field cannot exceed 10,000 bytes.
https://docs.databricks.com/api/latest/jobs.html#run-now
- jar_params (list[str], optional): A list of parameters for jobs with JAR tasks,
e.g. "jar_params": ["john doe", "35"]. The parameters will be used to invoke the main
function of the main class specified in the Spark JAR task. If not specified upon
run-now, it will default to an empty list. jar_params cannot be specified in conjunction
with notebook_params. The JSON representation of this field (i.e.
{"jar_params":["john doe","35"]}) cannot exceed 10,000 bytes.
https://docs.databricks.com/api/latest/jobs.html#run-now
- timeout_seconds (int, optional): The timeout for this run. By default a value of 0 is used
which means to have no timeout.
This field will be templated.
- polling_period_seconds (int, optional): Controls the rate which we poll for the result of
this run. By default the task will poll every 30 seconds.
- databricks_retry_limit (int, optional): Amount of times retry if the Databricks backend is
unreachable. Its value must be greater than or equal to 1.
- databricks_retry_delay (float, optional): Number of seconds to wait between retries (it
might be a floating point number).
- **kwargs (dict, optional): additional keyword arguments to pass to the
Task constructor
"""
def __init__(
self,
databricks_conn_secret: dict = None,
job_id: str = None,
json: dict = None,
notebook_params: dict = None,
python_params: List[str] = None,
spark_submit_params: List[str] = None,
jar_params: List[str] = None,
polling_period_seconds: int = 30,
databricks_retry_limit: int = 3,
databricks_retry_delay: float = 1,
**kwargs
) -> None:
self.databricks_conn_secret = databricks_conn_secret
self.json = json or {}
self.job_id = job_id
self.notebook_params = notebook_params
self.python_params = python_params
self.spark_submit_params = spark_submit_params
self.jar_params = jar_params
self.polling_period_seconds = polling_period_seconds
self.databricks_retry_limit = databricks_retry_limit
self.databricks_retry_delay = databricks_retry_delay
self.run_id = None
super().__init__(**kwargs)
def get_hook(self):
return DatabricksHook(
self.databricks_conn_secret,
retry_limit=self.databricks_retry_limit,
retry_delay=self.databricks_retry_delay,
)
@defaults_from_attrs(
"databricks_conn_secret",
"job_id",
"json",
"notebook_params",
"python_params",
"spark_submit_params",
"jar_params",
"polling_period_seconds",
"databricks_retry_limit",
"databricks_retry_delay",
)
def run(
self,
databricks_conn_secret: dict = None,
job_id: str = None,
json: dict = None,
notebook_params: dict = None,
python_params: List[str] = None,
spark_submit_params: List[str] = None,
jar_params: List[str] = None,
polling_period_seconds: int = 30,
databricks_retry_limit: int = 3,
databricks_retry_delay: float = 1,
) -> str:
"""
Task run method.
Args:
- databricks_conn_secret (dict, optional): Dictionary representation of the Databricks
Connection String. Structure must be a string of valid JSON. To use token based
authentication, provide the key `token` in the string for the connection and create the
key `host`.
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "login": "ghijklmn", "password": "opqrst"}'`
OR
`PREFECT__CONTEXT__SECRETS__DATABRICKS_CONNECTION_STRING=
'{"host": "abcdef.xyz", "token": "ghijklmn"}'`
See documentation of the `DatabricksSubmitRun` Task to see how to pass in the connection
string using `PrefectSecret`.
- job_id (str, optional): The job_id of the existing Databricks job.
https://docs.databricks.com/api/latest/jobs.html#run-now
- json (dict, optional): A JSON object containing API parameters which will be passed
directly to the `api/2.0/jobs/run-now` endpoint. The other named parameters
(i.e. `notebook_params`, `spark_submit_params`..) to this operator will
be merged with this json dictionary if they are provided.
If there are conflicts during the merge, the named parameters will
take precedence and override the top level json keys. (templated)
https://docs.databricks.com/api/latest/jobs.html#run-now
- notebook_params (dict, optional): A dict from keys to values for jobs with notebook task,
e.g. "notebook_params": {"name": "john doe", "age": "35"}.
The map is passed to the notebook and will be accessible through the
dbutils.widgets.get function. See Widgets for more information.
If not specified upon run-now, the triggered run will use the
job’s base parameters. notebook_params cannot be
specified in conjunction with jar_params. The json representation
of this field (i.e. {"notebook_params":{"name":"john doe","age":"35"}})
cannot exceed 10,000 bytes.
https://docs.databricks.com/user-guide/notebooks/widgets.html
- python_params (list[str], optional): A list of parameters for jobs with python tasks,
e.g. "python_params": ["john doe", "35"].
The parameters will be passed to python file as command line parameters.
If specified upon run-now, it would overwrite the parameters specified in
job setting.
The json representation of this field (i.e. {"python_params":["john doe","35"]})
cannot exceed 10,000 bytes.
https://docs.databricks.com/api/latest/jobs.html#run-now
- spark_submit_params (list[str], optional): A list of parameters for jobs with spark submit
task, e.g. "spark_submit_params": ["--class", "org.apache.spark.examples.SparkPi"].
The parameters will be passed to spark-submit script as command line parameters.
If specified upon run-now, it would overwrite the parameters specified
in job setting.
The json representation of this field cannot exceed 10,000 bytes.
https://docs.databricks.com/api/latest/jobs.html#run-now
- jar_params (list[str], optional): A list of parameters for jobs with JAR tasks,
e.g. "jar_params": ["john doe", "35"]. The parameters will be used to invoke the main
function of the main class specified in the Spark JAR task. If not specified upon
run-now, it will default to an empty list. jar_params cannot be specified in conjunction
with notebook_params. The JSON representation of this field (i.e.
{"jar_params":["john doe","35"]}) cannot exceed 10,000 bytes.
https://docs.databricks.com/api/latest/jobs.html#run-now
- polling_period_seconds (int, optional): Controls the rate which we poll for the result of
this run. By default the task will poll every 30 seconds.
- databricks_retry_limit (int, optional): Amount of times retry if the Databricks backend is
unreachable. Its value must be greater than or equal to 1.
- databricks_retry_delay (float, optional): Number of seconds to wait between retries (it
might be a floating point number).
Returns:
- run_id (str): Run id of the submitted run
"""
assert (
databricks_conn_secret
), "A databricks connection string must be supplied as a dictionary or through Prefect Secrets"
assert isinstance(
databricks_conn_secret, dict
), "`databricks_conn_secret` must be supplied as a valid dictionary."
self.databricks_conn_secret = databricks_conn_secret
# Initialize Databricks Connections
hook = self.get_hook()
run_now_json = json or {}
if job_id is not None:
run_now_json["job_id"] = job_id
if notebook_params is not None:
merged = run_now_json.setdefault("notebook_params", {})
merged.update(notebook_params)
run_now_json["notebook_params"] = merged
if python_params is not None:
run_now_json["python_params"] = python_params
if spark_submit_params is not None:
run_now_json["spark_submit_params"] = spark_submit_params
if jar_params is not None:
run_now_json["jar_params"] = jar_params
# Validate the dictionary to a valid JSON object
self.json = _deep_string_coerce(run_now_json)
# Submit the job
self.run_id = hook.run_now(self.json)
_handle_databricks_task_execution(self, hook, self.logger)
return self.run_id