簡體   English   中英

將 json 保存到 Azure Data Lake Storage Gen 2 中的文件

[英]Save json to a file in Azure Data Lake Storage Gen 2

在 Databricks 中,使用 Python,我正在使用 requests 庫發出獲取請求,響應是 json。

以下是獲取請求的示例:

json_data = requests.get("https://prod-noblehire-api-000001.appspot.com/job?").json()

我想將 json_data 變量保存為 Azure Data Lake Storage 中的文件。 我不想先將其讀入 Pyspark/Pandas DataFrame 然后保存。

如果我將它保存到計算機上的本地文件夾中,我會使用以下代碼:

j = json.dumps(json_data)
with open("MyJsonFile.json", "w") as f:
    f.write(j)
    f.close()

但是,由於我想將其保存在 Azure 數據湖存儲中,因此根據 Microsoft 的文檔,我應該使用以下內容:

def upload_file_to_directory():
    try:

        file_system_client = service_client.get_file_system_client(file_system="my-file-system")

        directory_client = file_system_client.get_directory_client("my-directory")
        
        file_client = directory_client.create_file("uploaded-file.txt")
        local_file = open("C:\\file-to-upload.txt",'r')

        file_contents = local_file.read()

        file_client.append_data(data=file_contents, offset=0, length=len(file_contents))

        file_client.flush_data(len(file_contents))

    except Exception as e:
      print(e)

如何結合這兩段代碼將變量保存為 ADLS 中的文件? 另外,有沒有更好的方法來做到這一點?

您實際上不必在本地保存。 相反,您可以掛載您的 ADLS 存儲帳戶,然后將所需的 JSON 內容寫入其中。 下面是對我有用的代碼。

import requests
import json

json_data = requests.get("<YOUR_URL>").json()
j = json.dumps(json_data)
with open("/<YOUR_MOUNT_POINT>/<FILE_NAME>.json", "w") as f:
    f.write(j)
    f.close()

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM