Module raphtory_io::graph_loader::source::csv_loader
source · Expand description
Module containing functions for loading CSV files into a graph.
Example
use std::path::{Path, PathBuf};
use regex::Regex;
use raphtory::core::Prop;
use raphtory::core::utils::calculate_hash;
use raphtory_io::graph_loader::source::csv_loader::CsvLoader;
use raphtory::db::graph::Graph;
use raphtory_io::graph_loader::example::lotr_graph::Lotr;
let g = Graph::new(2);
let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../../resource/"]
.iter()
.collect();
println!("path = {}", csv_path.as_path().to_str().unwrap());
let csv_loader = CsvLoader::new(Path::new(&csv_path));
let has_header = true;
let r = Regex::new(r".+(lotr.csv)").unwrap();
let delimiter = ",";
csv_loader
.set_header(has_header)
.set_delimiter(delimiter)
.with_filter(r)
.load_into_graph(&g, |lotr: Lotr, g: &Graph| {
let src_id = calculate_hash(&lotr.src_id);
let dst_id = calculate_hash(&lotr.dst_id);
let time = lotr.time;
g.add_vertex(
time,
src_id,
&vec![("name".to_string(), Prop::Str("Character".to_string()))],
)
.map_err(|err| println!("{:?}", err))
.ok();
g.add_vertex(
time,
dst_id,
&vec![("name".to_string(), Prop::Str("Character".to_string()))],
)
.map_err(|err| println!("{:?}", err))
.ok();
g.add_edge(
time,
src_id,
dst_id,
&vec![(
"name".to_string(),
Prop::Str("Character Co-occurrence".to_string()),
)],
None,
).expect("Failed to add edge");
})
.expect("Csv did not parse.");Structs
- A struct that defines the CSV loader with configurable options.