28次发布

0.8.10 2024年5月13日
0.8.6 2023年12月11日
0.8.5 2023年8月3日
0.8.4 2023年5月20日
0.1.1 2019年3月26日

#170 in 数据库接口

Download history 57/week @ 2024-05-03 208/week @ 2024-05-10 66/week @ 2024-05-17 17/week @ 2024-05-24 30/week @ 2024-05-31 33/week @ 2024-06-07 25/week @ 2024-06-14 22/week @ 2024-06-21 102/week @ 2024-06-28 1/week @ 2024-07-05 4/week @ 2024-07-12 20/week @ 2024-07-19 32/week @ 2024-07-26 27/week @ 2024-08-02 15/week @ 2024-08-09 299/week @ 2024-08-16

每月376次下载
用于 2 crates

Apache-2.0

285KB
8K SLoC

gremlin-client

Apache TinkerPop™的Rust客户端。

安装

crates.io 安装

[dependencies]
gremlin-client = "0.8"

通过 async-std 支持

[dependencies]
gremlin-client = { version = "0.8", features = ["async_std"] }

示例

基本用法

使用id执行简单的Gremlin查询并收集结果

同步

use gremlin_client::{GremlinClient, Vertex};

fn main() -> Result<(), Box<std::error::Error>> {
    let client = GremlinClient::connect("localhost")?;

    let results = client
        .execute("g.V(param)", &[("param", &1)])?
        .filter_map(Result::ok)
        .map(|f| f.take::<Vertex>())
        .collect::<Result<Vec<Vertex>, _>>()?;

    println!("{:?}", results);

    Ok(())
}

异步

使用 async-std

激活特性 async-std-runtime

gremlin-client= {version= "*",features= ["async-std-runtime"] }

     
use gremlin_client::{aio::GremlinClient, Vertex};
use async_std::prelude::*;

#[async_std::main]
async fn main() -> Result<(), Box<std::error::Error>> {

    let client = GremlinClient::connect("localhost").await?;
    let results = client.execute("g.V(param)", &[("param", &1)]).await?
        .filter_map(Result::ok)
        .map(|f| f.take::<Vertex>())
        .collect::<Result<Vec<Vertex>, _>>().await?;
    println!("{:?}", results);
    Ok(())
    
}

使用 tokio

激活特性 tokio-runtime

gremlin-client= {version= "*",features= ["tokio-runtime"] }

     
use gremlin_client::{aio::GremlinClient, Vertex};
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<std::error::Error>> {

    let client = GremlinClient::connect("localhost").await?;
    let results = client.execute("g.V(param)", &[("param", &1)]).await?
        .filter_map(Result::ok)
        .map(|f| f.take::<Vertex>())
        .collect::<Result<Vec<Vertex>, _>>().await?;
    println!("{:?}", results);
    Ok(())
    
}

Traversal示例 Rust GLV

使用提供的 GremlinClient 创建远程Traversal,并使用Rust语言构建Traversal。

同步

 use gremlin_client::{GremlinClient, Vertex, process::traversal::traversal};

 fn main() -> Result<(), Box<std::error::Error>> {
    let client = GremlinClient::connect("localhost")?;

    let g = traversal().with_remote(client);

    let results = g.v(()).has_label("person").has(("name","Jon")).to_list()?;   
    
    println!("{:?}", results);
    Ok(())
}

异步

使用 async-std

use gremlin_client::{aio::GremlinClient, Vertex, process::traversal::traversal};
use async_std::prelude::*;

#[async_std::main]
async fn main() -> Result<(), Box<std::error::Error>> {

    
    let client = GremlinClient::connect("localhost").await?;

    let g = traversal().with_remote_async(client);

    let results = g.v(()).has_label("person").has(("name","Jon")).to_list().await?;   

    println!("{:?}", results);
    Ok(())
    
}

使用 tokio

use gremlin_client::{aio::GremlinClient, Vertex, process::traversal::traversal};
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<std::error::Error>> {

    let client = GremlinClient::connect("localhost").await?;

    let g = traversal().with_remote_async(client);

    let results = g.v(()).has_label("person").has(("name","Jon")).to_list().await?;   

    println!("{:?}", results);
    Ok(())
}

附加功能

derive 特性

在Cargo.toml中包含 derive 特性

[dependencies]
gremlin_client = { version = "*", features = ["derive"] }

提供了两个derive宏

  • FromGMap
  • FromGValue

您可以使用它们将GMap和GValue(目前只有Map)映射到结构体中。

使用 GValue

use gremlin_client::derive::{FromGMap, FromGValue};
use gremlin_client::process::traversal::traversal;
use gremlin_client::GremlinClient;
use std::convert::TryFrom;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = GremlinClient::connect("localhost")?;

    #[derive(Debug, PartialEq, FromGValue, FromGMap)]
    struct Person {
        name: String,
    }

    let results = client
        .execute("g.V(param).valueMap()", &[("param", &1)])?
        .filter_map(Result::ok)
        .map(|f| Person::try_from(f))
        .collect::<Result<Vec<Person>, _>>()?;

    println!("Person {:?}", results[0);
    Ok(())
}

使用 GMap

use gremlin_client::derive::FromGMap;
use gremlin_client::process::traversal::traversal;
use gremlin_client::GremlinClient;
use std::convert::TryFrom;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = GremlinClient::connect("localhost")?;

    #[derive(Debug, PartialEq, FromGMap)]
    struct Person {
        name: String,
    }

    let g = traversal().with_remote(client);

    let results = g
        .v(1)
        .value_map(())
        .iter()?
        .filter_map(Result::ok)
        .map(Person::try_from)
        .collect::<Result<Vec<Person>, _>>()?;

    println!("Person {:?}", results[0);

    Ok(())
}

依赖项

~3–18MB
~277K SLoC