如何在 .net mvc 中使用 c# 创建和删除边缘属性(Titan 1.0)?

Posted

技术标签:

【中文标题】如何在 .net mvc 中使用 c# 创建和删除边缘属性(Titan 1.0)?【英文标题】:How to Create and Delete edge properties (Titan 1.0) using c# in .net mvc? 【发布时间】:2016-12-11 10:14:11 【问题描述】:

我使用 Titan 1.0 和 gremlin 服务器来创建和删除顶点。我想在我的 .net 项目中实现这个逻辑。我想知道asp.net中是否有任何用于titan和gremlin服务器的预构建插件?

目前我直接使用命令提示符来创建和删除所需的顶点和边。如何在我的 .net MVC 项目中实现它?

【问题讨论】:

现在我不知道 Gremlin/Titan 有任何直接的 c# 接口。但是,值得注意的是,您可以使用一个 rest api。更多信息here。这基本上允许您通过您选择的任何语言来提供 gremlin 遍历。不理想,但有些东西。 Titan 1.0/TinkerPop 3.0.x 应该有这个版本 - nuget.org/packages/Teva.Common.Data.Gremlin 【参考方案1】:

我在我的项目中创建了一个类,用于使用 REST API 与 Gremlin 服务器交互。您可以进行一些小的更改以使其适合您。

来源:https://askgif.com/blog/145/how-to-create-and-delete-edge-properties-titan-1-0-using-c-in-net-mvc/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using RestSharp;
using urNotice.Common.Infrastructure.Common.Config;
using urNotice.Common.Infrastructure.Common.Constants;
using urNotice.Common.Infrastructure.Common.Enum;
using urNotice.Common.Infrastructure.commonMethods;
using urNotice.Common.Infrastructure.Model.urNoticeModel.DynamoDb;
using urNotice.Services.NoSqlDb.DynamoDb;

