Created
March 12, 2023 01:10
-
-
Save vimota/23f40bca01593a3731c37779dc00cb9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import duckdb from 'duckdb'; | |
import fs from 'fs'; | |
import readline from 'readline'; | |
// Check if the file exists | |
const path = '/tmp/localpy.duckdb'; | |
const fileExists = fs.existsSync(path); | |
// Create a new database object | |
const db = new duckdb.Database(path); | |
function sequentialQueries() { | |
const line = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
line.question('Press Enter to continue...', () => { | |
line.close(); | |
db.all(` | |
with "inner" as ( | |
select r.follower as "id" | |
from "taxi_relationships" as "r" | |
where "r"."VendorID" in ('1', '2') | |
group by "id" | |
) | |
select * | |
from "inner" | |
inner join "taxi" as "u" on "inner"."id" = "u"."VendorId" | |
order by u.total_amount desc limit 30 | |
`, function (err, res) { | |
if (err) { | |
throw err; | |
} | |
console.log(res); | |
console.log('Query 1 executed'); | |
sequentialQueries(); | |
}); | |
}); | |
} | |
// Install httpfs | |
db.all('install httpfs', function (err, res) { | |
if (err) { | |
throw err; | |
} | |
// Create tables and insert data if the file doesn't exist | |
if (!fileExists) { | |
db.all(` | |
create table taxi as | |
select *, md5(total_amount), md5(tpep_pickup_datetime), md5(tpep_dropoff_datetime) | |
from parquet_scan('https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2022-01.parquet') | |
`, function (err, res) { | |
if (err) { | |
throw err; | |
} | |
db.all(` | |
insert into taxi | |
select *, md5(total_amount), md5(tpep_pickup_datetime), md5(tpep_dropoff_datetime) | |
from parquet_scan('https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2022-01.parquet') | |
`, function (err, res) { | |
if (err) { | |
throw err; | |
} | |
db.all(` | |
create table taxi_relationships as | |
select vendorID, vendorID + 1 as follower | |
from taxi | |
`, function (err, res) { | |
if (err) { | |
throw err; | |
} | |
// Query the database | |
sequentialQueries(); | |
}); | |
}); | |
}); | |
} else { | |
// Query the database | |
sequentialQueries(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment