Wednesday, August 12, 2020

Call Data bricks Job Using REST API

Below power shell will help to call Data bricks Job with parameter


 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$args.length

$job_name= $args[0]

$dbname= $args[1]

$adlpath= $args[2]

$prefix= $args[3]

$loadtype= $args[4]

$rerunid= $args[5]

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add("Authorization", "Bearer <<<dapisfsadasd2132432434>>>")

$headers.Add("Content-Type", "text/plain")

$job_nm = "https://<<<Workspace Name>>>.azuredatabricks.net/api/2.0/jobs/list"

$job_nm_response = Invoke-RestMethod $job_nm  -Method 'GET' -Headers $headers

foreach ($r in $job_nm_response.jobs)

{

        if($r.settings.name -eq $job_name)

        {

        $job_id = $r.job_id

        break

        }

}

$body = "{

`n  `"job_id`": $job_id,

`n  `"notebook_params`": {

`n  `"databasename`":`"$dbname`",

`n  `"adlpath`": `"$adlpath`",

`n  `"loadtype`": `"$loadtype`",

`n  `"failed_run_id`": `"$rerunid`",

`n  `"prefix`": `"$prefix`"

`n}

`n}"

$response = Invoke-RestMethod 'https://<<<Workspace Name>>>.azuredatabricks.net/api/2.0/jobs/run-now' -Method 'POST' -Headers $headers -Body $body

#$response | ConvertTo-Json

$batch_id =  $response.run_id

$run_sta = "https://<<<Workspace Name>>>.azuredatabricks.net/api/2.0/jobs/runs/get?run_id=" + $batch_id 

$response_status = Invoke-RestMethod $run_sta  -Method 'GET' -Headers $headers

$response_status1= $response_status.state.life_cycle_state

while($response_status1 -ne "success" -Or $response_status1 -ne "timedout" -Or $response_status1 -ne "failed" -Or $response_status1 -ne "canceled")

{

$response_status = Invoke-RestMethod $run_sta -Method 'GET' -Headers $headers

$response_status1 = $response_status.state.result_state

$life_cycle_state = $response_status.state.life_cycle_state

if ($response_status1 -eq $null)

{

#$life_cycle_state + ": " + $state_message

if($life_cycle_state -eq "PENDING" -Or $life_cycle_state -eq "RUNNING") { $life_cycle_state }

Else 

echo "FAILURE" 

exit 1

}

}

Else {

$response_status1

If ($response_status1 -eq "SUCCESS") 

echo "DONE" 

exit 0  

}

Else 

echo "FAILURE"

exit 1

}        

}

}


No comments:

Call Data bricks Job Using REST API

Below power shell will help to call Data bricks Job with parameter  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]...