Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Getting Started with DuckDB
Getting Started with DuckDB

Getting Started with DuckDB: A practical guide for accelerating your data science, data analytics, and data engineering workflows

Arrow left icon
Profile Icon Simon Aubury Profile Icon Ned Letcher
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback Jun 2024 382 pages 1st Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Simon Aubury Profile Icon Ned Letcher
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback Jun 2024 382 pages 1st Edition
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Getting Started with DuckDB

Loading Data into DuckDB

DuckDB is a flexible analytical database that can handle a variety of data types and workloads. A common task when working with DuckDB is loading data from external data sources, such as comma-separated values (CSV), JavaScript Object Notation (JSON), and Apache Parquet files. In this chapter, we will introduce the basic concepts and methods for loading data into DuckDB and provide some examples and best practices to help you get started.

You will learn how to load data into DuckDB from external data sources, how to create tables using SQL commands, and how to load data from various sources and formats, including CSV, JSON, and Parquet files, along with exploring some of the considerations when working with compressed columnar formats. We will also use DuckDB to query and analyze a public dataset, in addition to reviewing how we can export data from DuckDB.

In this chapter, we’re going to cover the following main topics:

  • Loading CSV files...

Technical requirements

We need to get data into DuckDB so that it can be queried, transformed, and analyzed. The files and example data for these exercises are available in the chapter_02 folder at https://github.com/PacktPublishing/Getting-Started-with-DuckDB/tree/main/chapter_02.

Important note

In this chapter, we will be learning how to load data into (and export data from) DuckDB using local files. DuckDB can also read and write data located in Simple Storage Service (S3)-compatible object stores, including AWS S3, Google Cloud Storage, and Azure Blob storage. We will explore using DuckDB to interact with object stores in Chapter 5.

Loading CSV files

CSV files are ubiquitous in the world of analytical data, which is why DuckDB comes with a powerful and flexible built-in CSV parser. The appeal of this simple text-based format is that CSV files are easy to inspect, and their tabular format is readily comprehended. While they are straightforward to produce and share, there are, however, often challenges when working with CSV files. Notably, they come in a wide variety of dialects and, often, non-standard variations. For example, despite their name, they sometimes use characters other than commas for delimiting each field—such as tabs, they may or may not have a header row with column names, and there are different approaches to escaping special characters, such as delimiters and quotes. When parsing a CSV file, the specific format of a CSV file can often be inferred but may need to be specified manually. Furthermore, CSV files don’t contain an embedded schema, meaning that conversion from text values...

Loading JSON files

JSON is a popular and open file format for storing and exchanging data and has good integration with many programming languages, libraries, and data systems. A JSON object is written inside curly braces and can contain multiple name-value pairs. A name-value pair consists of an attribute name in double quotes, followed by a colon, followed by a value. JSON values can include strings, numbers, and Boolean data types, as well as nested objects and array data types, which are represented as comma-separated sequences of values, wrapped with square braces. Here’s an example of a simple JSON object:

{"food_name":"Hawaiian Pizza", "quantity":6}

DuckDB can read JSON files with the read_json function in a variety of formats. In the wild, you may encounter a range of different types of JSON files, with records represented using different conventions. Two common flavors of JSON data that you may encounter in the wild are newline-delimited...

Working with Parquet files

Apache Parquet is an open source file format that is designed for efficient storage and retrieval of data. Their columnar-oriented format combined with the use of compression to reduce storage space and I/O cost of reading and writing make these files well suited for storing and retrieving large amounts of structured and semi-structured data for analytical applications.

Parquet files are encoded in a binary format, so you cannot view them as text files as you might with a CSV file. Parquet files are self-describing in that each file contains both data and metadata describing the schema of the data within the file. This means that column names, their data types, and summary information about the number of rows and columns are encoded within the file. This contrasts with CSV and JSON files, which contain purely text data without an embedded schema. In addition to performance gains, this is one of the notable benefits of Parquet files, as their built-in schema...

Exploring public datasets

Public datasets are collections of data that are made available to the public by various sources, such as governments, organizations, and researchers. Public datasets can be useful for analyzing trends and patterns in different domains, such as health, education, environment, or social impact.

DuckDB is a powerful tool for exploring, understanding, and gaining insights from public datasets. In this section, we’ll work with a public dataset that has been made available in CSV format. We’ll use DuckDB to load it in an appropriate form, summarize it, and export it to another format. This worked example will allow us to showcase some of DuckDB’s versatile features that make it well suited for analytical workflows.

Bike-share station readings

We are going to be exploring the Melbourne Bike Share dataset, which provides historical data from the Melbourne Bike Share service, which operated from 2010 to 2019, and is made available by...

Exporting data

So far, we have been importing data from a variety of data sources into DuckDB. We also often need to export data from DuckDB into a file, perhaps to transfer to another database system or to share our data with others. Let’s discuss how we can achieve that in the following sections.

Exporting a table into a CSV file

We can use the COPY ... TO command to export data from DuckDB to an external CSV, JSON, or Parquet file. This command can be called either with a table name or a query and will export the corresponding data to disk in the desired file format. Let’s try this out by creating a subset of our bike-share dataset and exporting it as a CSV file.

We’ll first create a table called bike_readings_april containing bike rides that occurred in April:

CREATE OR REPLACE TABLE bike_readings_april AS
SELECT *
FROM bikes
WHERE RUNDATE BETWEEN '2017-04-01' AND '2017-04-30';

With the bike_readings_april table created, let...

Summary

In this chapter, we learned how to load data into and unload data from DuckDB. We saw data as text formats in the form of CSV and JSON, as well as the self-describing binary columnar format, Apache Parquet.

We learned techniques to format data during import and skip records with errors, and we saw how DuckDB can support a variety of changing schemas and data types. We also learned how to find, process, and summarize public datasets and saw how DuckDB can be used to export data for consumption by analytical systems.

Now that we know how to load data into DuckDB, the next chapter will cover techniques for using DuckDB for data manipulation in order to transform your data. You will learn how to clean and reshape data using SQL and use these approaches to manipulate data from different sources and formats. You will also see how to interact with data located on remote systems, such as data located on remote web servers.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Use DuckDB to rapidly load, transform, and query data across a range of sources and formats
  • Gain practical experience using SQL, Python, and R to effectively analyze data
  • Learn how open source tools and cloud services in the broader data ecosystem complement DuckDB’s versatile capabilities
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

DuckDB is a fast in-process analytical database. Getting Started with DuckDB offers a practical overview of its usage. You'll learn to load, transform, and query various data formats, including CSV, JSON, and Parquet. The book covers DuckDB's optimizations, SQL enhancements, and extensions for specialized applications. Working with examples in SQL, Python, and R, you'll explore analyzing public datasets and discover tools enhancing DuckDB workflows. This guide suits both experienced and new data practitioners, quickly equipping you to apply DuckDB's capabilities in analytical projects. You'll gain proficiency in using DuckDB for diverse tasks, enabling effective integration into your data workflows.

Who is this book for?

If you’re interested in expanding your analytical toolkit, this book is for you. It will be particularly valuable for data analysts wanting to rapidly explore and query complex data, data and software engineers looking for a lean and versatile data processing tool, along with data scientists needing a scalable data manipulation library that integrates seamlessly with Python and R. You will get the most from this book if you have some familiarity with SQL and foundational database concepts, as well as exposure to a programming language such as Python or R.

What you will learn

  • Understand the properties and applications of a columnar in-process database
  • Use SQL to load, transform, and query a range of data formats
  • Discover DuckDB's rich extensions and learn how to apply them
  • Use nested data types to model semi-structured data and extract and model JSON data
  • Integrate DuckDB into your Python and R analytical workflows
  • Effectively leverage DuckDB's convenient SQL enhancements
  • Explore the wider ecosystem and pathways for building DuckDB-powered data applications

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 24, 2024
Length: 382 pages
Edition : 1st
Language : English
ISBN-13 : 9781803241005
Category :
Languages :
Concepts :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 24, 2024
Length: 382 pages
Edition : 1st
Language : English
ISBN-13 : 9781803241005
Category :
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 159.97
Machine Learning with PyTorch and Scikit-Learn
$54.99
Mastering Node.js Web Development
$49.99
Getting Started with DuckDB
$54.99
Total $ 159.97 Stars icon
Banner background image

Table of Contents

14 Chapters
Chapter 1: An Introduction to DuckDB Chevron down icon Chevron up icon
Chapter 2: Loading Data into DuckDB Chevron down icon Chevron up icon
Chapter 3: Data Manipulation with DuckDB Chevron down icon Chevron up icon
Chapter 4: DuckDB Operations and Performance Chevron down icon Chevron up icon
Chapter 5: DuckDB Extensions Chevron down icon Chevron up icon
Chapter 6: Semi-Structured Data Manipulation Chevron down icon Chevron up icon
Chapter 7: Setting up the DuckDB Python Client Chevron down icon Chevron up icon
Chapter 8: Exploring DuckDB’s Python API Chevron down icon Chevron up icon
Chapter 9: Exploring DuckDB’s R API Chevron down icon Chevron up icon
Chapter 10: Using DuckDB Effectively Chevron down icon Chevron up icon
Chapter 11: Hands-On Exploratory Data Analysis with DuckDB Chevron down icon Chevron up icon
Chapter 12: DuckDB – The Wider Pond Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Vishnuvardhan Oct 17, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
"Getting Started with DuckDB" provides an excellent, hands-on introduction to DuckDB, showcasing its speed and versatility in data analytics and engineering. The practical examples and easy-to-follow explanations make it a valuable resource for anyone looking to enhance their workflows. Ideal for beginners and experienced professionals alike, it bridges the gap between theory and application effectively
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.