Skip to content

Instantly share code, notes, and snippets.

@MrCroxx
Created March 25, 2024 05:50
Show Gist options
  • Select an option

  • Save MrCroxx/b911783d79e3214f589d14ea06ff3b85 to your computer and use it in GitHub Desktop.

Select an option

Save MrCroxx/b911783d79e3214f589d14ea06ff3b85 to your computer and use it in GitHub Desktop.
use std::{collections::HashMap, fs::File, path::Path};
use csv::StringRecord;
use itertools::Itertools;
fn open(path: impl AsRef<Path>) -> Vec<StringRecord> {
let f = File::open(path).unwrap();
let mut reader = csv::Reader::from_reader(f);
let headers = reader.headers().cloned().unwrap();
let records = reader.records().map(|res| res.unwrap()).collect_vec();
println!("{:?}", headers);
println!("records: {}", records.len());
println!("example: {:?}", records[0]);
records
}
fn main() {
// time, actor id, sequence, table id, bytes
let values = open("data/value.csv");
// time, actor id, sequence, table id, count
let counts = open("data/count.csv");
// hash join
let mut map = HashMap::new();
for record in values {
assert!(map
.insert(
(
record[0].to_owned(),
record[1].to_owned(),
record[2].to_owned(),
record[3].to_owned(),
),
vec![record[4].to_owned()],
)
.is_none());
}
for record in counts {
map.get_mut(&(
record[0].to_owned(),
record[1].to_owned(),
record[2].to_owned(),
record[3].to_owned(),
))
.inspect(|v| assert_eq!(v.len(), 1))
.unwrap()
.push(record[4].to_owned());
}
// sort
let mut records = map
.into_iter()
.map(|((time, actor, sequence, table), vc)| {
(table, actor, sequence, time, vc[0].clone(), vc[1].clone())
})
.collect_vec();
records.sort();
// write
let mut writer = csv::Writer::from_writer(File::create("data/output.csv").unwrap());
writer
.write_record(&["table", "actor", "sequence", "time", "bytes", "count"])
.unwrap();
for (table, actor, sequence, time, bytes, count) in records {
writer
.write_record(&[&table, &actor, &sequence, &time, &bytes, &count])
.unwrap();
}
writer.flush().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment