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]": {
|
"[Rust]": {
|
||||||
"editor.defaultFormatter": "rust-lang.rust-analyzer",
|
"editor.defaultFormatter": "rust-lang.rust-analyzer",
|
||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -1,18 +1,18 @@
|
||||||
use anyhow::Ok;
|
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
use dotenv_codegen::dotenv;
|
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::engine::remote::ws::Ws;
|
||||||
use surrealdb::opt::auth::Root;
|
use surrealdb::opt::auth::Root;
|
||||||
use surrealdb::Surreal;
|
use surrealdb::Surreal;
|
||||||
|
|
||||||
mod utils;
|
mod utils;
|
||||||
use utils::*;
|
use utils::*;
|
||||||
|
use wikidata::Entity;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Error> {
|
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?;
|
let db = Surreal::new::<Ws>("0.0.0.0:8000").await?;
|
||||||
|
|
||||||
db.signin(Root {
|
db.signin(Root {
|
||||||
|
@ -23,9 +23,23 @@ async fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
db.use_ns("wikidata").use_db("wikidata").await?;
|
db.use_ns("wikidata").use_db("wikidata").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.delete(&id).await?;
|
||||||
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
|
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
// println!("{:#?}", data);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
12
src/utils.rs
12
src/utils.rs
|
@ -1,20 +1,8 @@
|
||||||
use anyhow::{Error, Result};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::from_reader;
|
|
||||||
use serde_json::Value;
|
|
||||||
use std::fs::File;
|
|
||||||
use surrealdb::sql::Thing;
|
use surrealdb::sql::Thing;
|
||||||
use wikidata::ClaimValueData;
|
use wikidata::ClaimValueData;
|
||||||
use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId};
|
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)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct EntityMini {
|
pub struct EntityMini {
|
||||||
// In English
|
// In English
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue