13

用 Python 的 DuckDB 下 SQL 指令翻 Parquet 的資料

 3 years ago
source link: https://blog.gslin.org/archives/2021/06/28/10223/%e7%94%a8-python-%e7%9a%84-duckdb-%e4%b8%8b-sql-%e6%8c%87%e4%bb%a4%e7%bf%bb-parquet-%e7%9a%84%e8%b3%87%e6%96%99/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

用 Python 的 DuckDB 下 SQL 指令翻 Parquet 的資料

在「Querying Parquet using DuckDB」這邊看到 DuckDB 這個東西,裡面引用的文章是「Querying Parquet with Precision using DuckDB」,可以直接對 Parquet 格式的資料下 SQL 找資料。

先前好像有看到 DuckDB 但沒有太注意,剛剛再次看到,然後玩了一下還蠻有趣的。DuckDB 支援蠻多程式語言與資料格式,不過這邊文章拿 Python 與 Parquet 玩還蠻有趣的...

先把 Parquet 的範例資料抓下來,然後透過 pip 裝 duckdb:

cd /tmp; wget https://github.com/cwida/duckdb-data/releases/download/v1.0/taxi_2019_04.parquet; pip install -U duckdb

然後進到 Python 3 的互動界面:

>>> import duckdb
>>> print(duckdb.query("SELECT COUNT(*) FROM 'taxi_2019_04.parquet' WHERE pickup_at BETWEEN '2019-04-15' AND '2019-04-20'").fetchall())
[(1276565,)]

然後在範例裡面,檔名的部份還可以用 *,看了一下說明,底層是 glob 類的用法:

DuckDB supports the globbing syntax, which allows it to query all three files simultaneously.

文章裡有提到速度比 Pandas 快很多,不過我覺得這好像不太能這樣比,會拿 Pandas 出來的時候常常是其他用法,但至少看起來速度是個 DuckDB 在意的點。

不過反而馬上想到的是,之後處理 CSV 之類的檔案應該也會試看看 DuckDB...

Related

用 TSV 而不用 CSV

最近常常需要提供資料給其他部門 (非技術類的部門),有時候需要提供一些表格類的資料,傳統大多數人比較熟的是產生 CSV 格式的資料讓使用者可以用 Excel 打開,但這個格式其實有很多問題,最常見的就是 encoding 與資料有逗號 comma 的問題。 如果是在 Python 下,其中一個解法是用 openpyxl 直接產生 .xlsx,但用起來還是沒那麼有下面提到的方法順手。 如果是 shell script 時就比較麻煩了,像我這次手上有一堆影片檔,要用 FFmpeg 確認每個影片的 resolution 與 framerate 再提供給同事,這時候如果還是想產生 .xlsx 就累了... 下面要提到的解法好像記得是在 K 社的時候同事教的,用 TSV 檔格式 (當然檔名要取 .tsv),然後 encoding 用 UTF-16 (LE) 就可以解決上面提到的兩個問題,產生出來的檔案可以讓 Excel 直接打開。 在 StackOverflow 上的「Is it possible to force Excel…

March 4, 2020

In "Computer"

SAML、zxid、Lasso

OASIS Security Services (SAML) TC 是早期 (2002 年的年底發表 SAML 1.0) 解決 Single sign-on 問題的公開標準之一,通訊的過程主要是以 XML 以及 XML 的變形作為資料傳遞的格式。因為是很早期就存在的標準架構,所以 Google Apps 的 SSO API 就是走 SAML。 拿 SAML 去查,應該都會查到 zxid 這個 library,不過這個 library 先天不良加上後天失調,問題實在太多,前景又不看好... 缺乏文件,tarball 裡面的 document 只有 README 能看 (http://zxid.org/README.zxid.html,另有 PDF 格式),不過整個文件最後面將近一半都是 Schema Grammars... 前面的部份以寫 Spec 的方法寫,所以閱讀上會蠻吃力的。同時缺乏 tutorial,想要用的人沒有辦法在短時間內知道最基本的語法。 沒有 mailing…

July 16, 2007

In "Computer"

DuckDB

看到篇有趣的介紹,在講 DuckDB:「DuckDB」。 [I]t uses the PostgreSQL parser but models itself after SQLite in that databases are a single file and the code is designed for use as an embedded library, distributed in a single amalgamation C++ file (SQLite uses a C amalgamation). 看起來是個以 OLAP 為中心而設計出來的資料庫,然後在 Python 下可以直接透過 pip 裝起來。 看起來像是個用單機拼…

September 20, 2020

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on June 28, 2021June 28, 2021Categories Computer, Database, Library, Murmuring, Programming, SoftwareTags apache, database, db, duckdb, parquet, python, sql, sqlite

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK