Public
Edited
Aug 3, 2023
5 forks
Importers
47 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
db
SELECT * FROM csv_file
LIMIT 10
Insert cell
data
Insert cell
Insert cell
Insert cell
Insert cell
db = {
const client = new DuckDBClient();
if (!file) {
// create a table called `dt` with three columns
await client.query(
`CREATE TABLE csv_file(Choose STRING, a STRING, file STRING)`
);
return client;
}
const buffer = await file.arrayBuffer();
await client.insertCSV("csv_file", buffer, {
// dateFormat: "%Y-%m-%d",
// timestampFormat: "%Y-%m-%dT%H:%M:%SZ",
});
await client.query(
`EXPORT DATABASE '/tmp/duckdbexportparquet' (FORMAT 'parquet', COMPRESSION 'GZIP')`
);
const db = await client.db();
const files = await db.globFiles("/tmp/duckdbexportparquet/*");
return client;
}
Insert cell
buffer = (await db.db()).copyFileToBuffer(`/tmp/duckdbexportparquet/csv_file.parquet`)
Insert cell
Insert cell
button = (data, filename = 'data.csv') => {
if (!data) throw new Error('Array of data required as first argument');

let downloadData;
if (filename.includes('.csv')) {
downloadData = new Blob([d3.csvFormat(data)], { type: "text/csv" });
} else if (filename.includes('.json')) {
downloadData = new Blob([JSON.stringify(data, null, 2)], {
type: "application/json"
});
} else {
// save as binary
downloadData = new Blob([data], { type: "application/octet-stream" });
}

const size = (downloadData.size / 1024).toFixed(0);
const button = DOM.download(
downloadData,
filename,
`Download ${filename} (~${size} KB)`
);
return button;
}
Insert cell
// this allows you to export results of a SQL query by directly copying it
// TODO: figure out how to let this cell run more than once (File is not opened in write mode error)
modifiedParquet = {
let modifySQL = `
SELECT * EXCLUDE (comments) FROM csv_file
`
await db.query(`COPY (${modifySQL}) TO 'modified.parquet' (FORMAT 'parquet', CODEC 'GZIP')`)
let idb = await db.db()
return idb.copyFileToBuffer(`modified.parquet`)
}
Insert cell
file ? button(modifiedParquet, `modified.parquet`) : md``
Insert cell
// this allows you to export results of a SQL query by directly copying it
// TODO: figure out how to let this cell run more than once (File is not opened in write mode error)
// modifiedCSV = {
// let modifySQL = `
// SELECT * EXCLUDE (comments) FROM csv_file
// `
// await db.query(`COPY (${modifySQL}) TO 'modified.csv' (HEADER, DELIMITER ',')`)
// let idb = await db.db()
// return idb.copyFileToBuffer(`modified.parquet`)
// }
Insert cell
import {DuckDBClient} from "@cmudig/duckdb"
Insert cell
// Load apache arrow for setting types
arrow = require('apache-arrow@8.0.0')
Insert cell
{
// update loading indicator when we are done processing the CSV
if(data.length) {
status.innerText = "Done Loading"
}
return
}
Insert cell
data1 = FileAttachment("data1.parquet")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more