Working with Parquet Files
Parquet is a columnar file format built for analytics. flAPI reads it with DuckDB's built-in Parquet reader: no extension to install, no database server to run. The same pattern works for CSV (read_csv) and JSON (read_json).
Setup
Put the file path in the connection's properties:
project-name: parquet-api
template:
path: ./sqls
connections:
customers-parquet:
properties:
path: ./data/customers.parquet
A property that starts with ./ or ../ is made absolute against the directory of flapi.yaml, so the server finds the file whatever its working directory. The sample file is in the flAPI repository:
mkdir -p data sqls
curl -L -o data/customers.parquet https://github.com/DataZooDE/flapi/raw/main/examples/data/customers.parquet
A complete endpoint
url-path: /customers
request:
- field-name: id
field-in: query
required: false
validators:
- type: int
min: 1
- field-name: segment
field-in: query
required: false
validators:
- type: enum
allowedValues: [AUTOMOBILE, BUILDING, FURNITURE, HOUSEHOLD, MACHINERY]
template-source: customers.sql
connection: [customers-parquet]
SELECT
c_custkey AS id,
c_name AS name,
c_mktsegment AS segment,
c_acctbal AS balance
FROM read_parquet('{{{ conn.path }}}')
WHERE ({{ params.id }} IS NULL OR c_custkey = {{ params.id }})
{{#params.segment}}
AND c_mktsegment = '{{{ params.segment }}}'
{{/params.segment}}
ORDER BY c_acctbal DESC
{{{ conn.path }}}uses triple braces. Double braces HTML-escape the value, and/would become/.idis outside any section, so it is bound as a prepared-statement parameter; absent, it bindsNULLand the filter switches off.segmentis inside a section, so it is pasted as text, which theenumvalidator makes safe.- No
LIMIT: GET endpoints are paginated by default, and?limit=/?offset=are reserved for the pagination.
flapi -c flapi.yaml
curl 'http://localhost:8080/customers?segment=AUTOMOBILE&limit=5'
curl 'http://localhost:8080/customers?id=7'
Many files: globs
A glob reads every matching file as one table:
connections:
events:
properties:
path: ./data/events/*.parquet # one directory
all-events:
properties:
path: ./data/events/**/*.parquet # every subdirectory
filename = true adds a column naming the file each row came from, which is handy when the file name carries the month or the source system:
SELECT *, filename
FROM read_parquet('{{{ conn.path }}}', filename = true)
Hive-partitioned data
connections:
partitioned:
properties:
path: ./data/events/year=*/month=*/*.parquet
SELECT event_id, event_type, year, month
FROM read_parquet('{{{ conn.path }}}', hive_partitioning = true)
WHERE year = {{ params.year }}
DuckDB turns the directory names into columns, and a filter on them skips whole directories. Give year an int validator.
Files in object storage or behind HTTPS
httpfs is in flAPI's default extension set, so no init is needed to read s3://, gs://, az:// or https:// paths. What you do need is credentials, usually from the environment or a DuckDB secret:
connections:
lake:
properties:
path: s3://my-bucket/data/*.parquet
The credential setup per cloud, and loading flapi.yaml itself from a bucket, are in Cloud Storage. For wide globs over a bucket, consider a cache: each live request lists and reads the files again.
Why Parquet?
- Fast. Columnar storage means DuckDB reads only the columns you select, and row-group statistics let it skip data that a filter excludes.
- Portable. No database server; a file on disk, in a bucket or behind HTTPS.
- Cheap. No hosting or per-query costs, which makes it a good target for exporting data from a billed warehouse.
Best practices
- Select the columns you serve.
SELECT id, namereads two columns;SELECT *reads all of them. - Filter on sorted or partitioned columns. DuckDB pushes the filter into the Parquet reader and skips row groups and directories.
- Use
{{{ conn.path }}}, never{{ conn.path }}. - Keep files read-only while serving. Replace a file atomically (write a new file, then rename) rather than rewriting it in place.
Next steps
- Quickstart Guide: complete tutorial with Parquet
- Parquet API example: full working example
- Connections Overview: SQLite, several connections, live vs cache
- BigQuery Connection: graduate to a cloud warehouse