gremlin graph查询java

时间:2016-12-23 11:40:13

标签: java gremlin

这是我在尝试图形查询的代码。

package com;  
    import java.util.ArrayList;
    import java.util.List;

import org.apache.commons.configuration.BaseConfiguration;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;

public class SampleTitanMatchability {

    @SuppressWarnings("rawtypes")
    public static double matchability(GraphTraversalSource g, Vertex a, Vertex b) {
        int matchedQues = 0;
        int matchedAns = 0;

        List list1 = new ArrayList();
        List list2 = new ArrayList();

        list1 = g.V(a).outE().label().toList();
        System.out.println("total answered questions :" + list1);

        list2 = g.V(b).outE().label().toList();
        System.out.println("total answered questions :" + list2);

        if (list1.size() > list2.size())
        {   
            for (int i = 0; i < list2.size(); i++)
            {
                if (list1.contains(list2.get(i))) 
                {
                    matchedQues++;

                    if(g.V(a).outE(list2.get(i).toString()).next().inVertex().property("ans").toString().equals(g.V(b).outE(list2.get(i).toString()).next().inVertex().property("ans").toString()))
                    {
                        matchedAns++;
                    }

                }
            }

            System.out.println("matched questions :"+matchedQues);

            System.out.println("matched ans :"+matchedAns);

        }       
        else
        {
            for (int i = 0; i < list1.size(); i++)
            {
                if (list2.contains(list1.get(i))) 
                {
                    matchedQues++;

                    if(g.V(a).outE(list1.get(i).toString()).next().inVertex().property("ans").toString().equals(g.V(b).outE(list1.get(i).toString()).next().inVertex().property("ans").toString()))
                    {
                        matchedAns++;
                    }

                }
            }

            System.out.println("matched questions :"+matchedQues);

            System.out.println("matched ans :"+matchedAns);
        }

        double per = (matchedAns*100)/matchedQues;
        return per;
    }

