使用gremlin查询将Orient Vertex迁移到Cosmos DB

时间:2017-07-05 14:44:05

标签: azure-cosmosdb gremlin tinkerpop3

所以我有一个orient 2.0.0 db,我希望将db移到cosmos db。我使用的技术是.Net,所以我不能使用java和其他更丰富的驱动程序。

我在迁移过程中遇到的主要问题是我的orient顶点包含属于对象的属性。那么有没有办法使用gremlin查询将对象作为属性添加到cosmos db中。

有关gremlin的tinkerpop docAzure Cosmos DB Docs示例仅显示添加简单数据类型。

1 个答案:

答案 0 :(得分:2)

据我所知,目前使用gremlin查询到cosmos db中不支持添加对象作为属性。

我的工作是我们可以平放物体。我写了一个测试演示,你可以为它添加自己的逻辑。

以下是我的详细步骤:

1.创建一个C#项目并添加Microsoft.Azure.Graphs SDK,更多细节请参考packages.config部分。

2.为项目添加自定义类

        $clients = Client::all();

        return view('report_create')->with('clients', $clients);

3。添加一个函数将对象隐藏为Gremlin字符串

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

4.添加RunAsync功能,我们也可以从Azure门户获取演示代码

 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;
        }

5.在当地测试。

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();
        }

enter image description here

packages.config

    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();
            }