3
1

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 の replaceWhere による選択的上書きにて IN 句の利用可否の検証

Posted at

概要

Databricks の replaceWhere による選択的上書きにて IN 句(例:datasource in ("bbb.txt","ccc.txt"))の利用可否の検証結果を共有します。結論としては、IN 句で指定したデータが上書きされました。replaceWhere による選択的上書きとは、次のドキュメントにて記載されている機能です。

検証方法と検証結果

次のようなデータをもつテーブルを用意します。

image.png

datasource列にてbbb.txtccc.txtの値のレコードを次のようなデータに上書きします。

image.png

replaceWhere による選択的上書きを実施したところ、次のようにデータが上書きされました。

image.png

検証コード

1. スキーマを作成

%sql
create schema if not exists replace_test;

image.png

2. テーブル作成と初期データの挿入

%sql
create or replace table replace_test.tbl_01
(
  str_col string,
  int_col int,
  datasource string
);
 
INSERT INTO replace_test.tbl_01
(
  str_col,
  int_col,
  datasource
)
SELECT 'a',NULL,'aaa.txt'
UNION ALL
SELECT 'b',NULL,'aaa.txt'
UNION ALL
SELECT NULL,NULL,'bbb.txt'
UNION ALL
SELECT NULL,NULL,'bbb.txt'
UNION ALL
SELECT NULL,NULL,'ccc.txt'
UNION ALL
SELECT 'd',NULL,'ddd.txt'
UNION ALL
SELECT 'e',NULL,'ddd.txt'
;
select * from replace_test.tbl_01;

image.png

3. 上書きデータを保持したデータフレームを作成

src_d = [
    {
        'str_col':'c',
        'int_col':222,
        'datasource':'bbb.txt'
    },
    {
        'str_col':'d',
        'int_col':333,
        'datasource':'bbb.txt'
    },
    {
        'str_col':'e',
        'int_col':444,
        'datasource':'ccc.txt'
    },
]
 
src_s = """
str_col string,
int_col int,
datasource string
"""
 
df = spark.createDataFrame(src_d,src_s)
df.display()

image.png

4. replaceWhere による選択的上書きの実施と結果確認

(
    df.write
        .mode("overwrite")
        .option("replaceWhere", 'datasource in ("bbb.txt","ccc.txt")')
        .saveAsTable("replace_test.tbl_01"
    )
)
spark.table("replace_test.tbl_01").display()

image.png

5. リソースの作成

%sql
drop schema replace_test cascade

image.png

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?