0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Databricks の Serverless クラスターにて Workspace ファイルの参照可否の検証結果

0
Posted at

概要

Databricks の Serverless クラスターから Workspace ファイルを参照する方法について検証した結果を共有します。下記のディレクトリ構成を前提に、load_config ノートブックから setting.json を参照できるかを確認しました。

root/
  jobs/
    load_config.py
  config/
    setting.json

事前準備

Serverless クラスターのアタッチ

image.png

ワークスペースファイル(setting.json)の作成

config フォルダを作成し、その配下に setting.json を作成して下記の内容を記述します。

[
  {
    "key": "table_1",
    "args": {
      "full_table_name": "samples.tpch.nation"
    }
  },
  {
    "key": "table_2",
    "args": {
      "full_table_name": "samples.tpch.region"
    }
  },
  {
    "key": "table_3",
    "args": {
      "full_table_name": "samples.tpch.customer"
    }
  }
]

image.png

実行するノートブックの作成

jobs フォルダを作成し、その配下に load_config ノートブックを作成します。まず、現在のディレクトリを確認します。

%sh
pwd

image.png

続いて、ディレクトリおよびファイルのパスを変数にセットします。

from pathlib import Path
import json

print(Path.cwd())
root_dir = Path.cwd().parent
print(root_dir)
config_path = root_dir / "config" / "setting.json"
config_path_str = str(config_path)
print(config_path_str)

image.png

検証

1. Python の標準ライブラリからのアクセス

アクセス可能

with open(config_path, "r", encoding="utf-8") as f:
    config = json.load(f)
print(type(config))
print(config)

image.png

2. dbutils からのアクセス

アクセス可能

dbutils.fs.ls("file:" + config_path_str)
config = json.loads(dbutils.fs.head("file:" + config_path_str))
print(type(config))
print(config)

image.png

3. Spark DataFrame からのアクセス

アクセス不可

df = spark.read.json(config_path_str)
display(df)

df = spark.read.json("file:" + config_path_str)
display(df)

image.png

検証結果のまとめ

# アクセス方法 結果
1 Python 標準ライブラリ(open 可能
2 dbutils.fs 可能
3 Spark DataFrame(spark.read 不可
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?