簡體   English   中英

使用gremlin查詢將Orient Vertex遷移到Cosmos DB

[英]Migrating Orient Vertex to Cosmos DB using gremlin query

所以我有一個orient 2.0.0 db ,我想將此db移到cosmos db 我正在使用的技術是.Net ,所以我不能使用Java和其他更豐富的驅動程序。

我在遷移過程中面臨的主要問題是,我的東方頂點包含作為對象的屬性。 因此,有一種方法可以使用gremlin查詢將對象作為屬性添加到cosmos db中。

tinkerpop doc和gremlin的Azure Cosmos DB Docs上的示例均顯示僅添加簡單數據類型。

據我所知,目前不支持使用gremlin查詢將對象作為屬性添加到cosmos db中。

我的工作方式是可以平整物體。 我編寫了一個測試演示,您可以在其中添加自己的邏輯。

以下是我的詳細步驟:

1.創建一個C#項目並添加Microsoft.Azure.Graphs SDK,更多詳細信息請參考packages.config部分。

2.向項目添加自定義類

public class Custom
    {
        public string Type { get; set; }
        public string Name { get; set; }
    }

3.添加一個函數將隱蔽對象添加到Gremlin字符串

 public static string CovertToGremlinString(object pObject,string type)
        {
            var propertyList = new List<string>();
           // var dic = new Dictionary<string, string>();
            if (pObject == null) return null;
            var jobject = JObject.FromObject(pObject);
            propertyList.AddRange(pObject.GetType().GetProperties().Select(prop => prop.Name));
            var s = propertyList.Aggregate($"g.addV('{type}')", (current, property) => current + $".property('{property}','{jobject[property]})')");
           // dic.Add(type, s);
            return s;
        }

4.添加RunAsync函數,我們還可以從Azure門戶獲取演示代碼

public async Task RunAsync(DocumentClient client)
    {
        Database database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "graphdb" });

        DocumentCollection graph = await client.CreateDocumentCollectionIfNotExistsAsync(
            UriFactory.CreateDatabaseUri("graphdb"),
            new DocumentCollection { Id = "Custom" },
            new RequestOptions { OfferThroughput = 1000 });

        // Azure Cosmos DB supports the Gremlin API for working with Graphs. Gremlin is a functional programming language composed of steps.
        // Here, we run a series of Gremlin queries to show how you can add vertices, edges, modify properties, perform queries and traversals
        // For additional details, see https://aka.ms/gremlin for the complete list of supported Gremlin operators
        var custom = new Custom
        {
            Name = "Tom",
            Type = "1"
        };
        var s = CovertToGremlinString(custom, "custom");
        Dictionary<string, string> gremlinQueries = new Dictionary<string, string>
        {
            {"Cleanup", "g.V().drop()"},
            {"AddVertex 1", s}
        };

        foreach (KeyValuePair<string, string> gremlinQuery in gremlinQueries)
        {
            Console.WriteLine($"Running {gremlinQuery.Key}: {gremlinQuery.Value}");


            // The CreateGremlinQuery method extensions allow you to execute Gremlin queries and iterate
            // results asychronously
            IDocumentQuery<dynamic> query = client.CreateGremlinQuery<dynamic>(graph, (string) gremlinQuery.Value);
            while (query.HasMoreResults)
            {
                foreach (dynamic result in await query.ExecuteNextAsync())
                {
                    Console.WriteLine($"\t {JsonConvert.SerializeObject(result)}");
                }
            }

            Console.WriteLine();
        }

5.在本地測試。

    string endpoint = ConfigurationManager.AppSettings["Endpoint"];
    string authKey = ConfigurationManager.AppSettings["AuthKey"];

    using (DocumentClient client = new DocumentClient(
                new Uri(endpoint),
                authKey,
                new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp }))
            {
                Program p = new Program();
                p.RunAsync(client).Wait();
            }

在此處輸入圖片說明

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.DocumentDB" version="1.14.0" targetFramework="net452" />
  <package id="Microsoft.Azure.Graphs" version="0.2.0-preview" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Common" version="1.3.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.CSharp" version="1.3.0" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
  <package id="System.Collections" version="4.0.0" targetFramework="net452" />
  <package id="System.Collections.Immutable" version="1.1.37" targetFramework="net452" />
  <package id="System.Diagnostics.Debug" version="4.0.0" targetFramework="net452" />
  <package id="System.Globalization" version="4.0.0" targetFramework="net452" />
  <package id="System.Linq" version="4.0.0" targetFramework="net452" />
  <package id="System.Reflection.Metadata" version="1.2.0" targetFramework="net452" />
  <package id="System.Resources.ResourceManager" version="4.0.0" targetFramework="net452" />
  <package id="System.Runtime" version="4.0.0" targetFramework="net452" />
  <package id="System.Runtime.Extensions" version="4.0.0" targetFramework="net452" />
  <package id="System.Threading" version="4.0.0" targetFramework="net452" />
</packages>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM