Last active
August 29, 2015 14:00
-
-
Save dharkum/11381925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region edits | |
# Your HDInsight Cluster Name | |
$HDIClusterName = "ServerLogs" | |
# Your HDInisght Cluster Admin User Name | |
$MyHDInsightUserName = "sysadmin" | |
# Your HDInsight Cluster Admin Password | |
$MyHdInsightPwd = "xxxx" | |
# Your HDInsight Metastore User Name | |
$MetaStoreUserName = "sysadmin" | |
# Your HDInsight Metastore Password | |
$MetaStorePwd = "xxxxx" | |
# Paths to WASB locations for name node, Oozie coordinator path, and workflow application path | |
$namenode = "wasb://data@serverlogs1.blob.core.windows.net" | |
$oozieCoordPath="$namenode/slidingwindow/coordinator.xml" | |
$appPath = "$namenode/slidingwindow" | |
# Build secure credentials | |
$MSPwd = ConvertTo-SecureString $MetaStorePwd -AsPlainText -Force | |
$MetaStoreCreds= New-Object System.Management.Automation.PSCredential ($MetaStoreUserName, $MSPwd) | |
$HdInsightPwd = ConvertTo-SecureString $MyHDInsightPwd -AsPlainText -Force | |
$creds= New-Object System.Management.Automation.PSCredential ($MyHDInsightUserName, $HdInsightPwd) | |
$creds | |
# This is the time at which the Oozie job will start, defaults to the current time in the format of yyyy-MM-ddTHH:MMZ | |
[string]$jobStart = [DateTime]::ParseExact((get-date).ToUniversalTime().ToString("yyyy-MM-ddHHmm"), "yyyy-MM-ddHHmm",$null).ToString("yyyy-MM-ddTHH:mmZ"); | |
# This is the time at which the Oozie job will end, defaults to 2 minutes from the time of job start; of course this can be changed to end of year etc. based on business needs | |
[string]$jobEnd = [DateTime]::ParseExact(((get-date).AddMinutes(2)).ToUniversalTime().ToString("yyyy-MM-ddHHmm"), "yyyy-MM-ddHHmm",$null).ToString("yyyy-MM-ddTHH:mmZ"); | |
# This is the initial instance of the time when the job would need to start execution | |
[string]$initialInstance = $jobStart | |
# If polling for the Oozie job to finish, this variable determines the number of seconds to wait before polling next. | |
$waitTimeBetweenOozieJobStatusCheck=10 | |
# OoziePayload used for Oozie web service submission | |
$OoziePayload = @" | |
<?xml version="1.0" encoding="UTF-8"?> | |
<configuration> | |
<property> | |
<name>user.name</name> | |
<value>$MyHDInsightUserName</value> | |
</property> | |
<property> | |
<name>nameNode</name> | |
<value>$namenode</value> | |
</property> | |
<property> | |
<name>jobTracker</name> | |
<value>jobtrackerhost:9010</value> | |
</property> | |
<property> | |
<name>oozie.coord.application.path</name> | |
<value>$oozieCoordPath</value> | |
</property> | |
<property> | |
<name>appPath</name> | |
<value>$appPath</value> | |
</property> | |
<property> | |
<name>queueName</name> | |
<value>joblauncher</value> | |
</property> | |
<property> | |
<name>oozie.use.system.libpath</name> | |
<value>true</value> | |
</property> | |
<property> | |
<name>jobStart</name> | |
<value>$jobStart</value> | |
</property> | |
<property> | |
<name>jobEnd</name> | |
<value>$jobEnd</value> | |
</property> | |
<property> | |
<name>initialInstance</name> | |
<value>$initialInstance</value> | |
</property> | |
</configuration> | |
"@ | |
#endregion edits | |
#region code | |
# create Oozie job | |
Write-Host "Sending the following Payload to the cluster:" -ForegroundColor Green | |
Write-Host "`n--------`n$OoziePayload`n--------" | |
$clusterUriCreateJob = "https://$HDIClusterName.azurehdinsight.net/oozie/v2/jobs" | |
$response = Invoke-RestMethod -Uri $clusterUriCreateJob -Method Post -Credential $creds -Body $OoziePayload -ContentType "application/xml;charset=UTF-8" -OutVariable $oozieJobId -debug -Verbose | |
$jsonResponse = ConvertFrom-Json (ConvertTo-Json -InputObject $response) | |
$oozieJobId = $jsonResponse[0].("id") | |
Write-Host "Oozie job id is $oozieJobId..." | |
# get job status | |
Write-Host "Sleeping for $waitTimeBetweenOozieJobStatusCheck seconds until the job metadata is populated in the Oozie metastore..." -ForegroundColor Green | |
Start-Sleep -Seconds $waitTimeBetweenOozieJobStatusCheck | |
Write-Host "Getting job status and waiting for the job to complete..." -ForegroundColor Green | |
$clusterUriGetJobStatus = "https://$HDIClusterName.azurehdinsight.net:443/oozie/v2/job/" + $oozieJobId + "?show=info" | |
$response = Invoke-RestMethod -Method Get -Uri $clusterUriGetJobStatus -Credential $creds | |
$jsonResponse = ConvertFrom-Json (ConvertTo-Json -InputObject $response) | |
$JobStatus = $jsonResponse[0].("status") | |
while($JobStatus -notmatch "SUCCEEDED|KILLED") | |
{ | |
Write-Host "$(Get-Date -format 'G'): $oozieJobId is in $JobStatus state...waiting $waitTimeBetweenOozieJobStatusCheck seconds for the job to complete..." | |
Start-Sleep -Seconds $waitTimeBetweenOozieJobStatusCheck | |
$response = Invoke-RestMethod -Method Get -Uri $clusterUriGetJobStatus -Credential $creds | |
$jsonResponse = ConvertFrom-Json (ConvertTo-Json -InputObject $response) | |
$JobStatus = $jsonResponse[0].("status") | |
} | |
Write-Host "$(Get-Date -format 'G'): $oozieJobId is in $JobStatus state!" -ForegroundColor Green | |
#endregion code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment