Created
November 6, 2019 12:26
-
-
Save RichieBzzzt/c7218f91bb405bdfe930598c94558827 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
function Set-LocalNotebook ($DatabricksFile, $Language, $LocalOutputPath, $Format = "SOURCE") { | |
$DatabricksFileForUrl = Format-DataBricksFileName -DataBricksFile $DatabricksFile | |
$uri = "$global:DatabricksURI/api/2.0/workspace/export?path=" + $DatabricksFileForUrl + "&format=$Format&direct_download=false" | |
switch ($Format) { | |
"SOURCE" { | |
$FileExtentions = @{"PYTHON" = ".py"; "SCALA" = ".scala"; "SQL" = ".sql"; "R" = ".r" } | |
$FileExt = $FileExtentions[$Language] | |
} | |
"HTML" { | |
$FileExt = ".html" | |
} | |
"JUPYTER" { | |
$FileExt = ".ipynb" | |
} | |
"DBC" { | |
$FileExt = ".dbc" | |
} | |
} | |
$LocalExportPath = $DatabricksFile.Replace($ExportPath + "/", "") + $FileExt | |
$LocalExportPath = Join-Path $LocalOutputPath $LocalExportPath | |
$Headers = GetHeaders $null | |
Try { | |
# Databricks exports with a comment line in the header, remove this and ensure we have Windows line endings | |
$Response = Invoke-RestMethod -Method Get -Uri $uri -Headers $Headers | |
$decoded = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($Response.content)) | |
if ($Format -eq "SOURCE") { | |
$decoded = ($decoded.replace("[^`r]`n", "`r`n") -Join "`r`n") | |
} | |
Write-Verbose "Creating file $LocalExportPath" | |
New-Item -force -path $LocalExportPath -value $decoded -type file | out-null | |
} | |
Catch { | |
Write-Error $_.ErrorDetails.Message | |
Throw | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment