如何使用C#在gremlin服务器中创建,更新和删除边和顶点?

时间:2016-08-06 13:52:39

标签: graph graph-databases titan gremlin gremlin-server

我正在使用带有Razor的.net mvc 4.5,而我的前端是角度js。我正在使用Aws ec2服务器用于我的Titan 1.0并使用gremlin服务器通过REST API与它进行交互。 我尝试使用Rest api调用服务器,但它总是会出现语法错误。 为gremlin服务器边和顶点编写CRUD操作的正确方法应该是什么?

1 个答案:

答案 0 :(得分:1)

要使用gremlin服务器更新边缘,您可以检查以下问题线程

How to Create and Delete edge properties (Titan 1.0) using c# in .net mvc?

并且在gremlin服务器中创建,更新和删除顶点时,可以在更改几个参数后使用以下类。

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.Model.urNoticeModel.DynamoDb;
using urNotice.Services.NoSqlDb.DynamoDb;

namespace urNotice.Services.GraphDb
{
public class GremlinServerGraphVertexDb : IGraphVertexDb
{
    public Dictionary<string, string> AddVertex(string email, string graphName, Dictionary<string, string> properties, HashSet<string> canEdit, HashSet<string> canDelete,
        HashSet<string> sendNotificationToUsers)
    {
        string url = TitanGraphConfig.Server;
        var response = CreateVertex(graphName, properties, url);
        if (!response.ContainsKey(TitanGraphConstants.Id))
            return null;
        var orbitPageVertexDetail = new OrbitPageVertexDetail
        {
            url = url,
            vertexId = response[TitanGraphConstants.Id],
            graphName = graphName,
            properties = properties
        };

        IDynamoDb dynamoDbModel = new DynamoDb();
        dynamoDbModel.UpsertOrbitPageVertexDetail(orbitPageVertexDetail, email, canEdit, canDelete, sendNotificationToUsers);

        return response;
    }

    public Dictionary<string, string> UpdateVertex(string vertexId, string email, string graphName, Dictionary<string, string> properties)
    {
        string url = TitanGraphConfig.Server;
        var orbitPageCompanyUserWorkgraphyTable = GetVertexDetailsFromDynamoDb(vertexId);

        var response = UpdateGraphVertex(vertexId, graphName, properties, url, orbitPageCompanyUserWorkgraphyTable);
        var orbitPageVertexDetail = new OrbitPageVertexDetail
        {
            url = url,
            vertexId = vertexId,
            graphName = graphName,
            properties = response
        };

        orbitPageCompanyUserWorkgraphyTable.OrbitPageVertexDetail = orbitPageVertexDetail;
        IDynamoDb dynamoDbModel = new DynamoDb();
        dynamoDbModel.CreateOrUpdateOrbitPageCompanyUserWorkgraphyTable(orbitPageCompanyUserWorkgraphyTable);

        return response;
    }

    private Dictionary<String, String> UpdateGraphVertex(string vertexId, string graphName, Dictionary<string, string> properties, string url, OrbitPageCompanyUserWorkgraphyTable orbitPageCompanyUserWorkgraphyTable)
    {
        var uri = new StringBuilder("/?gremlin=");

        var oldProperties = new Dictionary<string, string>();

        if (orbitPageCompanyUserWorkgraphyTable != null && orbitPageCompanyUserWorkgraphyTable.OrbitPageVertexDetail != null)
            oldProperties = orbitPageCompanyUserWorkgraphyTable.OrbitPageVertexDetail.properties;

        var newProperties = new Dictionary<String, String>();

        if (oldProperties == null)
        {
            return null;
        }

        foreach (KeyValuePair<string, string> property in oldProperties)
        {
            newProperties[property.Key] = property.Value;
        }
        foreach (KeyValuePair<string, string> property in oldProperties)
        {
            //merge updated values in dictionary..
            if (properties.ContainsKey(property.Key))
            {
                newProperties[property.Key] = properties[property.Key];
            }
        }

        //new properties that is not available in old properties
        foreach (KeyValuePair<string, string> property in properties)
        {
            //merge updated values in dictionary..
            if (!newProperties.ContainsKey(property.Key))
            {
                newProperties[property.Key] = properties[property.Key];
            }
        }

        //if user want to delete previous uploaded image.
        if (oldProperties.ContainsKey(VertexPropertyEnum.CoverImageUrl.ToString()) &&
            !properties.ContainsKey(VertexPropertyEnum.CoverImageUrl.ToString()))
        {
            newProperties.Remove(VertexPropertyEnum.CoverImageUrl.ToString());
        }

        if (newProperties.ContainsKey(VertexPropertyEnum.CoverImageUrl.ToString()) && string.IsNullOrEmpty(newProperties[VertexPropertyEnum.CoverImageUrl.ToString()]))
            newProperties[VertexPropertyEnum.CoverImageUrl.ToString()] = CommonConstants.CompanySquareLogoNotAvailableImage;

        string graphProperties = string.Empty;

        //g.V(4192).property('name','William2').property('age',30)
        foreach (KeyValuePair<string, string> property in properties)
        {                
            if (property.Key == VertexPropertyEnum.CreatedTimeLong.ToString() || property.Key == VertexPropertyEnum.PostedTimeLong.ToString())
                graphProperties += ".property('" + property.Key + "'," + property.Value + ")";
            else
                graphProperties += ".property('" + property.Key + "','" + property.Value + "')";                
        }

        url = url + uri.ToString() + "g.V(" + vertexId + ")" + graphProperties;

        var client = new RestClient(url);
        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>();

        //TODO:To check if failed.
        //dynamic jsonResponse = JsonConvert.DeserializeObject(content);

        return newProperties;
    }

    private OrbitPageCompanyUserWorkgraphyTable GetVertexDetailsFromDynamoDb(string vertexId)
    {
        IDynamoDb dynamoDbModel = new DynamoDb();
        var orbitPageCompanyUserWorkgraphyTable = dynamoDbModel.GetOrbitPageCompanyUserWorkgraphyTable(DynamoDbHashKeyDataType.VertexDetail.ToString(),
            vertexId, null);

        //if (orbitPageCompanyUserWorkgraphyTable != null && orbitPageCompanyUserWorkgraphyTable.OrbitPageVertexDetail != null)
        //    return orbitPageCompanyUserWorkgraphyTable.OrbitPageVertexDetail.properties;

        return orbitPageCompanyUserWorkgraphyTable;
    }
    private Dictionary<String, String> CreateVertex(string graphName, Dictionary<string, string> properties, string url)
    {
        var uri = new StringBuilder("/?gremlin=");
        //graph.addVertex(label, "person", "name", "marko", "age", 29);
        var response = new Dictionary<String, String>();

        string graphProperties = string.Empty;

        foreach (KeyValuePair<string, string> property in properties)
        {
            if (property.Key == VertexPropertyEnum.Type.ToString())
            {
                if (string.IsNullOrEmpty(property.Value))
                {
                    response["status"] = "404";
                    return response;
                }
                graphProperties += "label, '" + property.Value + "' ,";
            }
            else
            {
                if (property.Key == VertexPropertyEnum.CreatedTimeLong.ToString() ||
                    property.Key == VertexPropertyEnum.PostedTimeLong.ToString())
                    graphProperties += "'" + property.Key + "', " + property.Value + " ,";
                else
                {
                    graphProperties += "'" + property.Key + "', '" + property.Value + "' ,";
                }
            }                
        }

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

        url = url + uri.ToString() + "graph.addVertex(" + graphProperties + ");";
        var client = new RestClient(url);
        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);

        response["status"] = "200";
        response[TitanGraphConstants.Id] = jsonResponse.result.data[0].id;
        response[TitanGraphConstants.RexsterUri] = url;
        return response;
    }

    public string ExecuteGremlinQuery(string gremlinQuery)
    {
        string url = TitanGraphConfig.Server;
        var uri = new StringBuilder("/?gremlin=");
        url = url + uri.ToString() + gremlinQuery;
        var client = new RestClient(url);
        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 
        return content;
    }

    public string GetVertexDetail(string gremlinQuery, string vertexId, string graphName, Dictionary<string, string> properties)
    {
        var uri = new StringBuilder("/graphs/" + graphName + "/vertices/" + vertexId);
        string url = TitanGraphConfig.Server;
        if (gremlinQuery != null)
        {
            uri.Append("/tp/gremlin?");
            uri.Append("script=" + gremlinQuery);
        }
        else if (properties.Count > 0)
        {
            uri.Append("?");
            foreach (KeyValuePair<string, string> property in properties)
            {
                uri.Append(property.Key + "=" + property.Value + "&");
            }
        }

        var client = new RestClient(url + 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 
        return content;
    }

    public Dictionary<string, string> DeleteVertex(string vertexId, string userVertexId, string label)
    {
        string url = TitanGraphConfig.Server;

        IDynamoDb dynamoDbModel = new DynamoDb();
        var vertexInfo = dynamoDbModel.GetOrbitPageCompanyUserWorkgraphyTable(DynamoDbHashKeyDataType.VertexDetail.ToString(), vertexId, null);
        if (vertexInfo == null)
            return null;

        //Get all inEdges to delete
        var allInEdgesInfo = dynamoDbModel.GetOrbitPageCompanyUserWorkgraphyTableUsingInEdges(vertexId);

        //Get all outEdges to delete
        var allOutEdgesInfo = dynamoDbModel.GetOrbitPageCompanyUserWorkgraphyTableUsingOutEdges(vertexId);

        var response = DeleteVertexNative(TitanGraphConfig.Graph, vertexId, url);
        dynamoDbModel.DeleteOrbitPageCompanyUserWorkgraphyTable(vertexInfo);
        dynamoDbModel.DeleteOrbitPageCompanyUserWorkgraphyTable(allInEdgesInfo);
        dynamoDbModel.DeleteOrbitPageCompanyUserWorkgraphyTable(allOutEdgesInfo);

        return response;
    }

    private Dictionary<String, String> DeleteVertexNative(string graphName, string vertexId, string url)
    {
        var uri = new StringBuilder(url + "/graphs/" + graphName + "/vertices/" + vertexId);

        //graphs/<graph>/edges/3

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

        request.Method = Method.DELETE;
        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";
        return response;
    }
}
}

只删除几个与您无关的代码,您可以使用以下类使用REST API调用在gremlin服务器中创建,更新和删除顶点。