コード例 #1
0
        public async Task ModifyNode(int projectId, string uid, string author, string data, string note, string source, string typeOfData)
        {
            DgraphConnection     dgraphConnection     = new DgraphConnection(_options.Value.Host, _options.Value.Port, ChannelCredentials.Insecure);
            DgraphConnectionPool dgraphConnectionPool = new DgraphConnectionPool().Add(dgraphConnection);
            DgraphNetClient      dgraphNetClient      = new DgraphNetClient(dgraphConnectionPool);

            using (var transaction = dgraphNetClient.NewTransaction())
            {
                StringBuilder req = new StringBuilder();
                req.AppendLine("{");
                req.AppendLine("\"uid\" : \"" + uid + "\",");
                req.AppendLine("\"author\" : \"" + author + "\",");
                req.AppendLine("\"data\" : \"" + data + "\",");
                req.AppendLine("\"note\" : \"" + note + "\",");
                req.AppendLine("\"projectId\" : \"" + projectId + "\",");
                req.AppendLine("\"source\" : \"" + source + "\",");
                req.AppendLine("\"typeOfData\" : \"" + typeOfData + "\",");
                req.AppendLine("\"lastUpdate\" : \"" + DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo) + "\"");
                req.AppendLine("}");
                Mutation mu = new Mutation {
                    SetJson = ByteString.CopyFromUtf8(req.ToString())
                };
                transaction.Mutate(mu);
                transaction.Commit();
            }
        }
コード例 #2
0
ファイル: JsonUpload.cs プロジェクト: Chators/Digger
        public void DataUpload(string pathSortedJson, string dgraphSchema)
        {
            //connection à dgraph
            DgraphConnection dgraphConnection = new DgraphConnection("localhost", 9080, ChannelCredentials.Insecure);

            DgraphConnectionPool dgraphConnectionPool = new DgraphConnectionPool().Add(dgraphConnection);

            DgraphNetClient dgraphNetClient = new DgraphNetClient(dgraphConnectionPool);


            //index le schéma

            string    schema = dgraphSchema;
            Operation op     = new Operation {
                Schema = schema
            };

            dgraphNetClient.Alter(op);


            //lecture du JSON cible
            JObject jsonObj = new JObject();

            jsonObj = JObject.Parse(File.ReadAllText(pathSortedJson));

            //parsing du JSON
            string jsonString = JsonConvert.SerializeObject(jsonObj);

            //on upload le JSON sur la bdd dgraph
            using (Transaction txn = dgraphNetClient.NewTransaction())
            {
                Mutation mu = new Mutation {
                    SetJson = ByteString.CopyFromUtf8(jsonString)
                };
                txn.Mutate(mu);
                txn.Commit();
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var connection = new DgraphConnection("localhost", 9080, ChannelCredentials.Insecure);

            var pool = new DgraphConnectionPool().Add(connection);

            var client = new DgraphNetClient(pool);

            client.Alter(new Operation {
                DropAll = true
            });

            //Index le schéma
            string    schema = "domain_name_index: string @index(hash) .";
            Operation op     = new Operation {
                Schema = schema
            };

            client.Alter(op);



            using (Transaction txn = client.NewTransaction())
            {
                /***************** JSON MIL TEST*********************/
                //Ouvre le json cible
                JObject o1 = JObject.Parse(File.ReadAllText(@"c:\marche.json"));
                Dictionary <string, JToken> dic_proprety = new Dictionary <string, JToken>();


                foreach (JProperty property in o1.Properties())
                {
                    if (property.Value.HasValues)
                    {
                        dic_proprety.Add(property.Name, property.Value);
                    }
                }

                foreach (var item in dic_proprety)
                {
                    if (item.Value.GetType() == typeof(JArray))
                    {
                        if (!item.Value.HasValues)
                        {
                            dic_proprety.Remove(item.Key);
                        }
                        var    lolae = item.Value;
                        JArray qs    = (JArray)lolae;
                        for (int i = 0; i < qs.Count; i++)
                        {
                            if (qs[i].GetType() == typeof(JArray))
                            {
                                qs.RemoveAt(i);
                            }
                        }
                    }
                }

                JObject k = new JObject();
                int     b = 0;


                foreach (var item in dic_proprety)
                {
                    if (item.Value.HasValues)
                    {
                        JArray array = new JArray();
                        b++;
                        JObject obj = new JObject(
                            new JObject(
                                new JProperty("name_value_list" + b, item.Value)
                                )
                            );
                        array.Add(obj);
                        JObject objSort = new JObject();
                        objSort["list" + b] = array;
                        JObject u = new JObject();
                        u.Add("obj" + b, objSort);
                        k.Merge(u, new JsonMergeSettings {
                            MergeArrayHandling = MergeArrayHandling.Union
                        });
                    }
                }
                k.Add("domain_name_index", "Millenium");


                //Parse et seria le json cible
                var    json2 = JsonConvert.SerializeObject(k);
                string json3 = json2.Replace("true", "0");
                string json4 = json3.Replace("false", "1");
                Console.WriteLine(k);


                // Run mutation

                //JObject o7 = JObject.Parse(File.ReadAllText(@"c:\Millenium_sorted.json"));
                //var json77 = JsonConvert.SerializeObject(o7);



                Mutation mu = new Mutation {
                    SetJson = ByteString.CopyFromUtf8(json4)
                };
                txn.Mutate(mu);
                txn.Commit();


                Console.ReadLine();
                //================CREATE DATA=====================

                /* Person p = new Person();
                 * p.Name = "Marceau";
                 *
                 */
            }
            // Query

            /* string query =
             * "query all($a: string){\n" +
             * "  all(func: eq(name,$a)) {\n" +
             * "    name\n" +
             * "  }\n" +
             * "}\n";
             *
             * IDictionary<string, string> vars = new Dictionary<string, string>
             * {
             *     { "$a", "Marceau" }
             * };
             *
             * Response res = client.NewTransaction().QueryWithVars(query, vars);
             *
             * //Deserialize
             * People ppl = JsonConvert.DeserializeObject<People>(res.Json.ToStringUtf8());
             *
             * //Print results
             * Console.WriteLine($"people found: {ppl.All.Count}");
             *
             * foreach (var p in ppl.All)
             * {
             *     Console.WriteLine(p.Name);
             *
             * }
             * Console.ReadLine();*/
        }
コード例 #4
0
        public async Task AddNodesToNode(int projectId, string uidMasterNode, List <AddNodesToNodeData> nodes)
        {
            List <string> jsonMutation = new List <string>();
            StringBuilder req          = new StringBuilder();


            for (int i = 0; i < nodes.Count; i++)
            {
                AddNodesToNodeData node = nodes[i];

                // Verify if node exists
                string  r          = GetNodeBy(projectId, node.Data, node.TypeOfData);
                dynamic nodeDGraph = JsonConvert.DeserializeObject(r);

                bool nodeExists  = nodeDGraph.FindNode.Count > 0;
                bool nodeHasLink = node.Link != null;

                if (nodeExists && nodeHasLink)
                {
                    // On vérifie si les 2 noeuds ne sont pas linké
                    string sourceUid;
                    for (int y = 0; y < node.Link.Count; y++)
                    {
                        if (node.Link[y].Uid == "master")
                        {
                            sourceUid = uidMasterNode;
                        }
                        else
                        {
                            sourceUid = node.Link[y].Uid;
                        }

                        bool nodeAlreadyLinked = false;
                        if (nodeDGraph.FindNode[0]["link"] != null)
                        {
                            for (int z = 0; z < nodeDGraph.FindNode[0].link.Count; z++)
                            {
                                if (nodeDGraph.FindNode[0].link[z].uid == sourceUid)
                                {
                                    nodeAlreadyLinked = true;
                                    break;
                                }
                            }
                        }
                        if (nodeDGraph.FindNode[0]["~link"] != null)
                        {
                            for (int z = 0; z < nodeDGraph.FindNode[0]["~link"].Count; z++)
                            {
                                if (nodeDGraph.FindNode[0]["~link"][z].uid == sourceUid)
                                {
                                    nodeAlreadyLinked = true;
                                    break;
                                }
                            }
                        }
                        if (!nodeAlreadyLinked)
                        {
                            await AddEdge(projectId, sourceUid, (string)nodeDGraph.FindNode[0].uid);
                        }
                    }
                }
                else
                {
                    if (nodeHasLink)
                    {
                        for (int y = 0; y < node.Link.Count; y++)
                        {
                            string nodeSource;
                            if (node.Link[y].Uid == "master")
                            {
                                nodeSource = uidMasterNode;
                            }
                            else
                            {
                                nodeSource = node.Link[y].Uid;
                            }

                            string nodeTarget = node.Data + node.TypeOfData;
                            req = new StringBuilder();
                            req.AppendLine("{");
                            req.AppendLine("\"uid\" : \"" + nodeSource + "\",");
                            req.AppendLine("\"link\" : {");
                            req.AppendLine("\"uid\" : \"_:" + nodeTarget + "\"");
                            req.AppendLine("}");
                            req.AppendLine("}");
                            jsonMutation.Add(req.ToString());
                        }
                    }

                    req = new StringBuilder();
                    req.AppendLine("{");
                    req.AppendLine("\"uid\" : \"_:" + node.Data + node.TypeOfData + "\",");
                    req.AppendLine("\"data\" : \"" + node.Data + "\",");
                    req.AppendLine("\"author\" : \"" + node.Author + "\",");
                    req.AppendLine("\"note\" : \"" + node.Note + "\",");
                    req.AppendLine("\"projectId\" : \"" + node.ProjectId + "\",");
                    req.AppendLine("\"source\" : \"" + node.Source + "\",");
                    req.AppendLine("\"typeOfData\" : \"" + node.TypeOfData + "\",");
                    req.AppendLine("\"lastUpdate\" : \"" + node.LastUpdate + "\"");
                    req.AppendLine("}");
                    jsonMutation.Add(req.ToString());
                }
            }
            req = new StringBuilder();
            req.AppendLine("[");
            for (int n = 0; n < jsonMutation.Count; n++)
            {
                string json = jsonMutation[n];
                req.AppendLine(json);
                if (n != jsonMutation.Count - 1)
                {
                    req.AppendLine(",");
                }
            }
            req.AppendLine("]");

            // Fourberie mais ça veut dire que y'a pas de changement donc ça sert à rien
            if (req.ToString().Length != 6)
            {
                //SAVE DATA
                DgraphConnection     dgraphConnection     = new DgraphConnection(_options.Value.Host, _options.Value.Port, ChannelCredentials.Insecure);
                DgraphConnectionPool dgraphConnectionPool = new DgraphConnectionPool().Add(dgraphConnection);
                DgraphNetClient      dgraphNetClient      = new DgraphNetClient(dgraphConnectionPool);
                using (var transaction = dgraphNetClient.NewTransaction())
                {
                    Console.WriteLine(req.ToString());
                    Mutation mu = new Mutation {
                        SetJson = ByteString.CopyFromUtf8(req.ToString())
                    };
                    transaction.Mutate(mu);
                    transaction.Commit();
                }
            }
        }