mirror of
https://github.com/NexVeridian/wikidata-to-surrealdb.git
synced 2025-09-02 01:49:13 +00:00
mulit line json
This commit is contained in:
parent
b838a5c326
commit
f0716081fe
3 changed files with 27 additions and 19 deletions
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
|
@ -1,4 +1,10 @@
|
|||
{
|
||||
"json.format.enable": false,
|
||||
"[json]": {
|
||||
"editor.wordWrap": "off",
|
||||
"editor.formatOnSave": false,
|
||||
"editor.formatOnPaste": false,
|
||||
},
|
||||
"[Rust]": {
|
||||
"editor.defaultFormatter": "rust-lang.rust-analyzer",
|
||||
"editor.formatOnSave": true,
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -1,18 +1,18 @@
|
|||
use anyhow::Ok;
|
||||
use anyhow::{Error, Result};
|
||||
use dotenv_codegen::dotenv;
|
||||
use serde_json::{from_str, Value};
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use surrealdb::engine::remote::ws::Ws;
|
||||
use surrealdb::opt::auth::Root;
|
||||
use surrealdb::Surreal;
|
||||
|
||||
mod utils;
|
||||
use utils::*;
|
||||
use wikidata::Entity;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
let data = get_entity("data/e.json").await?;
|
||||
let (id, data) = EntityMini::from_entity(data);
|
||||
|
||||
let db = Surreal::new::<Ws>("0.0.0.0:8000").await?;
|
||||
|
||||
db.signin(Root {
|
||||
|
@ -23,9 +23,23 @@ async fn main() -> Result<(), Error> {
|
|||
|
||||
db.use_ns("wikidata").use_db("wikidata").await?;
|
||||
|
||||
let _: Option<EntityMini> = db.delete(&id).await?;
|
||||
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
|
||||
let file = File::open("data/w.json")?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line?.trim().trim_end_matches(',').to_string();
|
||||
if line == "[" || line == "]" {
|
||||
continue;
|
||||
}
|
||||
|
||||
let json: Value = from_str(&line)?;
|
||||
let data = Entity::from_json(json).expect("Failed to parse JSON");
|
||||
|
||||
let (id, data) = EntityMini::from_entity(data);
|
||||
|
||||
let _: Option<EntityMini> = db.delete(&id).await?;
|
||||
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
|
||||
}
|
||||
|
||||
// println!("{:#?}", data);
|
||||
Ok(())
|
||||
}
|
||||
|
|
12
src/utils.rs
12
src/utils.rs
|
@ -1,20 +1,8 @@
|
|||
use anyhow::{Error, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::from_reader;
|
||||
use serde_json::Value;
|
||||
use std::fs::File;
|
||||
use surrealdb::sql::Thing;
|
||||
use wikidata::ClaimValueData;
|
||||
use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId};
|
||||
|
||||
pub async fn get_entity(path: &str) -> Result<Entity, Error> {
|
||||
// From here - https://www.wikidata.org/wiki/Special:EntityData/P1476.json
|
||||
let mut file = File::open(path)?;
|
||||
let json: Value = from_reader(&mut file)?;
|
||||
let data = Entity::from_json(json).expect("Failed to parse JSON");
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct EntityMini {
|
||||
// In English
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue