全部产品
云市场

Gremlin查询

更新时间:2019-08-30 18:10:39

1. Gremlin Query 示例:

  1. public class GremlinQueryExample extends Example{
  2. public static void main(String[] args) {
  3. MaxGraph maxGraph = null;
  4. try {
  5. maxGraph = MaxGraph.newConnector(graphName, endpoint).setTimeout(30).setUserName(username)
  6. .setPassword(password).connect();
  7. MaxGraphQueryClient maxGraphQueryClient = maxGraph.createQueryClient();
  8. //query
  9. Iterator<Result> resultList = maxGraphQueryClient.query("g.V().hasLabel('domain').order().by('domain_name').limit(10)");
  10. while (resultList.hasNext()) {
  11. Result result = resultList.next();
  12. System.out.println(result.getVertex().property("domain_name"));
  13. }
  14. //create table domain_tmp(domain_name String);
  15. //query and output odps
  16. String queryStr = "g.V().hasLabel('domain').order().by('domain_name').limit(10)"
  17. + " .map(Prop.fill('domain_name'))"
  18. + " .map(Output.writeOdps()"
  19. + " .endpoint('http://service.odpsstg.aliyun-inc.com/stgnew')"
  20. + " .accessId(\"accessId\")"
  21. + " .accessKey(\"accessKey\")"
  22. + " .project(\"biggraph_dev\").table(\"domain_tmp\")"
  23. + " .write('domain_name'));";
  24. resultList = maxGraphQueryClient.query(queryStr);
  25. while (resultList.hasNext()) {
  26. Result result = resultList.next();
  27. System.out.println(String.format("%s domain results output to odps table.", result.getObject()));
  28. }
  29. //close
  30. maxGraphQueryClient.close();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. } finally {
  34. if(maxGraph != null){
  35. maxGraph.close();
  36. }
  37. }
  38. }
  39. }

2 解析Path示例:

  1. public static void main(String[] args) {
  2. MaxGraph maxGraph = null;
  3. try {
  4. maxGraph = MaxGraph.newConnector("maxgraph_demo", "http://graph.alibaba.net").setTimeout(30).setUserName("xxx")
  5. .setPassword("yyy").connect();
  6. MaxGraphQueryClient maxGraphQueryClient = maxGraph.createQueryClient();
  7. // created是边类型,src类型为person, dst类型为software
  8. Iterator<Result> result = maxGraphQueryClient.query("g.V().hasLabel('person').limit(10).outE('created').inV().path()");
  9. while (result.hasNext()) {
  10. Path p = result.next().getPath();
  11. DetachedVertex vertexOfperson = (DetachedVertex) p.objects().get(0);
  12. DetachedEdge edgeOfCreated = (DetachedEdge) p.objects().get(1);
  13. DetachedVertex vertexOfSoftware = (DetachedVertex) p.objects().get(2);
  14. System.out.println(vertexOfperson.property("name").value() + " - "
  15. + edgeOfCreated.property("weight").value() + " - "
  16. + vertexOfSoftware.property("lang").value());
  17. }
  18. //close
  19. maxGraphQueryClient.close();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. } finally {
  23. if(maxGraph != null){
  24. maxGraph.close();
  25. }
  26. }
  27. }