Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
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
$54.99
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
$29.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
$54.99
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
$29.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$29.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Colombia

Standard delivery 10 - 13 business days

$19.95

Premium delivery 3 - 6 business days

$40.95
(Includes tracking information)

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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Colombia

Standard delivery 10 - 13 business days

$19.95

Premium delivery 3 - 6 business days

$40.95
(Includes tracking information)

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
Getting Started with DuckDB
$54.99
Mastering Node.js Web Development
$49.99
Machine Learning with PyTorch and Scikit-Learn
$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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela