Singdata Lakehouse へのデータ取り込み完全ガイド

データ取り込み:ZettaPark で SQL INSERT を使ってデータを読み込む

概要

Singdata Lakehouse は、PySpark と互換性のある ZettaPark を提供しています。VS Code などの一般的な IDE で Python と SQL を使い、Singdata Lakehouse のテーブルにデータを読み込めます。

ユースケース

この方法では、SQL の実行やファイルのアップロードを簡単に行えます。データを読み込む方法の一つは、レコードごとに SQL INSERT 文を実行することです。Python 環境で少量のデータを取り込む場合に適しています。データの挿入には便利ですが、Singdata Lakehouse は従来型のデータベースとは異なり、大量データの一括書き込みに最適化されているため、この方法は大量データの読み込みには効率的ではありません。

実装手順

お使いのコンピューターで VS Code を開き、py_zettapark_sql_insert.py という名前のファイルを作成します。以下のコードを py_zettapark_sql_insert.py にコピーします。

import json,gzip from clickzetta.zettapark.session import Session from datetime import datetime

設定ファイルからパラメーターを読み込みます。

with open('config-ingest.json', 'r') as config_file: config = json.load(config_file) print("Connecting to Singdata Lakehouse.....\n")

セッションを作成します。

session = Session.builder.configs(config).create() print("Connection successful!...\n") target_table_name = "lift_tuckets_import_by_py_insert" create_target_table_query = f""" CREATE TABLE if not exists ql_ws.ingest.{target_table_name}( `txid` string, `rfid` string, `resort` string, `purchase_time` timestamp_ltz, `expiration_time` date, `days` int, `name` string, `address_street` string, `address_city` string, `address_state` string, `address_postalcode` string, `phone` string, `email` string, `emergency_contact_name` string, `emergency_contact_phone` string) """ session.sql(create_target_table_query).collect() def save_to_clickzetta(session, message): record = json.loads(message) print('inserting record to Singdata Lakehouse') # Convert date and time fields purchase_time = datetime.strptime(record['purchase_time'], '%Y-%m-%d %H:%M:%S') expiration_time = datetime.strptime(record['expiration_time'], '%Y-%m-%d').date() row = ( f"'{record['txid']}'", f"'{record['rfid']}'", f"'{record['resort']}'", f"timestamp_ltz '{record['purchase_time']}'", f"date '{record['expiration_time']}'", record['days'], f"'{record['name']}'", f"'{record['address_street']}'", f"'{record['address_city']}'", f"'{record['address_state']}'", record['address_postalcode'], record['phone'], f"'{record['email']}'", f"'{record['emergency_contact_name']}'", record['emergency_contact_phone'] ) sql_query = f""" INSERT INTO ql_ws.ingest.{target_table_name} (TXID, RFID, RESORT, PURCHASE_TIME, EXPIRATION_TIME, DAYS, NAME, ADDRESS_STREET, ADDRESS_CITY, ADDRESS_STATE, ADDRESS_POSTALCODE, PHONE, EMAIL, EMERGENCY_CONTACT_NAME, EMERGENCY_CONTACT_PHONE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """ session.sql(sql_query, row).collect() print(f"inserted ticket {record}") if __name__ == "__main__": # Open the JSON file and read the content with gzip.open('lift_tickets_data.json.gz', 'rt', encoding='utf-8') as file: for message in file: if message.strip(): # Ensure it's not an empty line save_to_clickzetta(session, message) session.close() print("Ingest complete")

VS Code で新しい「ターミナル」を開き、次のコマンドを実行して「環境設定」手順で作成した Python 環境を有効にします。すでに cz-ingest-examples 環境を使用している場合は、この手順を省略します。

conda activate cz-ingest-examples

続けて、同じターミナルで次のコマンドを実行します。

python py_zettapark_sql_insert.py

次のステップ

最適化:この方法は、大量のデータを読み込むには効率的ではありません。タスクの並列度を上げ、各 INSERT INTO で複数のレコードを挿入すると、パフォーマンスを改善できます。

参考資料

SQL の INSERT INTO 文

ZettaPark クイックスタート