メインコンテンツへスキップ
メインコンテンツへスキップ

DataStore ファクトリーメソッド

DataStore は、ローカルファイル、データベース、クラウドストレージ、データレイクなど、さまざまなデータソースからインスタンスを生成するための 20 種類以上のファクトリーメソッドを提供します。

ユニバーサル URI インターフェイス

uri() メソッドは、ソースの種類を自動判別する、推奨される汎用的なエントリポイントです。

from chdb.datastore import DataStore

# Local files
ds = DataStore.uri("data.csv")
ds = DataStore.uri("/path/to/data.parquet")

# Cloud storage
ds = DataStore.uri("s3://bucket/data.parquet?nosign=true")
ds = DataStore.uri("https://example.com/data.csv")

# Databases
ds = DataStore.uri("mysql://user:pass@host:3306/db/table")
ds = DataStore.uri("postgresql://user:pass@host:5432/db/table")

URI 構文リファレンス

ソースタイプURI 形式
ローカルファイルpath/to/filedata.csv, /abs/path/data.parquet
S3s3://bucket/paths3://mybucket/data.parquet?nosign=true
GCSgs://bucket/pathgs://mybucket/data.csv
Azureaz://container/pathaz://mycontainer/data.parquet
HTTP/HTTPShttps://urlhttps://example.com/data.csv
MySQLmysql://user:pass@host:port/db/tablemysql://root:pass@localhost:3306/mydb/users
PostgreSQLpostgresql://user:pass@host:port/db/tablepostgresql://postgres:pass@localhost:5432/mydb/users
SQLitesqlite:///path?table=namesqlite:///data.db?table=users
ClickHouseclickhouse://host:port/db/tableclickhouse://localhost:9000/default/hits

ファイルソース

from_file

ローカルまたはリモートのファイルから、フォーマットを自動判別して DataStore を作成します。

DataStore.from_file(path, format=None, compression=None, **kwargs)

パラメータ:

パラメータデフォルト説明
pathstrrequiredファイルパス(ローカルまたは URL)
formatstrNoneファイル形式(None の場合は自動検出)
compressionstrNone圧縮形式(None の場合は自動検出)

サポートされている形式: CSV, TSV, Parquet, JSON, JSONLines, ORC, Avro, Arrow

例:

from chdb.datastore import DataStore

# Auto-detect format from extension
ds = DataStore.from_file("data.csv")
ds = DataStore.from_file("data.parquet")
ds = DataStore.from_file("data.json")

# Explicit format
ds = DataStore.from_file("data.txt", format="CSV")

# With compression
ds = DataStore.from_file("data.csv.gz", compression="gzip")

Pandas互換の読み込み関数

from chdb import datastore as pd

# CSV files
ds = pd.read_csv("data.csv")
ds = pd.read_csv("data.csv", sep=";", header=0, nrows=1000)

# Parquet files (recommended for large datasets)
ds = pd.read_parquet("data.parquet")
ds = pd.read_parquet("data.parquet", columns=['col1', 'col2'])

# JSON files
ds = pd.read_json("data.json")
ds = pd.read_json("data.jsonl", lines=True)

# Excel files
ds = pd.read_excel("data.xlsx", sheet_name="Sheet1")

Cloud ストレージ

from_s3

Amazon S3 から DataStore を作成します。

DataStore.from_s3(url, access_key_id=None, secret_access_key=None, format=None, **kwargs)

パラメータ:

パラメータデフォルト説明
urlstrrequiredS3 URL(s3://bucket/path)
access_key_idstrNoneAWS アクセスキー ID
secret_access_keystrNoneAWS シークレットアクセスキー
formatstrNoneファイル形式(自動検出)

例:

from chdb.datastore import DataStore

# Anonymous access (public bucket)
ds = DataStore.from_s3("s3://bucket/data.parquet")

# With credentials
ds = DataStore.from_s3(
    "s3://bucket/data.parquet",
    access_key_id="AKIAIOSFODNN7EXAMPLE",
    secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
)

# Using URI with query parameters
ds = DataStore.uri("s3://bucket/data.parquet?nosign=true")
ds = DataStore.uri("s3://bucket/data.parquet?access_key_id=KEY&secret_access_key=SECRET")

from_gcs

Google Cloud Storage から DataStore を作成します。

DataStore.from_gcs(url, credentials_path=None, **kwargs)

使用例:

ds = DataStore.from_gcs("gs://bucket/data.parquet")
ds = DataStore.from_gcs("gs://bucket/data.parquet", credentials_path="/path/to/creds.json")

from_azure

Azure Blob Storage から DataStore を作成します。

DataStore.from_azure(url, account_name=None, account_key=None, **kwargs)

例:

ds = DataStore.from_azure(
    "az://container/data.parquet",
    account_name="myaccount",
    account_key="mykey"
)

from_hdfs

HDFS から DataStore を作成します。

DataStore.from_hdfs(url, **kwargs)

使用例:

ds = DataStore.from_hdfs("hdfs://namenode:8020/path/data.parquet")

from_url

HTTP/HTTPS の URL から DataStore を作成します。

DataStore.from_url(url, format=None, **kwargs)

使用例:

ds = DataStore.from_url("https://example.com/data.csv")
ds = DataStore.from_url("https://raw.githubusercontent.com/user/repo/main/data.parquet")

データベース

from_mysql

MySQL データベースから DataStore を作成します。

DataStore.from_mysql(host, database, table, user, password, port=3306, **kwargs)

パラメーター:

パラメーターデフォルト説明
hoststr必須MySQL ホスト名
databasestr必須データベース名
tablestr必須テーブル名
userstr必須ユーザー名
passwordstr必須パスワード
portint3306ポート番号

使用例:

ds = DataStore.from_mysql(
    host="localhost",
    database="mydb",
    table="users",
    user="root",
    password="password"
)

# Using URI
ds = DataStore.uri("mysql://root:password@localhost:3306/mydb/users")

from_postgresql

PostgreSQL データベースから DataStore を作成します。

DataStore.from_postgresql(host, database, table, user, password, port=5432, **kwargs)

使用例:

ds = DataStore.from_postgresql(
    host="localhost",
    database="mydb",
    table="users",
    user="postgres",
    password="password"
)

# Using URI
ds = DataStore.uri("postgresql://postgres:password@localhost:5432/mydb/users")

from_clickhouse

ClickHouse サーバーから DataStore を作成します。

DataStore.from_clickhouse(host, database, table, user=None, password=None, port=9000, **kwargs)

例:

ds = DataStore.from_clickhouse(
    host="localhost",
    database="default",
    table="hits",
    user="default",
    password=""
)

# Connection-level mode (explore databases)
ds = DataStore.from_clickhouse(
    host="analytics.company.com",
    user="analyst",
    password="secret"
)
ds.databases()                  # List databases
ds.tables("production")         # List tables
result = ds.sql("SELECT * FROM production.users LIMIT 10")

from_mongodb

MongoDB から DataStore を作成します。

DataStore.from_mongodb(uri, database, collection, **kwargs)

使用例:

ds = DataStore.from_mongodb(
    uri="mongodb://localhost:27017",
    database="mydb",
    collection="users"
)

from_sqlite

SQLite データベースから DataStore を作成します。

DataStore.from_sqlite(database_path, table, **kwargs)

例:

ds = DataStore.from_sqlite("data.db", table="users")

# Using URI
ds = DataStore.uri("sqlite:///data.db?table=users")

データレイク

from_iceberg

Apache Iceberg テーブルから DataStore を作成します。

DataStore.from_iceberg(path, **kwargs)

使用例:

ds = DataStore.from_iceberg("/path/to/iceberg_table")
ds = DataStore.uri("iceberg://catalog/namespace/table")

from_delta

Delta Lake のテーブルから DataStore を作成します。

DataStore.from_delta(path, **kwargs)

例:

ds = DataStore.from_delta("/path/to/delta_table")
ds = DataStore.uri("deltalake:///path/to/delta_table")

from_hudi

Apache Hudi テーブルから DataStore を作成します。

DataStore.from_hudi(path, **kwargs)

使用例:

ds = DataStore.from_hudi("/path/to/hudi_table")
ds = DataStore.uri("hudi:///path/to/hudi_table")

インメモリ ソース

from_df / from_dataframe

pandas DataFrame から DataStore を作成します。

DataStore.from_df(df, name=None)
DataStore.from_dataframe(df, name=None)  # alias

使用例:

import pandas
from chdb.datastore import DataStore

pdf = pandas.DataFrame({'a': [1, 2, 3], 'b': ['x', 'y', 'z']})
ds = DataStore.from_df(pdf)

DataFrame コンストラクタ

pandas 風のコンストラクタを使用して DataStore を作成します。

from chdb import datastore as pd

# From dictionary
ds = pd.DataFrame({
    'name': ['Alice', 'Bob'],
    'age': [25, 30]
})

# From pandas DataFrame
import pandas
pdf = pandas.DataFrame({'a': [1, 2, 3]})
ds = pd.DataFrame(pdf)

特殊なデータソース

from_numbers

連番の数値を生成する DataStore を作成します(テスト用途に便利です)。

DataStore.from_numbers(count, **kwargs)

使用例:

ds = DataStore.from_numbers(1000000)  # 1M rows with 'number' column
result = ds.filter(ds['number'] % 2 == 0).head(10)  # Even numbers

from_random

ランダムデータから DataStore を作成します。

DataStore.from_random(rows, columns, **kwargs)

使用例:

ds = DataStore.from_random(rows=1000, columns=5)

run_sql

生の SQL クエリから DataStore を生成します。

DataStore.run_sql(query)

例:

ds = DataStore.run_sql("""
    SELECT number, number * 2 as doubled
    FROM numbers(100)
    WHERE number % 10 = 0
""")

概要表

Methodソース種別
uri()汎用DataStore.uri("s3://bucket/data.parquet")
from_file()ローカル/リモートファイルDataStore.from_file("data.csv")
read_csv()CSVファイルpd.read_csv("data.csv")
read_parquet()Parquetファイルpd.read_parquet("data.parquet")
from_s3()Amazon S3DataStore.from_s3("s3://bucket/path")
from_gcs()Google Cloud StorageDataStore.from_gcs("gs://bucket/path")
from_azure()Azure BlobDataStore.from_azure("az://container/path")
from_hdfs()HDFSDataStore.from_hdfs("hdfs://host/path")
from_url()HTTP/HTTPSDataStore.from_url("https://example.com/data.csv")
from_mysql()MySQLDataStore.from_mysql(host, db, table, user, pass)
from_postgresql()PostgreSQLDataStore.from_postgresql(host, db, table, user, pass)
from_clickhouse()ClickHouseDataStore.from_clickhouse(host, db, table)
from_mongodb()MongoDBDataStore.from_mongodb(uri, db, collection)
from_sqlite()SQLiteDataStore.from_sqlite("data.db", table)
from_iceberg()Apache IcebergDataStore.from_iceberg("/path/to/table")
from_delta()Delta LakeDataStore.from_delta("/path/to/table")
from_hudi()Apache HudiDataStore.from_hudi("/path/to/table")
from_df()pandas DataFrameDataStore.from_df(pandas_df)
DataFrame()辞書/DataFramepd.DataFrame({'a': [1, 2, 3]})
from_numbers()連番データDataStore.from_numbers(1000000)
from_random()ランダムデータDataStore.from_random(rows=1000, columns=5)
run_sql()生のSQLDataStore.run_sql("SELECT * FROM ...")