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?

More than 1 year has passed since last update.

Databricks Assistant にて Scala で記述されている Bloom filter indexes の一覧を表示するコードを python への書き換える方法

Posted at

概要

Databricks Assistant によて Scala で記述されている Bloom filter indexes の一覧を表示するコードを python への書き換えに成功したため、共有します。ドキュメントにて Scala のコードしか記載されておらず困っていたのですが、Databricks Assistant が python に書き換えてくれました。本記事ではプロンプトとその出力結果を共有します。

image.png

引用元:ブルーム フィルターのインデックス - Azure Databricks | Microsoft Learn

実行結果

事前準備

%sql
CREATE SCHEMA IF NOT EXISTS hive_metastore.uc_migration_01;
CREATE OR REPLACE TABLE hive_metastore.uc_migration_01.nation_02
(
  N_NATIONKEY integer
  ,N_NAME string
  ,N_REGIONKEY integer
  ,N_COMMENT string
);
CREATE BLOOMFILTER INDEX ON TABLE hive_metastore.uc_migration_01.nation_02 
  FOR COLUMNS(
    N_NAME OPTIONS (
      fpp = 0.1,
      numItems = 50000000
    )
  )

image.png

Databricks Assitant への質問と回答

次の Scala のコードを python に書き換えて

```scala
spark.table("<table-with-indexes>").schema.foreach(field => println(s"${field.name}: metadata=${field.metadata}"))
```

次のようにPythonでコードを書き換えることができます。

```python
for field in spark.table("<table-with-indexes>").schema:
    print(f"{field.name}: metadata={field.metadata}")
```

Scala では、spark.table() function の呼び出しによってDataFrameが返され、.schema Property が使用されます。 Python では同じ syntax でDataFrameを取得できます。for ループにより各フィールドの名前と metadata が表示されます。 f-string syntax (Python 3.6以降) が使用され、Stringの Interpolation が行われています。

image.png

Databricks 上での実行結果

for field in spark.table("hive_metastore.uc_migration_01.nation_02").schema:
    print(f"{field.name}: metadata={field.metadata}")

image.png

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?