Skip to content

Instantly share code, notes, and snippets.

@RichieBzzzt
Created November 6, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichieBzzzt/c7218f91bb405bdfe930598c94558827 to your computer and use it in GitHub Desktop.
Save RichieBzzzt/c7218f91bb405bdfe930598c94558827 to your computer and use it in GitHub Desktop.
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