Manish Kumar
Manish Kumar

Reputation: 10482

Titan: Count edge with different propeties

    Vertex u1 = g.addVertex(null);
    Vertex u2 = g.addVertex(null);
    Vertex u3 = g.addVertex(null);
    Vertex u4 = g.addVertex(null);
    Vertex u5 = g.addVertex(null);
    Vertex u6 = g.addVertex(null);
    Vertex u7 = g.addVertex(null);
    Edge addEdge = u1.addEdge("testRate",page);
    addEdge.setProperty("testRating", 1);
    Edge addEdge1 = u2.addEdge("testRate",page);
    addEdge1.setProperty("testRating", 2);
    Edge addEdge2 = u3.addEdge("testRate",page);
    addEdge2.setProperty("testRating", 1);
    Edge addEdge3 = u4.addEdge("testRate",page);
    addEdge3.setProperty("testRating", 3);
    Edge addEdge4 = u5.addEdge("testRate",page);
    addEdge4.setProperty("testRating", 2);
    Edge addEdge5 = u6.addEdge("testRate",page);
    addEdge5.setProperty("testRating", 2);
    Edge addEdge6 = u7.addEdge("testRate",page);
    addEdge6.setProperty("testRating", 1);

To get number of rating-1

page.query().direction(Direction.IN).labels("testRate").has("testRating", 1).count();

Now for geting all different kind of rating shall I execute the same kind of code or Is there some other way?

I found some groupCount() gremlin method. Is that what i need, is there any same way in java also?

Upvotes: 0

Views: 109

Answers (2)

Manish Kumar
Manish Kumar

Reputation: 10482

Finally this worked:

  HashMap<Integer,Number>itemCount = new HashMap<Integer,Number>();
  GremlinPipeline<Vertex,Edge> pipe = new GremlinPipeline<Vertex,Edge>();
  pipe.start(v).inE("testRate").property("testRating").groupCount(itemCount).iterate();
  Iterator<Integer> iterator = itemCount.keySet().iterator(); 
  while(iterator.hasNext()) { 
      Integer next = iterator.next(); 
      System.out.println(next+"------"+itemCount.get(next));
  }

Upvotes: 0

stephen mallette
stephen mallette

Reputation: 46206

Seems like groupCount would be a better way for several reasons:

  1. It is clear - your intention to get a count of each rating type is evident in the code which makes readability nice for yourself and others
  2. It is future proof - in other words, if you hard-code the has for each rating level (1,2,3, etc) if you ever add another rating level you will have to come back and add another "count" traversal.
  3. I think it might end up being more efficient for you as vertex query isn't going to help because you intend to iterate all the testRate edges anyway. Why not do it in a single traversal.

Upvotes: 1

Related Questions