【问题标题】:import table data and save as json documents in adls gen2 using databricks使用databricks在adls gen2中导入表数据并保存为json文档
【发布时间】:2020-08-03 18:40:39
【问题描述】:

我正在使用以下代码从 sql server 表生成 json 结果集。

Powershell:

$InstanceName = "SQLTEST1\ENG_TST1"
$connectionString = "Server=$InstanceName;Database=dbadb;Integrated Security=True;"

$query = "SELECT * FROM dbo.sales"

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = $query

$result = $command.ExecuteReader()

$table = new-object "System.Data.DataTable"

$table.Load($result)

$table | select $table.Columns.ColumnName | ConvertTo-Json

$connection.Close()

能否指导我使用 Azure Databricks 在 Azure Data Lake Storage Gen2 中存储 json 文档。

【问题讨论】:

    标签: powershell azure-databricks azure-data-lake-gen2


    【解决方案1】:

    您可以根据需要使用df.write.json API 写入任何特定位置。

    语法:df.write.json('location where you want to save the json file')

    示例:df.write.json("abfss://<file_system>@<storage-account-name>.dfs.core.windows.net/iot_devices.json")

    以下是使用 Azure Databricks 将 JSON 文档保存到 Azure Data Lake Gen2 的步骤。

    Step1:您可以使用spark.read.json API 读取json文件并创建数据框。

    第 2 步:可以使用以下文档中的说明将 blob 存储位置安装到 databricks dbfs 目录

    https://docs.microsoft.com/en-us/azure/databricks/data/data-sources/azure/azure-datalake-gen2

    Step3:然后使用df.write.json API 写入挂载点,该挂载点会写入blob存储

    更多详情,请参考以下文章:

    Azure Databricks – JSON files

    示例笔记本: https://docs.microsoft.com/en-us/azure/databricks/_static/notebooks/adls-passthrough-gen2.html

    【讨论】:

    • 谢谢@CHEEKATLAPRADEEP-MSFT
    • 如果我的回答对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢。
    【解决方案2】:

    如果要将文件保存到 Azure Databricks 中的 Azure Data Lake gen2,请参考以下步骤

    1. 创建一个 Azure Data Lake Storage Gen2 帐户。
    az login
    az storage account create \
        --name <account-name> \
        --resource-group <group name> \
        --location westus \
        --sku Standard_RAGRS \
        --kind StorageV2 \
        --enable-hierarchical-namespace true
    
    1. 创建服务主体并将 Storage Blob Data Contributor 分配给 Data Lake Storage Gen2 存储帐户范围内的 sp
    az login
    
    az ad sp create-for-rbac -n "MyApp" --role "Storage Blob Data Contributor" \
        --scopes /subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>
    
    1. 在 Azure Databricks 中创建 Spark 集群

    2. 在 Azure databricks(python) 中装载 Azure 数据湖 gen2

    configs = {"fs.azure.account.auth.type": "OAuth",
           "fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
           "fs.azure.account.oauth2.client.id": "<appId>",
           "fs.azure.account.oauth2.client.secret": "<clientSecret>",
           "fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/<tenant>/oauth2/token",
           "fs.azure.createRemoteFileSystemDuringInitialization": "true"}
    
    dbutils.fs.mount(
    source = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/folder1",
    mount_point = "/mnt/flightdata",
    extra_configs = configs)
    
    1. 将 json 保存到 azure data Lake gen2
    dbutils.fs.put("/mnt/flightdata/<file name>", """
    <json string>
    """, True)
    
    

    【讨论】:

    • @ITHelpGuy 您还有其他顾虑吗?如果您没有其他顾虑,您可以接受它作为答案吗?
    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 2021-03-06
    • 2020-02-01
    • 2020-08-27
    • 2022-01-13
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多