namespace urNotice.Services.GraphDb

    public class GremlinServerGraphEdgeDb : IGraphEdgeDb
    
        private delegate Dictionary<string, string> AddEdgeAsyncDelegate(string userName, string graphName, Dictionary<string, string> properties);

    public Dictionary<string, string> AddEdge(string userName, string graphName, Dictionary<string, string> properties)
    
        string url = TitanGraphConfig.Server;
        var response = CreateEdge(graphName, properties, url);

        // add edge to dynamodb.
        var edgeDetail = new OrbitPageEdgeDetail
        
            url = url,
            edgeId = response[TitanGraphConstants.Id],
            graphName = graphName,
            properties = properties
        ;

        IDynamoDb dynamoDbModel = new DynamoDb();
        dynamoDbModel.UpsertOrbitPageEdgeDetail(edgeDetail, userName, properties[EdgePropertyEnum._inV.ToString()], properties[EdgePropertyEnum._outV.ToString()]);

        //Adding edgeDetail for faster query.
        //dynamoDbModel.UpsertOrbitPageEdgeForQueryDetail(edgeDetail, userName, properties[EdgePropertyEnum._inV.ToString()], properties[EdgePropertyEnum._outV.ToString()]);

        return response;
    

    public Dictionary<string, string> DeleteEdge(string inV, string outV, string label)
    
        string url = TitanGraphConfig.Server;

        IDynamoDb dynamoDbModel = new DynamoDb();
        string uniqueKey = OrbitPageUtil.GenerateUniqueKeyForEdgeQuery(inV, label, outV);
        var edgeInfo = dynamoDbModel.GetOrbitPageCompanyUserWorkgraphyTable(
                   DynamoDbHashKeyDataType.EdgeDetail.ToString(),
                   uniqueKey,
                   null);

        if (edgeInfo == null)
            return null;

        var response = DeleteEdgeNative(TitanGraphConfig.Graph, edgeInfo.CompareId, url);
        dynamoDbModel.DeleteOrbitPageCompanyUserWorkgraphyTable(edgeInfo);

        //Deleting Edge detail creating for only query purpose.
        //string uniqueKey = OrbitPageUtil.GenerateUniqueKeyForEdgeQuery(inV, label, outV);
        //edgeInfo = dynamoDbModel.GetOrbitPageCompanyUserWorkgraphyTable(
        //            DynamoDbHashKeyDataType.EdgeDetail.ToString(),
        //            uniqueKey,
        //            null);

        //if(edgeInfo!=null)
        //    dynamoDbModel.DeleteOrbitPageCompanyUserWorkgraphyTable(edgeInfo);

        return response;
    

    public Dictionary<string, string> AddEdgeAsync(string userName, string graphName, Dictionary<string, string> properties)
    
        var addEdgeAsyncDelegate = new GremlinServerGraphEdgeDb.AddEdgeAsyncDelegate(AddEdge);
        addEdgeAsyncDelegate.BeginInvoke(userName, graphName, properties, null, null);
        return null;
    

    private Dictionary<String, String> CreateEdge(string graphName, Dictionary<string, string> properties, string url)
    
        var uri = new StringBuilder(url + "/?gremlin=");

        //http://localhost:8182/?gremlin=g.V(8320).next().addEdge("Using",g.V(12416).next(),"Desc","Item used by Person","time",12345)

        string graphproperties = string.Empty;

        //_outV must be the first parameter
        graphProperties += "'" + properties[EdgePropertyEnum._label.ToString()] + "', g.V(" + properties[EdgePropertyEnum._inV.ToString()] + ").next() ,";
        foreach (KeyValuePair<string, string> property in properties)
        
            if (property.Key == EdgePropertyEnum._inV.ToString() || property.Key == EdgePropertyEnum._outV.ToString() || property.Key == EdgePropertyEnum._label.ToString())
            
                //do nothing.. May be in future we will write logic here.                    
            
            else
            
                if (property.Key == EdgePropertyEnum.PostedDateLong.ToString() || property.Key == EdgePropertyEnum.SalaryAmount.ToString())
                    graphProperties += "'" + property.Key + "', " + property.Value + " ,";
                else
                    graphProperties += "'" + property.Key + "', '" + property.Value + "' ,";
            
        

        if (!string.IsNullOrEmpty(graphProperties))
        
            graphProperties = graphProperties.Substring(0, graphProperties.Length - 2);
        

        uri.Append("g.V(" + properties[EdgePropertyEnum._outV.ToString()] + ").next().addEdge(" + graphProperties + ");");
        var client = new RestClient(uri.ToString());
        var request = new RestRequest();

        request.Method = Method.GET;
        request.AddHeader("Accept", "application/json");
        request.Parameters.Clear();
        request.AddParameter("application/json", "", ParameterType.RequestBody);

        var res = client.Execute(request);
        var content = res.Content; // raw content as string 

        dynamic jsonResponse = JsonConvert.DeserializeObject(content);
        var response = new Dictionary<String, String>();
        response["status"] = "200";
        response["CreateEdgeStatus"] = "200";
        response[TitanGraphConstants.Id] = jsonResponse.result.data[0].id;
        response[TitanGraphConstants.RexsterUri] = url;
        return response;
    

    private Dictionary<String, String> DeleteEdgeNative(string graphName, string edgeId, string url)
    
        var uri = new StringBuilder(url + "/?gremlin=");
        //var uri = new StringBuilder(url + "/graphs/" + graphName + "/edges/" + edgeId);

        //http://localhost:8182/?gremlin=g.E('odxqo-6f4-2hat-9kw').drop()

        uri.Append("g.E('" + edgeId + "').drop();");

        var client = new RestClient(uri.ToString());
        var request = new RestRequest();

        request.Method = Method.GET;
        request.AddHeader("Accept", "application/json");
        request.Parameters.Clear();
        request.AddParameter("application/json", "", ParameterType.RequestBody);

        var res = client.Execute(request);
        var content = res.Content; // raw content as string 

        dynamic jsonResponse = JsonConvert.DeserializeObject(content);
        var response = new Dictionary<String, String>();
        response["status"] = "200";
        response["DeleteEdgeStatus"] = "200";
        //response[TitanGraphConstants.Id] = jsonResponse.result.data[0].id;
        //response[TitanGraphConstants.RexsterUri] = url;
        return response;
    


如果您在课堂上遇到任何问题,请发表评论。

【讨论】:

以上是关于如何在 .net mvc 中使用 c# 创建和删除边缘属性(Titan 1.0)?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 ASP.net MVC 程序中获取纯 c# 程序中的 JSON 数据?

如何使用 C# 在 ASP.NET Core 3.1 MVC 中使用会话变量

如何在 C# 页面 ASP.NET Core MVC 上使用 SignInManager 和 Usermanager

如何使用动态对象类创建 .net 核心 MVC 模型?

如何在 C# 和 ASP.NET MVC 中读取/写入 cookie

如何在.net mvc 3中使用c#检查网络中文件的存在