.Net
本文介绍如何基于.NET
编程环境连接和操作图数据库GDB。
进行以下操作时,请确保您的图数据库GDB实例与您的ECS虚拟机处于同一个VPC网络环境。
环境准备
1、请在运行以下范例之前
- 请安装版本在2.0以上的dotnet环境
- 如果使用windows开发环境,请使用
windows8
以上的OS版本
2、执行.Net命令生成console工程gremlinTest
dotnet new console -o gremlinTest
3、进入该工程目录
cd gremlinTest
4、为您的程序工程来安装Gremlin的SDK依赖Gremlin.NET
dotnet add package gremlin.net
5、编辑Program.cs文件
将内容替换如下,需要将配置信息替换成您的图数据库实例的相关信息:
- 将
${your-gdb-endpoint}
改为您的图数据库GDB实例的域名 - 将
${username}
改为您的图数据库GDB实例的用户名 - 将
${password}
改为您的图数据库GDB实例的密码
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Gremlin.Net;
using Gremlin.Net.Driver;
namespace gremlinTest
{
class Program
{
private static string endpoint = "${your_gdb_endpoint}";
private static int port = 8182;
private static string username = "${username}";
private static string password = "${password}";
static void Main(string[] args)
{
try
{
var gremlinServer = new GremlinServer(endpoint, port, username: username, password: password);
var gremlinClient = new GremlinClient(gremlinServer);
Program program = new Program();
program.RunQueryAsync(gremlinClient).Wait();
}
catch (Exception e)
{
Console.WriteLine("{0}", e);
}
}
private async Task RunQueryAsync(GremlinClient gremlinClient)
{
var vertices = await gremlinClient.SubmitWithSingleResultAsync<dynamic>("g.V().limit(1)");
Console.WriteLine("{0}", vertices);
}
}
}
连接并执行
- 运行命令执行程序,输出结果的范例如下
v[3a63cc90-d957-4324-9ffc-16a8e4c1c1f4]
- 上面的例子是使用
g.V().limit(1)
遍历返回GDB中的一个点。要查询其他内容,可以替换成其他的DSL - 其他dsl范例如下,图的点、边结构链接 http://tinkerpop.apache.org/docs/current/reference/#traversal
删除指定label的点、边
g.E().hasLabel('gdb_sample_knows').drop()
g.E().hasLabel('gdb_sample_created').drop()
g.V().hasLabel('gdb_sample_person').drop()
g.V().hasLabel('gdb_sample_software').drop()
添加顶点,为其设置id、property
g.addV('gdb_sample_person').property(id, 'gdb_sample_marko').property('age', 28).property('name', 'marko')
g.addV('gdb_sample_person').property(id, 'gdb_sample_vadas').property('age', 27).property('name', 'vadas')
g.addV('gdb_sample_person').property(id, 'gdb_sample_josh').property('age', 32).property('name', 'josh')
g.addV('gdb_sample_person').property(id, 'gdb_sample_peter').property('age', 35).property('name', 'peter')
g.addV('gdb_sample_software').property(id, 'gdb_sample_lop').property('lang', 'java').property('name', 'lop')
g.addV('gdb_sample_software').property(id, 'gdb_sample_ripple').property('lang', 'java').property('name', 'ripple')
修改(或新增) age 属性
g.V('gdb_sample_marko').property('age', 29)
建立关系,设置属性 weight
g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_vadas')).property('weight', 0.5f)
g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_josh')).property('weight', 1.0f)
g.addE('gdb_sample_created').from(V('gdb_sample_marko')).to(V('gdb_sample_lop')).property('weight', 0.4f)
g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_lop')).property('weight', 0.4f)
g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_ripple')).property('weight', 1.0f)
g.addE('gdb_sample_created').from(V('gdb_sample_peter')).to(V('gdb_sample_lop')).property('weight', 0.2f)
查询所有点/指定label的点数量
g.V().count()
g.V().hasLabel('gdb_sample_person').count()
查询指定条件的顶点 (>29岁的人, 按name降序排列所有人)
g.V().hasLabel('gdb_sample_person').has('age', gt(29))
g.V().hasLabel('gdb_sample_person').order().by('name', decr)
关联查询(获取 marko 认识的人, marko认识的人created的software)
g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person')
g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person').outE('gdb_sample_created').inV().hasLabel('gdb_sample_software')
删除关系、顶点
g.V('gdb_sample_marko').outE('gdb_sample_knows').where(inV().has(id, 'gdb_sample_josh')).drop()
g.V('gdb_sample_marko').drop()
有关Gremlin.Net使用方式的更多信息,请参阅Apache TinkerPop3文档中的Gremlin.Net。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论