Skip to Content
DuckDB: 起動して実行中
book

DuckDB: 起動して実行中

by Wei-Meng Lee
March 2025
Intermediate to advanced
308 pages
5h 8m
Japanese
O'Reilly Media, Inc.
Content preview from DuckDB: 起動して実行中

第2章. DuckDBにデータをインポートする

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

第1章では、簡単なDuckDBデータベースを作成し、そこにテーブルをロードする方法を説明した。実世界では、CSV、Excel、Parquet、データベースサーバなど、さまざまなデータソースやファイル形式からデータを取得することが多い。この章では、まずDuckDBデータベースを作成するさまざまな方法を学び、次にさまざまなデータソースを使用してデータベースをロードする方法を学ぶ。この章が終わるころには、各データソースの扱い方と、それを扱うためのヒントやコツについて、明確な考えを持つことができるだろう。

DuckDBデータベースを作成する

このセクションでは、、DuckDBデータベースを作成するさまざまな方法を紹介し、どのメソッドがあなたの目的に合うかを提案する。

DuckDBデータベースを作成する最も簡単な方法は、duckdb モジュールのconnect() 関数を使用することである:

import duckdb

conn = duckdb.connect()

connect() 関数はDuckDBPyConnection オブジェクトを返す。デフォルトでは、この文はここで見られるように、変更可能なin-memoryデータベースを開く:

conn = duckdb.connect(':memory:')

ストレージ上に永続化されるDuckDBデータベースを作成したい場合は、database 引数にデータベースの名前、例えばmydb.duckdbをセットする(ファイル名には任意の拡張子を使用できる):

conn = duckdb.connect(database = 'mydb.duckdb', read_only = False)

このステートメントを最初に実行すると、mydb.duckdbデータベースファイルがコード(Jupyterノートブックなど)と同じフォルダに作成される。データベースファイルがすでに存在する場合のみ、引数read_onlyTrue にセットできる。複数のPythonプロセスが同時に同じデータベースファイルにアクセスする必要がある場合は、読み取り専用モードでファイルを開く必要がある。

このin文を実行すると、カレントディレクトリにmydb.duckdbファイルが表示される。

インメモリDuckDBデータベース を作成し、引数read_onlyTrue に設定すると、データベースは不変性(読み取り専用)となり、テーブルをアタッチできなくなる。したがって、インメモリデータベースの場合、read_only 引数を必ずFalse に設定すること(または、False がデフォルトであるため、単に省略すること)。

DuckDBデータベースが作成されたので、次のセクションではさまざまなデータソース、形式からデータをロードする方法を学ぶ。

異なるデータソースと形式からデータをロードする

DuckDBは様々なデータソースやファイル形式に対応している。このセクションでは、以下のファイルからデータを読み込む方法を学ぶ:

  • CSVファイル

  • パーケットファイル

  • エクセルファイル

  • MySQL データベース

CSVファイルを扱う

最も一般的なデータソースのファイル形式のひとつがCSVファイルである。これは、表形式のデータを保存し、交換するための一般的で簡単な方法である。CSVファイルは以下のようなものである: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AWSでのサーバーレス開発

AWSでのサーバーレス開発

Sheen Brisals, Luke Hedger
初めてのGraphQL ―Webサービスを作って学ぶ新世代API

初めてのGraphQL ―Webサービスを作って学ぶ新世代API

Eve Porcello, Alex Banks, 尾崎 沙耶, あんどうやすし
エンジニアが学ぶ会計システムの「知識」と「技術」

エンジニアが学ぶ会計システムの「知識」と「技術」

広川 敬祐, 五島 伸二, 小田 恭彦, 大塚 晃, 川勝 健司

Publisher Resources

ISBN: 9798341633889