74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
import sys
|
|
import os
|
|
from datetime import datetime
|
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"..", "src")))
|
|
from typing import ClassVar, Optional
|
|
|
|
from lib.custom_logger import get_logger
|
|
logger = get_logger(level=10)
|
|
from dataclasses import dataclass
|
|
|
|
from models.bigquery_base_model import BigQueryBaseModel
|
|
from providers.google_cloud_bigquery_provider import GoogleCloudBigQueryProvider
|
|
|
|
@dataclass
|
|
class ExamplModel(BigQueryBaseModel):
|
|
device_code: str
|
|
time_stamp: datetime
|
|
|
|
table_id: ClassVar[str] = "example_dataset.example_table"
|
|
key_field: ClassVar[Optional[str]] = "device_code"
|
|
|
|
def example_model():
|
|
logger.info("Starting example_bigquery_model function.")
|
|
provider = GoogleCloudBigQueryProvider(
|
|
cred_path="keys/google_service_accout.json",
|
|
)
|
|
ExamplModel.set_provider(provider)
|
|
|
|
# テーブル内の全レコードを取得する
|
|
# records = ExamplModel.fetch_all()
|
|
# logger.info(f"Total records: {len(records)}")
|
|
# for record in records:
|
|
# logger.info(f"Record: {record}")
|
|
|
|
# レコードを生成する
|
|
# new_record = ExamplModel(
|
|
# device_code="device_010",
|
|
# time_stamp=datetime(2025, 1, 1, 15, 0, 0)
|
|
# )
|
|
# new_record.create()
|
|
|
|
# レコードを大量に生成する
|
|
# ExamplModel.insert(
|
|
# rows=[
|
|
# ExamplModel(device_code="device_011",time_stamp=datetime(2025, 1, 1, 15, 0, 0)),
|
|
# ExamplModel(device_code="device_012",time_stamp=datetime(2025, 1, 1, 15, 0, 0)),
|
|
# ])
|
|
|
|
# テーブルのストリームバッファ情報を取得する
|
|
if ExamplModel.is_streaming_buffer():
|
|
logger.info("Table is currently receiving streaming data.")
|
|
logger.info("not updated or delete in the streaming buffer.")
|
|
else:
|
|
logger.info("Table is not receiving streaming data.")
|
|
# 特定の条件でレコードを削除する
|
|
# ExamplModel.delete(where=[("device_code", "=", "device_010")])
|
|
# レコードを更新する
|
|
ExamplModel.update(
|
|
values={"device_code": "device_011_updated"},
|
|
where=[("device_code", "=", "device_011")]
|
|
)
|
|
|
|
|
|
|
|
# records = ExamplModel.list(where=[("device_code", "=", "device_001")])
|
|
# logger.info(f"Records with device_code='device_001': {records if records else 'No records found'}")
|
|
|
|
|
|
# record = ExamplModel.first()
|
|
# logger.info(f"First record: {record if record else 'No record found'}")
|
|
|
|
|
|
example_model() |