    @SuppressWarnings({})
    public static void main(String[] args) throws Exception {

        BaseConfiguration conf = new BaseConfiguration();

        conf.setProperty("storage.backend", "com.amazon.titan.diskstorage.dynamodb.DynamoDBStoreManager");
        conf.setProperty("storage.dynamodb.client.endpoint", "http://localhost:4567");
        conf.setProperty("index.search.backend", "elasticsearch");
        conf.setProperty("index.search.directory", "/tmp/searchindex");
        conf.setProperty("index.search.elasticsearch.client-only", "false");
        conf.setProperty("index.search.elasticsearch.local-mode", "true");

        TitanGraph graph = TitanFactory.open(conf);

        GraphTraversalSource g = graph.traversal();

        Vertex mayank = graph.addVertex("name", "mayank");
        Vertex mque1 = graph.addVertex("ans", "yes");
        mayank.addEdge("que1", mque1);
        Vertex mque2 = graph.addVertex("ans", "yes");
        mayank.addEdge("que2", mque2);
        Vertex mque3 = graph.addVertex("ans", "yes");
        mayank.addEdge("que3", mque3);
        //Vertex mque4 = graph.addVertex("ans", "yes");
        //mayank.addEdge("que4", mque4);
        Vertex mque5 = graph.addVertex("ans", "no");
        mayank.addEdge("que5", mque5);
        Vertex mque6 = graph.addVertex("ans", "yes");
        mayank.addEdge("que6", mque6);
        Vertex mque7 = graph.addVertex("ans", "yes");
        mayank.addEdge("que7", mque7);
        Vertex mque8 = graph.addVertex("ans", "yes");
        mayank.addEdge("que8", mque8);
        Vertex mque9 = graph.addVertex("ans", "yes");
        mayank.addEdge("que9", mque9);
        Vertex mque10 = graph.addVertex("ans", "no");
        mayank.addEdge("que10", mque10);
        //Vertex mque11 = graph.addVertex("ans", "yes");
        //mayank.addEdge("que11", mque11);
        Vertex mque12 = graph.addVertex("ans", "yes");
        mayank.addEdge("que12", mque12);
        Vertex mque13 = graph.addVertex("ans", "yes");
        mayank.addEdge("que13", mque13);
        Vertex mque14 = graph.addVertex("ans", "no");
        mayank.addEdge("que14", mque14);
        Vertex mque15 = graph.addVertex("ans", "yes");
        mayank.addEdge("que15", mque15);

        Vertex poonam = graph.addVertex("name", "poonam");
        Vertex pque1 = graph.addVertex("ans", "yes");
        poonam.addEdge("que1", pque1);
        Vertex pque2 = graph.addVertex("ans", "yes");
        poonam.addEdge("que2", pque2);
        Vertex pque3 = graph.addVertex("ans", "yes");
        poonam.addEdge("que3", pque3);
        Vertex pque4 = graph.addVertex("ans", "no");
        poonam.addEdge("que4", pque4);
        Vertex pque5 = graph.addVertex("ans", "yes");
        poonam.addEdge("que5", pque5);
        Vertex pque6 = graph.addVertex("ans", "yes");
        poonam.addEdge("que6", pque6);
        Vertex pque7 = graph.addVertex("ans", "yes");
        poonam.addEdge("que7", pque7);
        Vertex pque8 = graph.addVertex("ans", "no");
        poonam.addEdge("que8", pque8);
        //Vertex pque9 = graph.addVertex("ans", "yes");
        //poonam.addEdge("que9", pque9);
        Vertex pque10 = graph.addVertex("ans", "no");
        poonam.addEdge("que10", pque10);
        Vertex pque11 = graph.addVertex("ans", "yes");
        poonam.addEdge("que11", pque11);
        Vertex pque12 = graph.addVertex("ans", "yes");
        poonam.addEdge("que12", pque12);
        Vertex pque13 = graph.addVertex("ans", "yes");
        poonam.addEdge("que13", pque13);
        // Vertex pque14 = graph.addVertex("ans", "no");
        // poonam.addEdge("que14", pque14);
        // Vertex pque15 = graph.addVertex("ans", "yes");
        // poonam.addEdge("que15", pque15);

        Vertex bhoomi = graph.addVertex("name", "bhoomi");
        //Vertex bque1 = graph.addVertex("ans", "yes");
        //bhoomi.addEdge("que1", bque1);
        Vertex bque2 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que2", bque2);
        Vertex bque3 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que3", bque3);
        Vertex bque4 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que4", bque4);
        Vertex bque5 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que5", bque5);
        Vertex bque6 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que6", bque6);
        Vertex bque7 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que7", bque7);
        Vertex bque8 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que8", bque8);
        Vertex bque9 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que9", bque9);
        // Vertex bque10 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que10", bque10);
        // Vertex bque11 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que11", bque11);
        // Vertex bque12 = graph.addVertex("ans", "yes");
        // bhoomi.addEdge("que12", bque12);
        // Vertex bque13 = graph.addVertex("ans", "yes");
        // bhoomi.addEdge("que13", bque13);
        // Vertex bque14 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que14", bque14);
        // Vertex bque15 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que15", bque15);

        System.out.println("graph is " + graph);
        System.out.println("Graph created.....");
        System.out.println();

        double i = matchability(g, mayank, poonam);
        System.out.println("matchability between mayank and poonam is :" + i + "%");
        System.out.println();

        double j = matchability(g, mayank, bhoomi);
        System.out.println("matchability between mayank and bhoomi is :" + j + "%");
        System.out.println();


        double k = matchability(g, bhoomi, poonam);
        System.out.println("matchability between bhoomi and poonam is :" + k + "%");
        System.out.println();

        graph.close();
        System.exit(0);

        System.out.println("graph is " + graph);

        graph.close();
        System.exit(0);

    }

}

以下是我的代码如何减少matchability方法中的代码? 我希望它减少为gremlin query. 我无法将它减少到gremlin query。 请为我的代码建议适当的查询。

1 个答案:

答案 0 :(得分:3)

几乎相同的代码被打包到一个Gremlin遍历中:

public static double matchability(GraphTraversalSource g, Vertex a, Vertex b) {
    return g.withSack(0).V(a).outE().aggregate("x").by(label).limit(1)
            .V(b).outE().as("e").label().as("l").where(select("x").unfold().as("l"))
            .store("matchedQues").by(constant(1))
            .V(a).outE().where(label().as("l")).inV().as("a1")
            .select("e").inV().as("a2").where("a1", eq("a2")).by("ans")
            .aggregate("matchedAns").limit(1)
            .sack(assign).by(select("matchedAns").count(Scope.local))
            .sack(mult).by(constant(100))
            .sack(div).by(select("matchedQues").count(Scope.local)).<Long>sack()
            .tryNext().orElse(0L);
}

<强>更新

对于TP 3.0.1-incubating,您需要做更多的手工操作。这对我有用:

final Set<String> x = g.V(a).outE().label().toSet();
final Map<String, Long> m = g.withSideEffect("x", x).V(b).outE().as("e").label().as("l")
        .where(select("x").unfold().as("l"))
        .store("matchedQues").by(constant(1))
        .constant(a).outE().where(label().as("l")).inV().as("a1")
        .select("e").inV().as("a2").filter(select("a1", "a2").by("ans").where("a1", eq("a2")))
        .aggregate("matchedAns").by(constant(1)).cap("matchedAns", "matchedQues")
        .<Long>select("matchedAns", "matchedQues").by(count(local)).next();
return (m.get("matchedAns") * 100) / m.get("matchedQues");